Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to change the color of text in HTML

Understanding the Basics

As you embark on your journey to learn programming, one of the most common tasks you'll come across is changing the color of text in HTML. HTML, short for HyperText Markup Language, is the standard language for creating web pages. In this blog post, you will learn a simple and effective way to change the color of text in HTML.

Consider HTML as a painting canvas. You can create beautiful paintings (websites) with the right tools (HTML tags). Just like in a painting, the color plays a significant role in HTML. The color of the text can make your website more attractive and easy to read. Let's dive in and learn how to change the text color in HTML.

Changing Text Color using Inline CSS

CSS, short for Cascading Style Sheets, is a language used to describe the look and formatting of a document written in HTML. Think of CSS as the paint for our canvas (HTML). We'll be using inline CSS, which means we'll be applying our CSS directly within our HTML tags.

Here's a simple example of how you can change the text color with inline CSS:

<p style="color:red;">This is a paragraph.</p>

In the example above, <p> is an HTML tag that we use to create a paragraph. The style attribute inside the <p> tag is used to add CSS properties. color:red; is a CSS property which changes the color of the text to red.

You can replace 'red' with any color you like. Try 'blue', 'green', 'yellow', and so on. You can also use hexadecimal color codes like #FF5733 for a more specific shade.

Adjusting Text Color using Internal CSS

While inline CSS is a handy method, it can be cumbersome if you have a lot of text and you want to apply the same style to all of them. This is where internal CSS comes in.

With internal CSS, you can define styles for multiple elements at once in the <head> section of the HTML document. Let's see how it works:

   p {
     color: blue;
   <p>This is a blue paragraph.</p>
   <p>This is another blue paragraph.</p>

In this example, we define a style for the <p> tag inside the <style> tag in the <head> section. Every <p> tag in the body of our HTML document will now be blue.

Utilizing External CSS

If you're working with a larger project with multiple HTML files, you'll want to use external CSS. With external CSS, you can change the color of text across multiple HTML files all at once.

To use external CSS, you'll need to create a separate .css file. Here's an example of what that might look like:

In your CSS file (let's call it "styles.css"):

p {
  color: purple;

And in your HTML file:

  <link rel="stylesheet" type="text/css" href="styles.css">
  <p>This is a purple paragraph.</p>
  <p>This is another purple paragraph.</p>

In the HTML file above, we link to our external CSS file using the <link> tag in the <head> section. Now, all paragraphs in this HTML file will be purple, and you can apply this CSS to as many HTML files as you want by using the same <link> tag.

Final Thoughts

Changing the color of text in HTML can seem like a small task, but it's a fundamental skill that you'll use frequently as you continue to learn and grow as a programmer. As you've seen, there are a few different methods to accomplish this task, each with its own use case and advantages.

Just remember the painting analogy: HTML is your canvas, and CSS is your paint. With practice, you'll be creating beautiful, colorful websites in no time. Happy coding!