cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="container">
  <div class="row">
    
    <div class="col-xs-12 input-area">
      <h1>SVG to Data URI converter</h1>
      <p>Paste or drag the contents of a valid SVG document source below and generate the data URI that should work cross-browser</p>
    </div>
    <div class="col-xs-12 col-sm-6 input-area">
      <div class="form-group">
        <label for="input">Enter your SVG here (demos: 
          <button id="circle" class="btn btn-xs">1</button>
          <button id="use" class="btn btn-xs">2</button>
          <button id="animate" class="btn btn-xs" title="only works in some browsers e.g. chrome">3</button>
          <button id="trans" class="btn btn-xs">4</button>
          <button id="fenders" class="btn btn-xs">5</button>
          )
        </label>
        <textarea id="input" placeholder="<svg ..."></textarea>
      </div>

      <div class="form-group">
        <button type="button" id="convert" class="btn btn-primary">Convert ...</button>
        bg:
        <input type="color" value="#ffffff" id="background-colour"/>
      </div>
      <p>If you use this great tool then please comment and/or like this pen.</p>
    </div>
    <div class="col-xs-12 col-sm-6 input-area">
      <div class="form-group">
        <label for="output">CSS generated here</label>
        <textarea id="output" placeholder=""></textarea>
      </div>

      <div class="form-group">
        <button type="button" id="copy" class="btn btn-default">Copy to clipboard</button>
      </div>
      
    </div>

  </div>
</div>
            
          
!
            
              .input-area {
  background: #FFF;
  background: rgba(255, 255, 255, 0.9);
}

textarea {
  width: 100%;
  height: 16em;
}

/* also check out the similar https://websemantics.uk/tools/svg-to-background-image-conversion/ which is pretty cool and supplies some context too */
            
          
!
            
              function escapeRegExp(str) {
  return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}

