Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="svg-bg">
  <label>
    <span class="svg-bg__label">Your SVG: <small>(Type or upload)</small> <div class="svg-bg__button" is="file-upload" v-on:load="loaded">Upload SVG</div></span>
  <textarea class="svg-bg__input" ref="input" v-model="input">

  </textarea>
  </label>
  
  <span class="svg-bg__label">background-image for CSS: 
  <button class="svg-bg__button" v-on:click="copy">Copy Output</button></span>
  <div class="svg-bg__output" ref="output" v-html="output"></div>
</div>

<!-- SVG Background Pattern Adapted from https://github.com/progers/Patterns-Gallery -->

              
            
!

CSS

              
                
.svg-bg {
  border: solid 4px #000;
  background-color: rgba(255,255,255,.7);
  padding: 2em;
  width: 90%;
  max-width: 800px;
  
  > * { display: block; margin-bottom: 2em; }
  :last-child { margin-bottom: 0; }
}

.svg-bg__label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: bold;
  margin-bottom: 0.5em;
}

.svg-bg__button {
  font-weight: 100;
  border: solid 2px #000;
  background: #000;
  color: #FFF;
  padding: 0.5em 0.75em;
  cursor: pointer;
  text-transform: uppercase;
  font-size: 0.75em;
  letter-spacing: 0.1em;
  
  &:hover {
    background: #FFF;
    color: #000;
  }
}

.svg-bg__input,
.svg-bg__output {
  border: 2px solid #000;
  background-color: rgba(255,255,255,.95);
  font-family: monospace;
  padding: 1%;
  word-break: break-all;
  box-sizing: border-box;
}

.svg-bg__input { 
  display: block;
  width: 100%;
  height: 10em;
  box-sizing: border-box;
}

svg { display: none; }
* { box-sizing: border-box; }
html { width: 100%; height: 100%; }
body { min-height: 100%; display: flex; background-position: center center; }
.svg-bg { margin: auto; }


.file-upload__label {
  position: relative;
  overflow: hidden;
  cursor: pointer;
}
.file-upload__input {
  display: block;
  width: 100%;
  height: 100%;
  cursor: pointer;
  opacity: 0;
  position: absolute;
  top: 0;
  left: 0;
}
              
            
!

JS

              
                console.clear();

Vue.component('file-upload',{

  template: `
<div class="file-upload">
  <label class="file-upload__label">
    <slot></slot>
    <input ref="input" @change="loadFiles" accept=".svg" class="file-upload__input" type="file" />
  </label>
  <div class="file-upload__overlay"></div>
</div>
`,
  
  data: ()=>({
    dragging: false,
  }),
  
  // mounted(){
  //   document.addEventListener("dragenter", function(){ fileDrag.className = 'dragenter'; });
  //   document.addEventListener('dragover',function(e){ e.preventDefault(); /* Essential! */ });
  //   document.addEventListener("drop", FileDragDrop);
  //   fileDrag.addEventListener("dragleave", FileDragReset);
  // },
  
  methods:{
    
    
    loadFiles(e){
        e = e || window.event;
        this.dragLeave(e);
        
        var files = Array.from(e.target.files || e.dataTransfer.files || this.$ref.input.files),
            len = files.length,
            i = 0,
            completed = [];
        
        files.forEach((file) => {
          var reader = new FileReader();
          reader.onloadend = (ev)=>{
            this.$emit('load', reader.result, reader, file);
            //completed.push(reader.result);
            //if ( completed.length >= files.length ) { this.$emit('loaded',completed); }
          };
          reader.readAsText(file);
          //reader.readAsDataURL(file);
        });
    },
    
    dragOver(e){ e.preventDefault(); /* Essential */ },
    dragEnter(){ this.dragging = true },
    dragLeave(e){ this.dragging = false; e.preventDefault(); },


  }
});


new Vue({
  el: '.svg-bg',
  data: ()=>({
    input: `<svg xmlns='http://www.w3.org/2000/svg' width='26' height='26' viewBox="0 0 8 8">
  <circle cx="1" cy="1" r="1" opacity='0.25' />
  <circle cx="5" cy="5" r="1" opacity='0.25' />
</svg>`
  }),

  computed: {
    output(){
      let url = this.encodeSVG(this.optimized);
      document.body.style.backgroundImage = url;
      let msg = 'background-image: ' + url + ';' +
          '<br >/* SVG encoded with https://cdpn.io/rrOZQQ */';
      return msg;
    },
    
    optimized(){
      return this.input
        .replace(/\<\?xml.+\?\>/g, '')
        .replace(/(\<\!DOCTYPE(.*?)\>)/g, '') 
        .replace(/([\s\n]+)/g,' ');
    }
  },

  methods: {
    
    loaded(f){
      console.log('file loaded!', typeof f, f);
      this.input = f;
    },

    copy(){  
      var range = document.createRange();  
      range.selectNode(this.$refs.output);  
      window.getSelection().addRange(range);  

      try {  
        // Now that we've selected the anchor text, execute the copy command  
        var successful = document.execCommand('copy');  
        var msg = successful ? 'successful' : 'unsuccessful';  
        console.log('Copy email command was ' + msg);  
      } catch(err) {  
        console.log('Oops, unable to copy');  
      }  

      // Remove the selections - NOTE: Should use
      // removeRange(range) when it is supported  
      window.getSelection().removeAllRanges();  
    },

    encodeSVG(svg) {
      svg = svg + '';
      var b64 = encodeURIComponent(svg.replace(/\<\?xml.+\?\>|\<\!DOCTYPE.+]\>/g, ''))
        .replace(/%20/g," ")
        .replace(/%3D/g,"=")
      // Additional optimizations thanks to https://codepen.io/tigt/post/optimizing-svgs-in-data-uris
        .replace(/%3A/g, ':') // ditto colons
        .replace(/%2F/g, '/') // ditto slashes
        .replace(/%22/g, "'"); // replace quotes with apostrophes (may break certain SVGs)
      return 'url("data:image/svg+xml;charset=utf-8,'+b64+'")';
    }
  }
});
              
            
!
999px

Console