×

How to change text color in HTML

Changing the text colour of a web page is essential because it enhances readability.

There are several ways to change the colour of the text in HTML, which are as follows:

Text color:

This is a very effortless way to define the colour of the text. The colour name refers to the special name in HTML. There are 140 colours that HTML supports.

Example:

This example shows the use of the colour name. We are changing the text colour of both paragraph elements by using the inline style attribute.

<head>

    <title> Example 1 of Text Color </title>

</head>

<body>

  <h1> Environment </h1>

  <p style="color:blueviolet;"> Environment is derived from the French word 'environ' which means 'encircle'. </p>

  <p style="color:maroon;"> Everything that surrounds us is called the environment. </p>

</body>

</html>

Output:

The output shows the result of the above code. The text colour has been changed. The first paragraph is in blue-violet, and the second paragraph is in maroon.

How to change text color in HTML

RGB and RGBA value:

The complete form of RGB is Red, Green, and Blue, and RGBA is Red, Green, and Blue, with alpha indicating opacity. A specific colour is made by mixing red, blue and green.

The range of alpha is from 0 to 1. A decimal value between 0 and 1 can be used to define opacity. The input value of RGB ranges from 0 to 255, where 0 means low intensity and 255 means maximum intensity.

RGB (140, 0, 225) represents the RGB value, where the first value defines red, the second value defines green, and the last value represents blue.

RGBA (145, 55, 245, 0.6) is a representation of an RGB value with an alpha.

Example:

This example demonstrates the use of RGB and RGBA values. We are using the RGB value to change the colour of the text inside the h1 tag. We are using RGBA values inside paragraph tags to change both paragraphs' text colours.

<!DOCTYPE html>
<html>
<head>
    <title> Example 2 of Text Color </title>
    <style>
        h1{
            color: rgb(140, 0, 225);
        }
    </style>
</head>


<body>
  <h1> Components of Environment </h1>
  <p style="color: rgba(255, 99, 71, 0.8);"> Environment have two basic components: </p>
  <p style="color: rgba(240, 10, 50, 0.9);"> 1. Abiotic: These are non-living components. <br>
  2. Biotic: These are living components. </p>
</body>


</html>

Output:

The output shows a change in colour in both the h1 and paragraph elements.

How to change text color in HTML

HSL and HSLA value:

The complete form of HSL is Hue, Saturation and Lightness, and the complete form of HSLA is Hue, Saturation and Lightness with Alpha. The hue's input value is in degrees ranging from 0 to 360. The input values of saturation and lightness are in percentages from 0 to 100%.

hsl(204, 40%, 60%) is the HSL value and hsla(150, 55%, 75%, 0.6) is the HSL value with alpha, where alpha refers to opacity.

Example:

This example shows the use of HSL and HSLA values. We are changing the text colour in a paragraph by using the inline style attribute inside the span tag.

 <!DOCTYPE html>
<html>
<head>
    <title> Example 3 of Text Color </title>
    <style>
        h2{
            color: hsl(204, 40%, 60%);
        }
    </style>
</head>


<body>
  <h2> Examples of Biotic and Abiotic component of environment </h2>
  <p> 1. <span style="color:hsla(250, 79%, 68%, 0.8);"> Abiotic: </span> <span style="color:hsla(250, 45%, 45%);"> Water, air, land, etc. </span> <br>
  2. <span style="color:hsla(250, 79%, 68%, 0.8)"> Biotic: </span> <span style="color:hsla(250, 45%, 45%);"> Human beings, plants, animals, etc.</span> </p>
</body>
</html>

Output:

The output shows the change in colour of text written inside individual span tags.

How to change text color in HTML

Hex value:

The hex value consists of numbers from 0 to 9 and letters from A to F.

#800080 is the hex code, where the first two values represent the intensity of red, the middle two values represent the intensity of green, and the last two values represent the intensity of blue.

Example:

This example shows the use of a hex value to change the colour of the text. We will use the style attribute inside the paragraph, which will have a color property with a hex value.

<!DOCTYPE html>
<html>
<head>
    <title> Example 4 of Text Color </title>
    <style>
        h2{
            color: #f45572;
        }
    </style>
</head>


<body>
  <h2> Color names: </h2>
  <p style="color:#ca8e99 ;"> 1. Red <br>
2. Green <br> 
3. Blue <br> 
4. orange <br> 
5. Pink <br> 
6. Purple </p>
</body>
</html>

Output:

The output shows a change in the colour of the text inside the paragraph.

How to change text color in HTML

Font tags:

This tag was used before HTML5 was released. The syntax of this tag is as follows:

<font color="red"> Some text </font>

It uses the colour attribute to specify a colour. We can specify colours using HSL and HSLA, RGB and RGBA, hex values, and colour names.

HTML5 does not support this tag.

Example:

We are using the <font> tag to display the colour of the text in an HTML document. We are using the colour name, hex value and RGB value of the colour attribute inside the <font> tag to define the colour of the text.

<!DOCTYPE html>
<html>
<head>
    <title> Example 5 of Text Color </title>


</head>


<body>
  <h2> We use a font tag to change the text color. </h2>
  <font color="aquamarine"> Text color using color name value. </font> <br> <br>
  <font color="#800500"> Text color using hex value. </font> <br> <br>
  <font color="rgb(155, 255, 250)"> Text color using rgb value. </font>
</body>


</html>

Output:

The output shows the change in the text colour of the paragraph tag.

How to change text color in HTML

Inline CSS:

We can use the inline element to define the text colour. We use style attribute with colour property inside the element to apply text colour. We can use any colour value like RGB, RGBA, HSL, HSLA, colour name etc. We can change the colour of any text by using the inline style attribute.

