Javascript DOM CSS Style color Property

Introduction

Set the text-color for elements:

View in separate window

<!DOCTYPE html>
<html>
<body>

<h2 id="myH2">This is an example h2</h2>
<p id="myP">This is an example paragraph.</p>
<p id="myP2">This is also an example paragraph.</p>
<div id="myDiv">This is an example div.</div>
<br>

<button type="button" onclick="myFunction()">Set text color</button>

<script>
function myFunction() {/*from   w  w  w .  j a  v a 2  s .c  o  m*/
  document.getElementById("myH2").style.color = "#ff0000";
  document.getElementById("myP").style.color = "magenta";
  document.getElementById("myP2").style.color = "yellow";
  document.getElementById("myDiv").style.color = "lightblue";
}
</script>

</body>
</html>

The color property sets or gets the color of the text.

Property Values

Value Description
color Sets the color of the text.
initial Sets this property to its default value.
inherit Inherits this property from its parent element.

The color property returns a String representing the text-color of an element.




PreviousNext

Related