Skip to content

Instantly share code, notes, and snippets.

@azzcatdesign
Created April 9, 2013 19:49
Show Gist options
  • Save azzcatdesign/5348790 to your computer and use it in GitHub Desktop.
Save azzcatdesign/5348790 to your computer and use it in GitHub Desktop.
A CodePen by Catherine Azzarello. SVG background image demo - Rebuilding old site with image background. I adjusted the white/gray texture to match client's colors and use on new site.
<!-- Yo, edit me! -->
<!-- This SVG will be encoded as a base64 -->
<!-- image for cross-browser compatibility -->
<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='6' height='6'>
<rect width='6' height='6' fill='#ded8cc'/>
<g id='c'>
<rect width='3' height='3' fill='#bfab91'/>
<rect y='1' width='3' height='2' fill='#eee'/>
</g>
<use xlink:href='#c' x='3' y='3'/>
</svg>
<!-- This is used for output -->
<div id="demo"><pre id="code">
[base64]
</pre></div>
// Short script to encode our SVG in base64
// This can be reversed using window.atob('base64')
var code = document.getElementById('code');
var demo = document.getElementById('demo');
var svg = document.getElementsByTagName('svg')[0];
// Convert the SVG node to HTML.
var div = document.createElement('div');
div.appendChild(svg.cloneNode(true));
// Encode the SVG as base64
var b64 = 'data:image/svg+xml;base64,'+window.btoa(div.innerHTML);
var url = 'url("' + b64 + '")';
code.innerHTML = 'Base64 CSS (for cross-browser compatibility)\n\nbackground-image: ' + url + ';';
demo.style.backgroundImage = url;
html {
height: 100%; width: 100%;
}
#demo {
position: absolute;
top: 0px; left: 0px;
right: 0px; bottom: 0px;
}
#code {
border: 1px solid #000;
background-color: rgba(255,255,255,0.95);
padding: 0.5em;
margin: 1em;
}
svg {
display: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment