A
A
Alexander Prisyazhnyuk2018-11-03 02:53:22
Sass
Alexander Prisyazhnyuk, 2018-11-03 02:53:22

How to insert svg into SASS(SCSS) variable?

The question is quite simple and stupid, but I could not find the answer to it on my own (I googled, selected values, sat with a languid look thinking about the meaning of life svg).
Recently I started to get acquainted with svg and surfing the Internet, I found a nice function to convert svg encoding to URL . All this is needed, obviously, to reduce http requests on the page and everyone's favorite automation.
Below is the function code:

@function svg-url($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);

    $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;
  }
  // sass-lint:disable quotes
  @return url("data:image/svg+xml;utf-8,#{$encoded}");
  // sass-lint:enable quotes
}

@mixin background-svg($svg) {
  background-image: svg-url($svg);
}

The SVG code needs to be inserted into the $svg variable , this is my stupid question, actually how to do it?
No matter how I do it, it doesn’t work out, most likely it’s simple, but it often happens with us that you write complex code, but you fall into a blunt on such a simple thing ...
How it should look like in the end:
5bddbc8854d4e009270431.png
In the code:
5bddbd2ab353e561374808.png
Once again, I ask sorry for the stupid question and thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Veronica, 2019-03-01
@niktariy

https://codepen.io/kevinweber/pen/dXWoRw
I don't know how it works, but you can try :))

F
foronlyfriends, 2019-07-11
@foronlyfriends

$sort: '%3Csvg ...
background-image: url("data:image/svg+xml, #{$sort} ");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question