Computer >> Computer tutorials >  >> Programming >> HTML

HTML DOM Style color Property


The HTML DOM Style color property is used for getting or setting the color for the text. The color for the text can be specified in hexadecimal, rgb(), rgba(), hsl(), hsla() or using known keyword.

Following is the syntax for −

Setting the color property −

object.style.color = "color|initial|inherit"

The above properties are explained as follows −

ValueDescription
ColorFor specifying the color value for the text.
InitialFor setting this property to initial value.
InheritTo inherit the parent property value

Let us look at an example for the color property −

Example

<!DOCTYPE html>
<html>
<body>
<h1>Color property example</h1>
<p>This is paragraph 1</p>
<p>This is paragraph 2</p>
<p>This is paragraph 3</p>
<p>This is paragraph 4</p>
<p>This is paragraph 5</p>
<p>Change the above paragraphs color by clicking the below button</p>
<button type="button" onclick="ChangeColor()">Change Text Color</button>
<script>
   function ChangeColor() {
      for(var i=0;i<5;i++){
         if(i%2==0)
            document.getElementsByTagName("P")[i].style.color="blue";
         else
            document.getElementsByTagName("P")[i].style.color="red";
      }
   }
</script>
</body>
</html>

Output

HTML DOM Style color Property

On clicking the “Change Text Color” button −

HTML DOM Style color Property