Skip to content

Instantly share code, notes, and snippets.

@0kenx
Forked from jennyknuth/README.md
Created January 15, 2024 02:02
Show Gist options
  • Save 0kenx/64da99eb3c8c29ac81775ac0092c8d35 to your computer and use it in GitHub Desktop.
Save 0kenx/64da99eb3c8c29ac81775ac0092c8d35 to your computer and use it in GitHub Desktop.

Revisions

  1. @jennyknuth jennyknuth revised this gist Mar 6, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # How to transform an SVG into a data URI
    by Jenny Knuth, based on the work of Chris Coyier and Taylor Hunt

    A data URI is a nice way to include a web resource without needing to make an HTTP request. Chris Coyier explains the technique nicely in [Probably Don't Base64 SVG](https://css-tricks.com/probably-dont-base64-svg/).

  2. @jennyknuth jennyknuth revised this gist Feb 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # How to transform an SVG into a data URI

    Using a data URI is a nice way to include a web resource without needing to make an HTTP request. Chris Coyier explains the technique nicely in [Probably Don't Base64 SVG](https://css-tricks.com/probably-dont-base64-svg/).
    A data URI is a nice way to include a web resource without needing to make an HTTP request. Chris Coyier explains the technique nicely in [Probably Don't Base64 SVG](https://css-tricks.com/probably-dont-base64-svg/).

    While a PNG might use Base64 encoding, for SVG, there is a better way.

  3. @jennyknuth jennyknuth revised this gist Feb 20, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # How to transform an SVG into a data URI

    Using a data URI is a nice way to include a web resource without needing to make an HTTP request. Chris Coyier explains the technique nicely in [Probably Don't Base64 SVG](https://css-tricks.com/probably-dont-base64-svg/).

    While a PNG might use Base64 encoding, for SVG, there is a better way.
  4. @jennyknuth jennyknuth revised this gist Feb 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ Here is a step-by-step method to create your encoded SVG string to include as a

    1. Replace all non-ASCII and URL-unsafe characters by running your SVG string through an encoding function like the one below by Taylor Hunt—surround your `svgString` with double quotes in the function's argument.

    1. Paste your encoded SVG into your HTML tag's `src` attribute or CSS's url call using double quotes, after the `data:image/sg+xml,` prefix.
    1. Paste your encoded SVG into your HTML tag's `src` attribute or CSS's `url` call using double quotes, after the `data:image/sg+xml,` prefix.

    Examples:

  5. @jennyknuth jennyknuth revised this gist Feb 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ HTML:
    CSS:

    ```css
    .image {
    .logo {
    background: url("data:image/svg+xml,%3Csvg ... /svg%3E");
    }
    ```
  6. @jennyknuth jennyknuth revised this gist Feb 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ HTML:
    CSS:

    ```css
    .bg {
    .image {
    background: url("data:image/svg+xml,%3Csvg ... /svg%3E");
    }
    ```
  7. @jennyknuth jennyknuth revised this gist Feb 20, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -8,13 +8,13 @@ Taylor Hunt's experiments led to this solution for [optimizing SVGs in data URIs

    Here is a step-by-step method to create your encoded SVG string to include as a data URI:

    1. run your SVG file through [svgo](https://github.com/svg/svgo) to optimize. (Directions are found at the svgo link.)
    1. Run your SVG file through [svgo](https://github.com/svg/svgo) to optimize. (Directions are found at the svgo link.)

    1. In your text editor, replace all double quotes in your SVG file with single quotes. Make sure there are no line breaks.

    1. Replace all non-ASCII and URL-unsafe characters by running your SVG string through an encoding function like the one below—surround your `svgString` with double quotes in the function's argument.
    1. Replace all non-ASCII and URL-unsafe characters by running your SVG string through an encoding function like the one below by Taylor Hunt—surround your `svgString` with double quotes in the function's argument.

    1. Paste your encoded SVG into your html tag's `src` attribute or css's url call using double quotes, after the `data:image/sg+xml,` prefix.
    1. Paste your encoded SVG into your HTML tag's `src` attribute or CSS's url call using double quotes, after the `data:image/sg+xml,` prefix.

    Examples:

  8. @jennyknuth jennyknuth revised this gist Feb 20, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -31,5 +31,6 @@ CSS:
    background: url("data:image/svg+xml,%3Csvg ... /svg%3E");
    }
    ```
    The `%3svg` starts your encoded string and the `svg%3E` ends your encoded string.

    Let me know if any questions.
  9. @jennyknuth jennyknuth revised this gist Feb 20, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -8,11 +8,11 @@ Taylor Hunt's experiments led to this solution for [optimizing SVGs in data URIs

    Here is a step-by-step method to create your encoded SVG string to include as a data URI:

    1. run your SVG file through [svgo](https://github.com/svg/svgo).
    1. run your SVG file through [svgo](https://github.com/svg/svgo) to optimize. (Directions are found at the svgo link.)

    1. In your text editor, replace all double quotes in your SVG file with single quotes. Make sure there are no line breaks.

    1. Replace all non-ASCII and URL-unsafe characters by running your svg string through an encoding function like the one below—surround your `svgString` with double quotes in the function's argument.
    1. Replace all non-ASCII and URL-unsafe characters by running your SVG string through an encoding function like the one below—surround your `svgString` with double quotes in the function's argument.

    1. Paste your encoded SVG into your html tag's `src` attribute or css's url call using double quotes, after the `data:image/sg+xml,` prefix.

  10. @jennyknuth jennyknuth revised this gist Feb 20, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -8,13 +8,13 @@ Taylor Hunt's experiments led to this solution for [optimizing SVGs in data URIs

    Here is a step-by-step method to create your encoded SVG string to include as a data URI:

    - 1 run your SVG file through [svgo](https://github.com/svg/svgo).
    1. run your SVG file through [svgo](https://github.com/svg/svgo).

    - 2 In your text editor, replace all double quotes in your SVG file with single quotes. Make sure there are no line breaks.
    1. In your text editor, replace all double quotes in your SVG file with single quotes. Make sure there are no line breaks.

    - 2 Replace all non-ASCII and URL-unsafe characters by running your svg string through an encoding function like the one below—surround your `svgString` with double quotes in the function's argument.
    1. Replace all non-ASCII and URL-unsafe characters by running your svg string through an encoding function like the one below—surround your `svgString` with double quotes in the function's argument.

    - 1 Paste your encoded SVG into your html tag's `src` attribute or css's url call using double quotes, after the `data:image/sg+xml,` prefix.
    1. Paste your encoded SVG into your html tag's `src` attribute or css's url call using double quotes, after the `data:image/sg+xml,` prefix.

    Examples:

  11. @jennyknuth jennyknuth revised this gist Feb 20, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,9 @@ While a PNG might use Base64 encoding, for SVG, there is a better way.

    Taylor Hunt's experiments led to this solution for [optimizing SVGs in data URIs](https://codepen.io/tigt/post/optimizing-svgs-in-data-uris):

    "So the best way of encoding SVG in a data uri is `data:image/svg+xml,[actual data]`. We don’t need the `;charset=utf-8` parameter (or the invalid `;utf8` parameter…), because URLs are always ASCII."
    "So the best way of encoding SVG in a data URI is `data:image/svg+xml,[actual data]`. We don’t need the `;charset=utf-8` parameter (or the invalid `;utf8` parameter…), because URLs are always ASCII."

    Here is a step-by-step method to create your encoded SVG string:
    Here is a step-by-step method to create your encoded SVG string to include as a data URI:

    - 1 run your SVG file through [svgo](https://github.com/svg/svgo).

  12. @jennyknuth jennyknuth revised this gist Feb 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ Using a data URI is a nice way to include a web resource without needing to make

    While a PNG might use Base64 encoding, for SVG, there is a better way.

    Taylor Hunt's experiments led to this solution for [optimizing SVGs in data uris](https://codepen.io/tigt/post/optimizing-svgs-in-data-uris):
    Taylor Hunt's experiments led to this solution for [optimizing SVGs in data URIs](https://codepen.io/tigt/post/optimizing-svgs-in-data-uris):

    "So the best way of encoding SVG in a data uri is `data:image/svg+xml,[actual data]`. We don’t need the `;charset=utf-8` parameter (or the invalid `;utf8` parameter…), because URLs are always ASCII."

  13. @jennyknuth jennyknuth revised this gist Feb 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    Using a data uri is a nice way to include a resource without needing to make an HTTP request. Chris Coyier explains the technique nicely in [Probably Don't Base64 SVG](https://css-tricks.com/probably-dont-base64-svg/).
    Using a data URI is a nice way to include a web resource without needing to make an HTTP request. Chris Coyier explains the technique nicely in [Probably Don't Base64 SVG](https://css-tricks.com/probably-dont-base64-svg/).

    While a PNG might use Base64 encoding, for SVG, there is a better way.

  14. @jennyknuth jennyknuth revised this gist Feb 20, 2018. No changes.
  15. @jennyknuth jennyknuth created this gist Feb 20, 2018.
    35 changes: 35 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    Using a data uri is a nice way to include a resource without needing to make an HTTP request. Chris Coyier explains the technique nicely in [Probably Don't Base64 SVG](https://css-tricks.com/probably-dont-base64-svg/).

    While a PNG might use Base64 encoding, for SVG, there is a better way.

    Taylor Hunt's experiments led to this solution for [optimizing SVGs in data uris](https://codepen.io/tigt/post/optimizing-svgs-in-data-uris):

    "So the best way of encoding SVG in a data uri is `data:image/svg+xml,[actual data]`. We don’t need the `;charset=utf-8` parameter (or the invalid `;utf8` parameter…), because URLs are always ASCII."

    Here is a step-by-step method to create your encoded SVG string:

    - 1 run your SVG file through [svgo](https://github.com/svg/svgo).

    - 2 In your text editor, replace all double quotes in your SVG file with single quotes. Make sure there are no line breaks.

    - 2 Replace all non-ASCII and URL-unsafe characters by running your svg string through an encoding function like the one below—surround your `svgString` with double quotes in the function's argument.

    - 1 Paste your encoded SVG into your html tag's `src` attribute or css's url call using double quotes, after the `data:image/sg+xml,` prefix.

    Examples:

    HTML:

    ```html
    <img src="data:image/svg+xml,%3Csvg width='247' height='34' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23FFF' fill-rule='non.zero'%3E%3Cpath d='M13.918 ... 4.022-2.888 4.57V26h-4.46V7.975z' fill='%23D7DCE1'/%3E%3C/g%3E%3C/svg%3E"/>
    ```

    CSS:

    ```css
    .bg {
    background: url("data:image/svg+xml,%3Csvg ... /svg%3E");
    }
    ```

    Let me know if any questions.
    32 changes: 32 additions & 0 deletions encodeSvg.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    // this function is from the work of Taylor Hunt found at https://codepen.io/tigt/post/optimizing-svgs-in-data-uris

    function encodeSvg(svgString) {
    return svgString.replace('<svg',(~svgString.indexOf('xmlns')?'<svg':'<svg xmlns="http://www.w3.org/2000/svg"'))

    //
    // Encode (may need a few extra replacements)
    //
    .replace(/"/g, '\'')
    .replace(/%/g, '%25')
    .replace(/#/g, '%23')
    .replace(/{/g, '%7B')
    .replace(/}/g, '%7D')
    .replace(/</g, '%3C')
    .replace(/>/g, '%3E')

    .replace(/\s+/g,' ')
    //
    // The maybe list (add on documented fail)
    //
    // .replace(/&/g, '%26')
    // .replace('|', '%7C')
    // .replace('[', '%5B')
    // .replace(']', '%5D')
    // .replace('^', '%5E')
    // .replace('`', '%60')
    // .replace(';', '%3B')
    // .replace('?', '%3F')
    // .replace(':', '%3A')
    // .replace('@', '%40')
    // .replace('=', '%3D')
    ;}