The three ways to set text color in HTML

You change text color in HTML using the color property in CSS. HTML itself has no color attribute — you must write CSS either in a style attribute on the tag, in a <style> block in your page head, or in a separate CSS file. The simplest method for a single word or phrase is the inline style attribute. For a whole page or section, a style block or external file keeps your code cleaner.

The color property accepts color names, hex codes, RGB values, and HSL values. Color names like red, blue, and green work in most browsers. Hex codes like #FF5733 give you precise control over millions of colors. RGB and HSL are alternatives that some developers prefer, but hex and color names cover nearly all real-world use.

Key Takeaways

  • Add style="color: red;" directly to any HTML tag to change that text's color without touching CSS.
  • Wrap multiple tags in a <style> block in your page head to set colors for whole sections or all paragraphs at once.
  • Hex codes like #FF5733 let you pick from 16 million colors, while color names like red and blue work instantly.
  • The color property changes text only — use background-color to change the background behind text instead.

Using inline styles for a single element

The fastest way to color one piece of text is to add a style attribute directly to the tag. Open the tag, type style="color: ", then add your color choice and close with a semicolon and quote.

Here are three examples:

  • <p style="color: red;">This text is red.</p>
  • <h2 style="color: #0066CC;">This heading is blue.</h2>
  • <span style="color: rgb(255, 165, 0);">This text is orange.</span>

Inline styles work on any tag — paragraphs, headings, spans, links, list items, and more. The color applies only to that one tag, so if you want ten paragraphs to be blue, you must add the style to each one. For that reason, inline styles are best for one-off changes.

Using a style block for multiple elements

When you want to color many tags the same way, write a <style> block in the <head> section of your page. Inside the style block, write a CSS rule that targets the tags you want to change.

Here is a complete example:

<!DOCTYPE html> <html> <head> <style> p { color: navy; } h2 { color: #FF6B6B; } .highlight { color: green; } </style> </head> <body> <p>All paragraphs are navy.</p> <p>This one too.</p> <h2>All headings are red.</h2> <p class="highlight">This paragraph is green.</p> </body> </html>

The rule p { color: navy; } colors every <p> tag on the page. The rule .highlight { color: green; } colors only tags with class="highlight". This approach scales — change the color once in the style block and it updates everywhere that rule applies.

Color formats: names, hex, RGB, and HSL

HTML and CSS accept four ways to write a color. Color names are the simplest: red, blue, green, purple, orange, navy, teal, and 140 others. They work instantly and require no memorization of codes, but they cover only basic colors.

Hex codes start with a hash and six characters: #FF5733. The first two characters control red, the next two control green, and the last two control blue. Each pair ranges from 00 (none of that color) to FF (full intensity). Hex codes let you pick from 16 million colors and are the industry standard. If you do not know the hex code for a color you want, search "color picker" in your browser to find a tool that shows you the code for any color you click.

RGB uses three numbers from 0 to 255: rgb(255, 165, 0) for orange. HSL uses hue (0–360), saturation (0–100%), and lightness (0–100%): hsl(39, 100%, 50%) for orange. Both work, but hex and color names are more common in practice.

Changing text color in links and buttons

Links and buttons are tags too, so you color them the same way. For a link, add style to the <a> tag:

<a href="page.html" style="color: purple;">Click here</a>

By default, browsers show links in blue and visited links in purple. If you change the color, users may not recognize it as a link. For that reason, keep link colors recognizable — purple, blue, or another dark color — and avoid changing them unless you have a strong reason.

For buttons, the process is the same:

<button style="color: white;">Submit</button>

This changes the text color inside the button. To change the button's background color, use background-color instead. Most buttons need both — dark background with light text, or light background with dark text — so they are readable.

Common mistakes and how to fix them

The most common mistake is forgetting the colon or semicolon. style="color red" will not work — it must be style="color: red;". The colon separates the property name from the value, and the semicolon ends the declaration.

Another mistake is misspelling the color name. color: redd; will not work because redd is not a valid color. If a color does not work, check the spelling or switch to a hex code instead.

A third mistake is confusing color with background-color. color changes the text itself. background-color changes the background behind the text. If your text is invisible, it may be because the text color and background color are too similar — try changing one or the other.

Using external CSS files for larger projects

For a website with many pages, keep your CSS in a separate file instead of writing it in every page's head. Create a file called styles.css and write your color rules there:

p { color: navy; } h2 { color: #FF6B6B; } .highlight { color: green; }

Then link to that file in the head of each HTML page:

<link rel="stylesheet" href="styles.css">

Now every page that links to styles.css uses the same colors. If you need to change a color, edit it once in the CSS file and it updates everywhere. This method is standard for real websites and keeps your code organized as your project grows.

Frequently Asked Questions

Can I use color names I make up, or do they have to be standard?

You must use standard color names that browsers recognize. Browsers know about 140 named colors like red, blue, coral, and darkslategray, but they will not recognize made-up names like mycolor. If you want a custom color, use a hex code instead.

What is the difference between color and text-color?

CSS uses color, not text-color. If you write text-color: red;, it will not work. The property is always color by itself.

How do I make text transparent or invisible?

Set color: transparent; to make text invisible while keeping the space it occupies. You can also use color: rgba(255, 0, 0, 0.5); where the last number (0.5) controls transparency — 0 is fully transparent, 1 is fully opaque.

Does the color property work on all HTML tags?

Yes, color works on any tag that contains text — paragraphs, headings, spans, links, list items, table cells, and more. It does not work on tags that do not display text, like <img> or <video>.