Skip to content

Instantly share code, notes, and snippets.

@gemmadlou
Created November 23, 2016 12:48
Show Gist options
  • Select an option

  • Save gemmadlou/6435d6abfaf7bd3e41df442e243b5317 to your computer and use it in GitHub Desktop.

Select an option

Save gemmadlou/6435d6abfaf7bd3e41df442e243b5317 to your computer and use it in GitHub Desktop.
Background Image SVG SASS Example
@function svg-url($svg) {
@if not str-index($svg,xmlns) {
$svg: str-replace($svg, '<svg','<svg xmlns="http://www.w3.org/2000/svg"');
}
$encoded:'';
$slice: 2000;
$index: 0;
$loops: ceil(str-length($svg)/$slice);
@for $i from 1 through $loops {
$chunk: str-slice($svg, $index, $index + $slice - 1);
//
// Encode
//
$chunk: str-replace($chunk,'"', '\'');
$chunk: str-replace($chunk,'%', '%25');
$chunk: str-replace($chunk,'&', '%26');
$chunk: str-replace($chunk,'#', '%23');
$chunk: str-replace($chunk,'{', '%7B');
$chunk: str-replace($chunk,'}', '%7D');
$chunk: str-replace($chunk,'<', '%3C');
$chunk: str-replace($chunk,'>', '%3E');
$encoded: #{$encoded}#{$chunk};
$index: $index + $slice;
}
@return url("data:image/svg+xml,#{$encoded}");
}
// Helper function to replace characters in a string
@function str-replace($string, $search, $replace: '') {
$index: str-index($string, $search);
@return if($index,
str-slice($string, 1, $index - 1) + $replace +
str-replace(str-slice($string, $index +
str-length($search)), $search, $replace),
$string);
}
@function svg-arrow-right($color: $white) {
$svgurl: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 477.175 477.175"><path fill="'+$color+'" d="M360.731 229.075l-225.1-225.1c-5.3-5.3-13.8-5.3-19.1 0s-5.3 13.8 0 19.1l215.5 215.5-215.5 215.5c-5.3 5.3-5.3 13.8 0 19.1 2.6 2.6 6.1 4 9.5 4 3.4 0 6.9-1.3 9.5-4l225.1-225.1c5.3-5.2 5.3-13.8.1-19z"/></svg>';
@return svg-url($svgurl);
}
@gemmadlou

Copy link
Copy Markdown
Author

Use like:

background-image: svg-arrow-right($white);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment