Javascript DOM HTML Element style Property set background

Introduction

The style property is used to get or set a specific style of an element using different CSS properties.

To set the style of an element, append a "CSS" property to style and specify a value, like this:

element.style.backgroundColor = "red";?? // set the background color of an element to red

View in separate window

<!DOCTYPE html>
<html>
<body>

<p id="myP">Click the button to give me a red background color.</p>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {// w  w w  . j  av  a 2 s. c o  m
  document.getElementById("myP").style.backgroundColor = "red";
}
</script>

</body>
</html>



PreviousNext

Related