add css to js code example
Example 1: how to give css style in javascript
document.getElementById("myH1").style.color = "red";
Example 2: add css in javascript
document.getElementById("demo").style.display = "none";
Example 3: add css style sheet with javascript
<head>
<script>
function myFunction() {
var x = document.createElement("LINK");
x.setAttribute("rel", "stylesheet");
x.setAttribute("type", "text/css");
x.setAttribute("href", "styles.css");
document.head.appendChild(x);
}
myFunction()
</script>
</head>
Example 4: append css file with javascript
var cssFile = document.createElement('link');
cssLink1.rel = 'stylesheet';
cssLink1.href = "styles.css"; // or path for file {themes('/styles/mobile.css')}
document.head.appendChild(cssFile); // append css to head element
Example 5: js add css to document
document.querySelector('head').innerHTML += '<link rel="stylesheet" href="styles.css" type="text/css"/>';