Skip to content

Instantly share code, notes, and snippets.

@thybzi
Last active October 5, 2016 07:52
Show Gist options
  • Save thybzi/86897b0f8402da3dbce4fe1649d3e04a to your computer and use it in GitHub Desktop.
Save thybzi/86897b0f8402da3dbce4fe1649d3e04a to your computer and use it in GitHub Desktop.
function createStyle(data, useImportant, w) {
var styleElement, styleContent, selector, property, value;
w = w || window;
styleContent = '';
for (selector in data) {
if (!data.hasOwnProperty(selector)) continue;
styleContent += '\n' + selector + ' {\n';
for (property in data[selector]) {
if (!data[selector].hasOwnProperty(property)) continue;
value = data[selector][property];
styleContent += ' ' + property + ': ' + value;
if (useImportant) {
styleContent += ' !important';
}
styleContent += ';\n'
}
styleContent += ' }\n';
}
styleElement = w.document.createElement('style');
styleElement.textContent = styleContent;
w.document.querySelector('head').appendChild(styleElement);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment