Skip to content

How to Change Text Color in HTML

Short answer: use the CSS color property. For a quick change, put it in a style attribute: <p style="color: #1d4ed8;">. To reuse it, give the element a class and set the color in a stylesheet. Any CSS color works: a name like tomato, hex like #FF6347, rgb(255 99 71) or hsl(9 100% 64%). Every example below is live: edit the code and the preview changes.

The quick answer

There are two common ways to set the color: a style attribute on the element itself, or a CSS rule in a <style> block or a stylesheet file. A class is the better habit, because you can change every matching element in one place.

A style attribute and a class
When rules clash: which color wins

When two rules give the same element a color, the more specific one wins: a class beats a tag name, and a style attribute beats both. If two rules are equally specific, the one that comes later wins. That explains most “why won’t my color change?” puzzles.

The examples run in a locked box: scripts are off, nothing is sent anywhere, and your edits go away when you reload. To keep one, copy it into the HTML Editor.

Color names, hex, rgb and hsl

CSS gives you several ways to write the same color. Here’s tomato red written five ways. They all look the same:

One color, five ways
hsl makes shades easy
FormatExampleHow it works
Name tomato, navy, teal 148 named colors. Easy to read, but you can only pick from the list.
Hex #FF6347, #333 Red, green and blue, two digits each from 00 to FF. Three digits is a short form: #333 means #333333. Upper or lower case both work.
rgb() rgb(255 99 71) Red, green and blue from 0 to 255. The older form with commas, rgb(255, 99, 71), works too.
hsl() hsl(9 100% 64%) Hue (0 to 360 around the color wheel: 0 red, 120 green, 240 blue), saturation and lightness. Change the last number to get a lighter or darker shade of the same color.

Not sure which code you need? The HTML Color Picker lets you pick a color and copy it in every format, and lists all 148 color names with a search.

The whole page, headings and currentColor

color is inherited: children take the color of their parent unless you give them their own. So setting it once on body colors all the text on the page, and then you only add rules for the parts that differ, like headings. Links are the exception: browsers give them their own blue, so they don’t inherit.

body for all text, h2 for headings
currentColor for borders and icons

currentColor means “whatever the text color is”. Use it for borders, outlines and inline SVG icons (fill="currentColor"), and they’ll change along with the text. Change #b91c1c in the second example and watch the border and the dot follow.

Color one word

Wrap the word in a <span> with a class, and set the color on that class. If the word is important, not only colorful, use <strong> or <em> instead of a span: they mark the word as important in the HTML itself, not only in how it looks, and you can still color them.

A span, strong and em

💡 Don’t let color be the only clue. About 1 in 12 men see some colors differently, so “the fields in red are required” may not work for everyone. Add a word or a symbol too, like “Required”.

Links have states, and each can get its own color: :link (not visited yet), :visited, :hover (mouse over it), :focus-visible (reached with the keyboard) and :active (while it’s being clicked). Write them in that order, because a later rule wins when two match.

All the link states
A menu without underlines
  • :visited can only change colors. For privacy, browsers ignore other changes there, like size or background images, so pages can’t find out where you’ve been.
  • Keep links easy to spot. If you remove the underline in body text, the link color has to stand out from the text around it. Menus are fine without underlines, because everyone expects them to be links.

Why not <font color>?

<font color="red"> is how text was colored in the 1990s. It has been obsolete since HTML5. Browsers still show it so old pages don’t break, but the HTML Validator flags it, and you’d have to change every tag by hand to change the color. A <span> with a class does the same job, and the color lives in one place.

Old <font> vs a span with CSS

Pick a color people can read

The difference in lightness between text and its background is called contrast, written as a ratio like 4.5:1. The web accessibility guidelines (WCAG) ask for at least 4.5:1 for normal text and 3:1 for large text (24px, or about 19px bold). Light gray and bright colors on white often fall short. Tomato looks great, but on white it only reaches about 2.9:1.

Two grays and tomato on white

Check any pair in the Color Contrast Checker. It shows the ratio, says what passes, and suggests a darker or lighter shade that does. Colors on a colored background are covered in the background color tutorial.

Lighter text: opacity or a see-through color?

To make text paler, you could lower its opacity, but that fades everything in the element: the text, the background, the borders and any child elements. To fade only the text, give the color an alpha value instead: rgb(163 230 53 / 50%), hsl(83 78% 55% / 50%) or 8-digit hex like #a3e63580.

opacity vs an alpha color

One catch: see-through text has less contrast, because the background shows through. For gray text on white, a solid gray like #4b5563 is easier to read and easier to check than black at 60%.

Questions people ask

How do I change the text color in HTML?

Use the CSS color property. Right in the HTML: <p style="color: #1d4ed8;">Blue text</p>. In a stylesheet: give the element a class, like <p class="note">, and add .note { color: #1d4ed8; }. The class is easier to change later, because every element with it updates at once.

How do I change the color of all the text on a page?

Set color on body, for example body { color: #374151; }. Color is inherited, so every element on the page takes that color unless it has its own rule. Links are the exception, because browsers give them their own color. Set a { color: ... } for those.

How do I change the color of one word?

Wrap the word in a span with a class and color the class: The sale ends <span class="hot">tonight</span> with .hot { color: #b91c1c; }. If the word is important, not only colorful, use <strong> or <em> and color that instead.

Which color codes can I use for text?

Any CSS color: a name like tomato or navy (148 of them), hex like #FF6347 or the short #333, rgb(255 99 71), or hsl(9 100% 64%). They are different ways to write the same thing: tomato, #FF6347, rgb(255 99 71) and hsl(9 100% 64%) are all the same red.

How do I change the color of a link?

Set color on a, and on its states if you like: a { color: #1d4ed8; } a:visited { color: #7c3aed; } a:hover { color: #b91c1c; }. Keep the order :link, :visited, :hover, :focus-visible, :active, because a later rule wins when two of them match. :visited can only change colors, for privacy reasons.

Can I still use <font color="red">?

Browsers still show it, but the font tag has been obsolete since HTML5 and validators flag it. Use CSS instead: <span style="color: red;"> for a one-off, or a class with color in your stylesheet.

Why doesn’t my text color change?

Usually another rule wins. A class beats a tag name, an id beats a class, and a style attribute beats both. If two rules are equally specific, the later one wins. Also check for typos in the color code, a missing # before hex, and that links have their own rule, since they don’t inherit the color of their parent.

How do I make text a little see-through?

Give the color an alpha value: rgb(0 0 0 / 60%), hsl(0 0% 0% / 60%) or 8-digit hex like #00000099. That fades only the text. opacity: 0.6 fades the whole element, including its background, borders and children. Keep in mind that faded text has less contrast.

Which text colors are easy to read?

Colors with enough contrast against the background. Aim for at least 4.5:1 for normal text and 3:1 for large text (24px, or about 19px bold). For example, #4b5563 gray on white is about 7.6:1, while light gray #9ca3af is only 2.5:1. A contrast checker tells you the exact ratio.

Keep going

Tools that help