Create a style tag with javascript

Posted on October 24, 2021

If you want to create programmatically a style tag with javascript, here the tip:

const style = document.createElement('style')
style.type = 'text/css'
style.append(myCSS)
document.head.appendChild(style)

where myCSS is your CSS string you want to inject, such as:

const myCSS = `p{
  color: blue;
}`