Last active
July 26, 2016 10:32
-
-
Save leejordan/01b7ce9ed7c9cef42482bbce8889710a to your computer and use it in GitHub Desktop.
helper for encoding svgs into data-uri format with the highest cross browser compatibility possible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Based on the findings of this post for the most cross browser compatible svg data-uri encoding: | |
https://codepen.io/tigt/post/optimizing-svgs-in-data-uris | |
Save the following in a file (no file extension required): | |
s/"/'/g | |
s/#/%23/g | |
s/</%3C/g | |
s/>/%3E/g | |
s/&/%26/g | |
Then run this in the terminal: | |
sed -f <file with patterns> <file to change> | |
## LESS mixin: | |
.svg-data-uri-bg(@svg) { | |
@svg-quotes: replace(~"@{svg}", '"', "'", "g"); | |
@svg-hash: replace(~"@{svg-quotes}", "#", "%23", "g"); | |
@svg-less: replace(~"@{svg-hash}", "<", "%3C", "g"); | |
@svg-more: replace(~"@{svg-less}", ">", "%3E", "g"); | |
@svg-amp: replace(~"@{svg-more}", "&", "%26", "g"); | |
background-image: url(~"data:image/svg+xml, @{svg-amp}"); | |
} | |
Given an svg as an argument, will output the correctly formatted data-uri as background-image url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment