Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to change color of text in HTML

Learning to Use Colors in HTML

Web design involves more than just understanding how to write code. It's about integrating numerous visual elements to create an appealing, user-friendly website. A significant part of this revolves around understanding how to manipulate text color. In this blog, we'll explore how you can change the color of text in HTML.

Understanding HTML and CSS

Before we dive in, it's important to clarify two key terms: HTML and CSS. HTML (HyperText Markup Language) is the standard markup language for creating web pages. It tells the web browser how to structure the web page's content. CSS (Cascading Style Sheets), on the other hand, is a language used for describing the look and formatting of a document written in HTML. In simple terms, HTML is the skeleton of the webpage, while CSS is the skin, hair, and clothes.

The Basics of Text Color in HTML

Originally, HTML was not designed to contain tags for formatting a web page. However, designers quickly realized the need for such features, leading to the development of tags such as <font>. The <font> tag was used to define the font size, color, and face of text. However, this tag is not supported in HTML5. Instead, these formatting tasks are better handled by CSS.

For example, if you wanted to make a paragraph of text red in the old days, you might have written:

<font color="red">This is some text!</font>

Today, the better way to achieve the same result would be:

<p style="color:red;">This is some text!</p>

In the above example, p is the HTML tag for a paragraph, style is an attribute that allows inline CSS, and color:red; is the CSS that changes the color of the text to red.

Using CSS Styles to Change Text Color

There are three main ways to use CSS to change text color in HTML: inline styles, internal style sheets, and external style sheets.

Inline Styles

Inline styles are used to apply CSS to a single HTML element. You simply include the CSS in the style attribute of the HTML tag as shown in the previous example.

<p style="color:blue;">This is a blue text!</p>

While inline styles are straightforward, they're not the most efficient way to handle CSS, especially for larger websites. If you're changing the color of many elements, you'd have to include the CSS in every tag.

Internal Style Sheets

An internal style sheet can be used to apply styles to multiple elements on a single HTML page. Instead of including the CSS in every tag, you include it once in the <head> section of the HTML document using the <style> tag.

<head>
  <style>
    p {
      color: green;
    }
  </style>
</head>
<body>
  <p>This is a green text!</p>
  <p>This is another green text!</p>
</body>

With this method, all the p elements in the web page would be green.

External Style Sheets

External style sheets are the most efficient way to handle CSS for larger, more complex websites. Here, the CSS is placed in a separate file (typically with a .css extension). This file is then linked to the HTML document.

For example, if you have a CSS file named styles.css with the following CSS:

p {
  color: purple;
}

You'd link it to your HTML document like so:

<head>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <p>This is a purple text!</p>
  <p>This is another purple text!</p>
</body>

With this method, all p elements across all the web pages that link to the styles.css file would be purple.

Understanding Color Values

In CSS, colors can be defined in a few different ways: by name, by hexadecimal value, or by RGB (red, green, blue) value.

  • Named colors: CSS has a predefined list of color names you can use, like red, blue, green, etc.
  • Hexadecimal colors: Here, colors are represented by a six-digit hexadecimal (base 16) number: RRGGBB. The two leftmost digits represent the red color, the middle two the green, and the two on the right the blue.
  • RGB colors: With this method, colors are defined using their red, green, and blue components.

Here's how you would use these different methods:

<p style="color:red;">This is a red text!</p>
<p style="color:#FF0000;">This is a red text!</p>
<p style="color:rgb(255, 0, 0);">This is a red text!</p>

All three lines of code produce red text, just with different methods.

Conclusion

In this blog post, we've covered how to change the color of text in HTML using CSS. We've looked at inline styles, internal style sheets, and external style sheets, and we've explored different ways to define colors. Remember, while it's possible to use inline styles for quick, small changes, it's generally better to use external style sheets for larger projects. By separating your HTML (content) from your CSS (appearance), you make your code easier to read and maintain.

Changing text color may seem like a small thing, but mastering it is an important step in becoming a skilled web designer. Happy coding!