How to create Stylish SVG Patterns Background
HTML
<!– Yo, edit me! –>
<!– This SVG will be encoded as a base64 –>
<!– image for cross-browser compatibility –>
<svg xmlns=’http://www.w3.org/2000/svg’ width=’70’ height=’70’>
<rect width=’70’ height=’70’ fill=’#bbd817’/>
<g transform=’rotate(45)’>
<rect width=’99’ height=’25’ fill=’#a9ce00’/>
<rect y=’-50′ width=’99’ height=’25’ fill=’#a9ce00’/>
</g>
</svg>
CSS
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;
}
JS
// 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;