xxxxxxxxxx
element.style["name_of_css_property"]="property_value";
// example
my_text_input.style["text-transform"]="capitalize";
xxxxxxxxxx
// Bad:
element.setAttribute("style", "background-color: red;");
// Good:
element.style.backgroundColor = "red";
xxxxxxxxxx
<html>
<body>
<p id="p2">Hello World!</p>
<script>
document.getElementById("p2").style.color = "blue";
</script>
<p>The paragraph above was changed by a script.</p>
</body>
</html>
xxxxxxxxxx
var elem = document.querySelector('#some-element');
// Set color to purple
elem.style.color = 'purple';
// Set the background color to a light gray
elem.style.backgroundColor = '#e5e5e5';
// Set the height to 150px
elem.style.height = '150px';
xxxxxxxxxx
// Add style rule to an element without altering other styles.
element.style.backgroundColor = 'red'
xxxxxxxxxx
to get a property value of an element in js :
html:
<div class="element" style="color: red;">Red hot chili pepper!</div>
js:
const your_element = document.querySelector('.element')
const color_of_my_element = your_element.style.color
console.log(color_of_my_element) // red
xxxxxxxxxx
var style = document.createElement('style');
style.innerHTML = `
#target {
color: blueviolet;
}
`;
document.head.appendChild(style);
xxxxxxxxxx
to get a property value of an element in js :
html:
<div class="element" style="color: red;">Red hot chili pepper!</div>
js:
const your_element = document.querySelector('.element')
const color_of_my_element = your_element.style.color
console.log(color_of_my_element) // red
xxxxxxxxxx
let overlayDiv = document.createElement('DIV');
overlayDiv.style.backgroundColor = "red";
xxxxxxxxxx
to get a property value of an element in js :
html:
<div class="element" style="color: red;">Red hot chili pepper!</div>
js:
const your_element = document.querySelector('.element')
const color_of_my_element = your_element.style.color
console.log(color_of_my_element) // red
xxxxxxxxxx
document.head.insertAdjacentHTML("beforeend", `<style>body{background:red}</style>`)