Skip to content

Instantly share code, notes, and snippets.

@elwinschmitz
Created April 6, 2017 13:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elwinschmitz/d7801b0cb5dcb603f20b11b1561db997 to your computer and use it in GitHub Desktop.
Save elwinschmitz/d7801b0cb5dcb603f20b11b1561db997 to your computer and use it in GitHub Desktop.
Convert a SVG into CSS background-image url
var svg = '<svg width="10" height="10" xmlns="http://www.w3.org/2000/svg"><circle fill="#F00" cx="5" cy="5" r="5"></circle></svg>';
var url = 'data:image/svg+xml,' + encodeURI(svg).replace(/#/g, '%23').replace(/%20/g, ' ').replace(/%22/g, "'");
console.log('background-image: url("' + url + '");');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment