Changing Text Color in HTML – Sample code

The old HTML way to change text color was to use the <font> tag. However, in the HTML5 era where HTML is for structure and CSS is for styling, the right way is to use CSS to set the text color.

The Right way to change text color

Use CSS to define a class for that style color of text in the stylesheet. Like this:

.highlighted
{
   color:red;
}

and then use that class in the HTML code:

Knowing <span class="highlighted">the components</span> of cells 
and how cells work is fundamental 
to <span class="highlighted">all biological sciences</span>. 

This results in the text being colored like this:
html text color sample

Here is an illustration how it works together:
html text color illustration

Inline style

An (not so elegant but working) alternative way to change text color in HTML is to use inline CSS in the HTML tag for example:

Knowing <span style="color:red">the components</span> of cells 
and how cells work is fundamental 
to <span style="color:red">all biological sciences</span>.

Advantage of using CSS to set the text color

The advantage of using CSS to apply the text color can be seen from the example above. When you want to make changes to the style (say, changing from red to dark red) you have to find every place where you used the inline style and make the change or your website will have inconsistent styling.

When you are using a CSS sheet, just make change to the CSS style and it gets applied everywhere.

Changing text color of entire paragraph

CSS

.quote
{
  color:#888;
}

HTML

Vivamus at lacus condimentum, hendrerit nunc quis, semper elit. 
Maecenas vehicula augue lorem, et pretium sem ullamcorper id. 
Sed rhoncus, diam nec feugiat pellentesque, leo felis eleifend nisl, 
et egestas velit libero at quam. Aliquam non eleifend dolor. 
<p class="quote">
Donec varius mollis arcu maximus efficitur. 
Vestibulum ante ipsum primis in faucibus orci luctus et 
ultrices posuere cubilia Curae; In pellentesque sem et velit 
suscipit interdum nec tristique ante.
</p>

which displays like this:
html text color paragraph

Close

Copy and paste this code to display the image on your site

Copied!