Last active
May 27, 2021 19:00
-
-
Save kevindees/32a65bf7e64971e2c6b294e99cee2c89 to your computer and use it in GitHub Desktop.
SVG SCSS/SASS Background Image URL Encode
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
// Replace letters | |
@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; | |
} | |
// Encode symbols | |
@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,#{url-encode($string)}'); | |
} | |
@mixin background-svg($svg){ | |
background-image: inline-svg($svg); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage example: