style css javascript code example
Example 1: add style javascript
element.setAttribute("style", "background-color: red;");
element.style.backgroundColor = "red";
Example 2: 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 3: how to give css style in javascript
document.getElementById("myH1").style.color = "red";
Example 4: js add css style to element
var style = document.createElement('style');
style.innerHTML =
'.some-element {' +
'color: purple;' +
'background-color: #e5e5e5;' +
'height: 150px;' +
'}';
var ref = document.querySelector('script');
ref.parentNode.insertBefore(style, ref);
Example 5: html css js
<!DOCTYPE html>
<html lang="pt-br" xml:lang="fr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>...</title>
</head>
<body>
...
<script type="text/javascript" src="script.js"></script>
</body>
</html>
Example 6: change style js
const el = document.querySelector('.para')
el.style.color = 'purple'