Set a background color of a specific <div> element: - Javascript CSS Style Property

Javascript examples for CSS Style Property:backgroundColor

Description

Set a background color of a specific <div> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {// w  w w  .ja  v  a  2s . com
    width: 300px;
    height: 300px;
    background-color: coral;
    color: white;
}
</style>
</head>
<body>

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

<div id="myDIV">
  <h1>Hello</h1>
</div>

<script>
function myFunction() {
    document.getElementById("myDIV").style.backgroundColor = "red";
}
</script>

</body>
</html>

Related Tutorials