how to add css using js code example
Example 1: javascript modify css
var el = document.getElementById("elementID");
el.style.css-property = "cssattribute";
el.style.backgroundColor = "blue";
$("#elementID").css("css-property", "css-attribute");
$("#elementID").css("background-color", "blue");
$("#elementID").css({"css-property": "css-attribute", "css-property": "css-attribute"});
$("#elementID").css({
"css-property": "css-attribute",
"css-property": "css-attribute"});
Example 2: how to give css style in javascript
document.getElementById("myH1").style.color = "red";
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: how to change an element in a different elements code css
#container:hover > #cube { background-color: yellow; }
#container:hover + #cube { background-color: yellow; }
#container:hover #cube { background-color: yellow; }
#container:hover ~ #cube { background-color: yellow; }