[javascript] How do you add CSS with Javascript?

Here's a sample template to help you get started

Requires 0 libraries and uses only javascript to inject both HTML and CSS.

The function was borrowed from the user @Husky above

Useful if you want to run a tampermonkey script and wanted to add a toggle overlay on a website (e.g. a note app for instance)

_x000D_
_x000D_
// INJECTING THE HTML_x000D_
document.querySelector('body').innerHTML += '<div id="injection">Hello World</div>';_x000D_
_x000D_
// CSS INJECTION FUNCTION_x000D_
//https://stackoverflow.com/questions/707565/how-do-you-add-css-with-javascript_x000D_
function insertCss( code ) {_x000D_
    var style = document.createElement('style');_x000D_
    style.type = 'text/css';_x000D_
    if (style.styleSheet) {_x000D_
        // IE_x000D_
        style.styleSheet.cssText = code;_x000D_
    } else {_x000D_
        // Other browsers_x000D_
        style.innerHTML = code;_x000D_
    }_x000D_
    document.getElementsByTagName("head")[0].appendChild( style );_x000D_
}_x000D_
_x000D_
// INJECT THE CSS INTO FUNCTION_x000D_
// Write the css as you normally would... but treat it as strings and concatenate for multilines_x000D_
insertCss(_x000D_
  "#injection {color :red; font-size: 30px;}" +_x000D_
  "body {background-color: lightblue;}"_x000D_
)
_x000D_
_x000D_
_x000D_