Example:

This example shows changing the text colour using inline CSS. We use an inline style attribute with colour property to apply text colour to paragraphs. We are assigning various colour values to the style attribute to change the colour of the text.

<!DOCTYPE html>
<html>
<head>
    <title> Example 6 of Text Color </title>
</head>


<body>
  <h1> Inline CSS </h1>
  <p style="color: rgba(255, 90, 65);"> Changing text color by giving color name to the style attribute. </p>
  <p style="color: rgb(140, 100, 150);"> Changing text color by giving rgb value to the style attribute. </p>
  <p style="color: #505080"> Changing text color by giving hex value to the style attribute. </p>
  <p style="color: hsl(250, 110%, 50%);"> Changing text color by giving hsl value to the style attribute. </p>
</body>


</html>

Output:

The output shows the paragraphs of different colours.

How to change text color in HTML

Embedded CSS:

We can use the style tags and place them inside the head tags. We can define colour property inside the style tag and change the colour of the text of any element.

Example:

We are embedding the CSS using the <style> tag inside the <head> tag. We are changing the text colour of all paragraph elements. We have named a class for each paragraph so that we can easily change the text colour by defining the colour of the paragraph element using the class. We will use the colour name, hex value, RGB value, and HSL value to change the colour of the text.

<!DOCTYPE html>
<html>
<head>
    <title> Example 7 of Text Color </title>
    <style> 
    .text1{
        color: brown;
    }
    .text2{
        color: #a5aa5b;
    }
    .text3{
        color: rgb(167, 114, 114);
    }
    .text4{
        color: hsl(165, 61%, 49%);
    }
    </style>
</head>


<body>
  <h1> Embedded CSS </h1>
  <p class="text1"> Changing the text color of paragraph 1 using color name. </p>
  <p class="text2"> Changing the text color of paragraph 2 using color name. </p>
  <p class="text3"> Changing the text color of paragraph 3 using color name. </p>
  <p class="text4"> Changing the text color of paragraph 4 using color name. </p>
</body>


</html>

Output:

The output shows the different colours in the paragraphs.

How to change text color in HTML

Related Topics

HTML <source> tag

For media attributes, such as <video> tag, <audio> tag, and <picture> tag, we use the <source> tag in html for adding such type of multiple media html elements in our...

4 minutes read.

HTML Basic Tags

Heading Tags: This tag represents the document header. It is said to be a good practice if every document starts with Heading. Heading tags ranges from H1 to H6 i.e....

3 minutes read.

HTML Unordered List

In HTML, we have <ul> tag which defines the unordered list. And it is also known as bulleted list, because as we know it is not ordered list therefore it...

2 minutes read.

HTML Ordered List

We use <ol> tag to make an ordered list in html. An ordered list is made to make a list of items in ordered form, ordered list can be either...

3 minutes read.

Html &lt;datalist >Tag

The autocomplete feature in HTML files is provided by the <datalist> tag. It can be used in conjunction with an input tag to enable users to quickly fill out form...

4 minutes read.

HTML Event Attributes

HTML Event Attributes If a browser responds to the user behavior, an event is called. For example, if we click on the submit button, a box of details will appear on...

4 minutes read.

HTML <summary> tag

When we simply want to show the primary heading or topic name of hidden detailed material to users, then we can achieved this by using the summary tag. This makes it...

2 minutes read.

HTML Nested Table

HTML nested table refers to a table that is created inside another table. The table is in the form of rows and columns. From left to right are called horizontal rows,...

5 minutes read.

HTML Code Tag

The code tag in HTML is a phrase tag used to insert a piece of computer programming code. In HTML there is a separate tag named as <code> tag. Sometimes,...

3 minutes read.

HTML Code

Before jumping into the topic, let’s understand a common scenario where we have a code snippet in our HTML file. By code snippet, we mean computer code, which is written...

5 minutes read.

How to change text color in HTML

Changing the text colour of a web page is essential because it enhances readability. There are several ways to change the colour of the text in HTML, which are as follows: Text...

7 minutes read.

HTML Text Format

Introduction HTML may format text in two different ways. The HTML tags define a text property to make it appear one way or another. A text element may also be formatted...

7 minutes read.

HTML <samp> tag

The Samp tag in HTML is used for defining the sample output from the computer program. This tag is a phrase type tag and a container tag that means the...

2 minutes read.

HTML <u> tag

Underline Element/Tag: The u tag in html is used to specify the important text or phrase by underline. In earlier HTML versions, this element was known as the "Underline" element, and...

5 minutes read.

HTML Layouts

HTML Layouts HTML templates provide a way for well-managed, well-structured responsive web pages to be organized. We could say that the HTML layout specifies how web pages can be set up....

4 minutes read.

Html &lt;Col> Tag

You can write the col tag inside the table tag after the thead, tbody, foot, and tr tags. You can also apply it after the caption tag (in which the...

4 minutes read.

HTML Forms

Forms are required when we want to collect data from the user. Html forms consists of different fields to take user inputs such as: input, text area, checkbox, password, radio...

4 minutes read.

HTML Form Action

The <form> tag has an attribute named action that contains a URL. The HTML action attribute defines the webpage URL on which you want to send the data after submitting...

4 minutes read.

HTML Tutorial

Introduction This HTML tutorial helps beginners and professionals to learn HTML easily. HTML is a widely used web technology for website development. It stands for Hyper Text Markup Language. It is mainly used to...

7 minutes read.

HTML SVG

HTML SVG: SVG stands for Scalable Vector Graphics which defines the graphics for the web. It is a language for describing 2-D graphics in XML. Each drawn shape in SVG is...

1 minute read.