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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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>
          )
        </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;
}


            
          
!
            
              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>');
  });

  $("#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
Loading ..................

Console