Document.createElement()
to create a new style
element and set its type to text/css
.Element.innerText
to set the value to the given CSS string.Document.head
and Element.appendChild()
to append the new element to the document head.style
element.const injectCSS = css => {
let el = document.createElement('style');
el.type = 'text/css';
el.innerText = css;
document.head.appendChild(el);
return el;
};
injectCSS('body { background-color: #000 }');
// '<style type="text/css">body { background-color: #000 }</style>'
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️