$(document).ready(function () {
  $("#circle").click(function(){
    $("#input").val('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40" width="40" height="40">\r\n  <circle cx="20" cy="20" r="20" fill="none" stroke="#aaa" stroke-width="2" />\r\n</svg>');
  });

  $("#use").click(function(){
    $("#input").val('<svg viewBox="0 0 96 64" width="96" height="64" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">\r\n  <defs>\r\n    <symbol id="c">\r\n      <path fill="inherit" d="M0 0v32h32V0H0zm24 9.6c1 0 2 1.4 1 2.4L15 22c-.6.5-1.4.5-2 0l-4-4c-1.2-1.3.7-3.2 2-2l3 3 9-9c.4-.3.7-.4 1-.4z"/>\r\n    </symbol>\r\n  </defs>\r\n  <use x="0" y="0" xlink:href="#c" fill="olive" />\r\n  <use x="32" y="0" xlink:href="#c" fill="green" />\r\n  <use x="64" y="0" xlink:href="#c" fill="forestgreen" />\r\n  <use x="0" y="32" xlink:href="#c" fill="seagreen" />\r\n  <use x="32" y="32" xlink:href="#c" fill="darkolivegreen" />\r\n  <use x="64" y="32" xlink:href="#c" fill="olivedrab" />\r\n</svg>');
  });

  $("#animate").click(function(){
    $("#input").val('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40" width="40" height="40">\r\n<style type="text/css">\r\n  #c {animation: x 5s alternate infinite;}\r\n  @keyframes x { from { fill: gold; } to { fill: purple} }\r\n</style>\r\n<circle id="c" cx="20" cy="20" r="20" fill="gold"/>\r\n<!-- works in chrome ... not in IE and others -->\r\n</svg>');
  });
  
    $("#trans").click(function(){
    $("#input").val('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="32" height="32">\r\n    <path fill="#ddd" d="m0 0h16v32h16V16H0z" />\r\n</svg>');
  });
  
$("#fenders").click(function(){
    $("#input").val('<svg xmlns="http://www.w3.org/2000/svg" width="200" height="58" viewBox="0 0 200 58">\r\n  <defs>\r\n	  <linearGradient id="fl-tr" y2="1">\r\n		<stop offset="0" stop-color="#f47e5c"/>\r\n		<stop offset="1" stop-color="#f16281"/>\r\n	  </linearGradient>\r\n	  <linearGradient id="fl-bl" y2="1">\r\n		<stop offset="0" stop-color="#45bb8b"/>\r\n		<stop offset="1" stop-color="#26c4f4"/>\r\n	  </linearGradient>\r\n	  <linearGradient id="fl-tl" y2="1">\r\n		<stop offset=".3" stop-color="#f9bd27"/>\r\n		<stop offset=".7" stop-color="#f47e5c"/>\r\n	  </linearGradient>\r\n	  <linearGradient id="fl-br" y2="1">\r\n		<stop offset=".3" stop-color="#7650a0"/>\r\n		<stop offset=".7" stop-color="#6b3895"/>\r\n	  </linearGradient>\r\n  </defs>\r\n  <path fill="url(#fl-tr)" d="M170.1 20c-2.7-2.7-2.7-7 0-9.6 2.7-2.7 7-2.7 9.6 0l17.7 17.7c2.7 2.7 2.7 7 0 9.6-2.7 2.7-7 2.7-9.6 0L170.1 20z"/>\r\n  <path fill="url(#fl-bl)" d="M29.9 38c2.7 2.7 2.7 7 0 9.6-2.7 2.7-7 2.7-9.6 0L2.6 29.9c-2.7-2.7-2.7-7 0-9.6 2.7-2.7 7-2.7 9.6 0L29.9 38z"/>\r\n  <path fill="black" d="M48.3 20.2c-7.6 0-12.8 6.2-12.8 13.6v.1c0 8 5.8 13.5 13.6 13.5 3.8 0 6.7-1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0-9.7l17.4-17.6c1.2-1.2 3.6-4.6 1.5-8.1 0 0 6.6 6.6 8 8.1 2.6 2.7 2.6 7 0 9.7L180 55.6c-2.5 2.7-6.8 2.7-9.5 0z"/>\r\n</svg>');
  });
  
  $("#copy").click(function(){
    $("#output").focus();
    $("#output").select();
    try {
      var successful = document.execCommand('copy');
      var msg = successful ? 'successful' : 'unsuccessful';
      console.log('Copying text command was ' + msg);
    } catch (err) {
      console.log('Oops, unable to copy');
    }
  });

  // URIs as defined by RFC 3986 (see Section 2: Characters) 
  // may contain any of the following characters:
  // ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=`.

  $("#convert").click(function(){
    var raw = $("#input").val();
    var encoded = raw.replace(/\s+/g, " ")
    
    // According to Taylor Hunt, lowercase gzips better ... my tiny test confirms this
    encoded = replaceAll(encoded, "%", "%25"); 
    encoded = replaceAll(encoded, "> <", "><"); // normalise spaces elements
    encoded = replaceAll(encoded, "; }", ";}"); // normalise spaces css
    encoded = replaceAll(encoded, "<", "%3c");
    encoded = replaceAll(encoded, ">", "%3e");
    encoded = replaceAll(encoded, "\"", "'");
    encoded = replaceAll(encoded, "#", "%23"); // needed for ie and firefox
    encoded = replaceAll(encoded, "{", "%7b");
    encoded = replaceAll(encoded, "}", "%7d");     
    encoded = replaceAll(encoded, "|", "%7c");
    encoded = replaceAll(encoded, "^", "%5e");
    encoded = replaceAll(encoded, "`", "%60"); 
    encoded = replaceAll(encoded, "@", "%40"); 

    // charset reportedly not needed ... I need to test before implementing
    var uri = 'url("data:image/svg+xml;charset=UTF-8,' + encoded + '")';
    var style = 'background-image: ' + uri + ';';

    $("#output").val(style);
    $("body").css( { "background-image": uri } );
    
    // TODO some positionining (preserveAspectRatio)
    $("body").css( { "background-color": $('#background-colour').val() } );
  });

  $('#background-colour').change(function(){
    $("body").css( 
      { "background-color": $('#background-colour').val() } );
  });
});


            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console