Home > front end >  How to change the color of a div tag in html with javascript code
How to change the color of a div tag in html with javascript code

Time:11-25

How to change the color of a div tag in html with javascript code

How to change the color of a div tag in html with javascript code

CodePudding user response:

<div id="myDiv">
  //your code goes here
</div>
<script>
  document.getElementById("myDiv").style.backgroundColor = "red";
</script>

You have to trigger this is somehow like on body load or on button click

CodePudding user response:

// selecting the html element using id

var div = document.getElementById("apple");
div.style.color = "red";
<div id="apple">
  <p>some text</p>
 </div>

CodePudding user response:

You can use color like this and also take help of this site https://www.w3schools.com/html/html_colors.asp

<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">Lorem ipsum...</p>

  •  Tags:  
  • html
  • Related