Created
January 5, 2022 10:37
-
-
Save elfacht/4965399a690f67ce6deb7a66cfbb0c97 to your computer and use it in GitHub Desktop.
Convert SVG to data-uri in SCSS
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
// choose a color | |
$icon-color: #F84830; | |
// functions to urlencode the svg string | |
@function str-replace($string, $search, $replace: '') { | |
$index: str-index($string, $search); | |
@if $index { | |
@return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace); | |
} | |
@return $string; | |
} | |
@function url-encode($string) { | |
$map: ( | |
"%": "%25", | |
"<": "%3C", | |
">": "%3E", | |
" ": "%20", | |
"!": "%21", | |
"*": "%2A", | |
"'": "%27", | |
'"': "%22", | |
"(": "%28", | |
")": "%29", | |
";": "%3B", | |
":": "%3A", | |
"@": "%40", | |
"&": "%26", | |
"=": "%3D", | |
"+": "%2B", | |
"$": "%24", | |
",": "%2C", | |
"/": "%2F", | |
"?": "%3F", | |
"#": "%23", | |
"[": "%5B", | |
"]": "%5D" | |
); | |
$new: $string; | |
@each $search, $replace in $map { | |
$new: str-replace($new, $search, $replace); | |
} | |
@return $new; | |
} | |
@function inline-svg($string) { | |
@return url('data:image/svg+xml;charset=US-ASCII,#{url-encode($string)}'); | |
} | |
// icon styles | |
// note the fill="' + $icon-color + '" | |
.icon { | |
display: inline-block; | |
width: 50px; | |
height: 50px; | |
background: inline-svg('<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" | |
viewBox="0 0 30 30" enable-background="new 0 0 30 30" xml:space="preserve"> | |
<path fill="' + $icon-color + '" d="M18.7,10.1c-0.6,0.7-1,1.6-0.9,2.6c0,0.7-0.6,0.8-0.9,0.3c-1.1-2.1-0.4-5.1,0.7-7.2c0.2-0.4,0-0.8-0.5-0.7 | |
c-5.8,0.8-9,6.4-6.4,12c0.1,0.3-0.2,0.6-0.5,0.5c-0.6-0.3-1.1-0.7-1.6-1.3c-0.2-0.3-0.4-0.5-0.6-0.8c-0.2-0.4-0.7-0.3-0.8,0.3 | |
c-0.5,2.5,0.3,5.3,2.1,7.1c4.4,4.5,13.9,1.7,13.4-5.1c-0.2-2.9-3.2-4.2-3.3-7.1C19.6,10,19.1,9.6,18.7,10.1z"/> | |
</svg>'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment