Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to change text color in HTML

Getting Started with Changing Text Color in HTML

For the uninitiated, HTML (HyperText Markup Language) is a programming language used to design the structure and presentation of web pages. One of the features it offers is the ability to change text color. If you're just getting the hang of programming, this skill can be an exciting addition to your toolkit.

The process of changing text color in HTML is like using a paintbrush on a canvas. Just as you select different colors for different elements in a painting, HTML allows you to customize the colors of different text elements on your web page.

The Basics: HTML Tags and Attributes

Before we delve into the color-changing process, let's briefly touch upon two essential concepts: HTML tags and attributes. Think of an HTML tag as a command that tells the browser what to do. For example, a <p> tag tells the browser to start a new paragraph.

An attribute, on the other hand, is like an assistant to the tag. It provides additional information about the tag and helps specify how the command should be executed. If we think of the tag as a cook, the attribute would be the recipe, providing specific instructions on how to prepare the dish.

Coloring Text with Inline CSS

Now that we understand HTML tags and attributes, let's dive into the process of changing text color. One of the easiest ways to do this is by using inline CSS (Cascading Style Sheets). CSS is like the wardrobe of a web page, controlling its visual presentation.

Here is a simple example:

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

In this example, <p> is the tag, style is the attribute, and color:blue; is the value of the attribute. This value tells the browser to color the text blue.

Choosing Your Colors

In the above example, we used a simple color name (blue). However, HTML supports a wide variety of colors. You can use precise color names ("blue", "red", "yellow", etc.), hexadecimal color codes (e.g., "#FF0000" for red), or RGB values (e.g., "rgb(255,0,0)" for red).

Consider the following examples:

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

All three lines of code will display the same red text.

Adding Some Style with CSS Classes

While inline CSS is simple and straightforward, it can become tedious if you want to apply the same style to multiple elements. Imagine needing to paint multiple parts of your canvas the same color. It would be much simpler to define the color once and then apply it wherever needed. This is where CSS classes come into play.

You can define a CSS class in a <style> element within the <head> section of your HTML document. Once defined, you can apply this class to any text element in your document.

Here's an example:

<head>
<style>
.blue-text {
    color: blue;
}
</style>
</head>

<body>
<p class="blue-text">This is a blue text.</p>
<p class="blue-text">This is another blue text.</p>
</body>

In this example, we've created a CSS class named blue-text that changes the text color to blue. We've then applied this class to two <p> elements, resulting in two lines of blue text.

Conclusion: The Power of HTML and CSS

Changing text color in HTML is like using a paintbrush on a canvas. With the right commands (HTML tags and attributes) and the right colors (CSS styles), you can create a vibrant and colorful web page. Whether you're using inline styles or CSS classes, the process is simple and straightforward. Keep practicing, and before long, you'll be painting the web with your colors. Happy coding!