How to Change Text Color in HTML (Hex, RGB & Names)

Quick answer

<p style="color: #2563eb;">This text is blue.</p> <style> h1 { color: darkgreen; } .warning { color: rgb(220, 38, 38); } </style>

Step 1: Choose where to set the color

Text color is set with CSS, which you can put in three places (see how to link CSS to HTML for details):

  • Inline, on one element: <h1 style="color: navy;">. Quick, but you have to repeat it everywhere.
  • A class in a <style> block or stylesheet: .highlight { color: crimson; }, then <p class="highlight">. Reusable.
  • An element selector: p { color: #334155; } colors every paragraph on the page; body { color: … } sets the default for all text.
index.html
Preview

Step 2: Pick a color format

CSS understands several ways of writing the same color. All of these are the same red:

FormatExampleNotes
Namered, navy, tomato148 named colors; easy to read, limited choice
Hex#ff0000 or short #f00The most common format; two digits each for red, green, blue (00–ff)
RGBrgb(255 0 0)Red, green and blue from 0 to 255
RGB + transparencyrgb(255 0 0 / 50%)Half-transparent red; also hex with 8 digits: #ff000080
HSLhsl(0 100% 50%)Hue (0–360°), saturation, lightness: easiest for making lighter/darker shades

Older code writes RGB and HSL with commas, rgb(255, 0, 0) and rgba(255, 0, 0, 0.5). Both styles work in every current browser.

Step 3: Find the exact color you want

You don’t need to work out hex codes by hand. Use a color picker, then copy the value:

  • In Chrome, Edge or Firefox, open DevTools (right-click → Inspect), click any color square in the Styles panel and use the picker or eyedropper.
  • Search “color picker” in Google for a quick picker in the results.
  • Or use an <input type="color"> like the one in the example below, which shows the hex code as you pick.
index.html
Preview

Step 4: Color only part of a sentence

To color a few words without starting a new line, wrap them in a <span> and give it a class. A span has no meaning or style of its own, so it’s made for this. If the words are important, use <strong> (important) or <em> (emphasis) instead and color those; for highlighted text, <mark> adds a highlighter background.

index.html
Preview

Background, link and highlight colors

  • Background: background-color colors the area behind the text. Set text and background colors together so they always contrast.
  • Links: links have their own default blue and purple, so a color on the paragraph doesn’t change them. Style them with a { color: … } (see how to add a link).
  • Selected text: ::selection { background: #c7d2fe; color: #1e1b4b; } changes the highlight when visitors select text.
  • Placeholder text in forms: input::placeholder { color: #64748b; }.
  • currentColor: a keyword meaning “the current text color”, handy for borders and SVG icons that should match the text.

Make sure text is readable: contrast

Colored text must stand out clearly from its background, or many people (including anyone reading a phone in sunlight) can’t read it. The WCAG accessibility guidelines ask for a contrast ratio of at least 4.5 : 1 for normal text and 3 : 1 for large text (about 24px, or 19px bold). Chrome’s DevTools shows the ratio when you click a color in the Styles panel, and free online contrast checkers do the same.

Compare the pairs in the example: the light grey on white fails, the darker grey passes.

index.html
Preview

Also, never use color alone to carry meaning. “Fields in red are required” doesn’t work for colorblind visitors; add a word or symbol, like “Required *”.

Choosing a color palette (and dark mode)

Most professional sites use a small, consistent set of colors: a very dark color for body text (often a dark grey rather than pure black, which can feel harsh), a lighter grey for less important text, and one or two accent colors for links, buttons and highlights. Define them once as CSS custom properties and reuse them everywhere:

:root { --text: #1e293b; --text-muted: #64748b; --accent: #4f46e5; } body { color: var(--text); } a { color: var(--accent); } @media (prefers-color-scheme: dark) { :root { --text: #e2e8f0; --text-muted: #94a3b8; --accent: #a5b4fc; } body { background: #0f172a; } }

Changing a color is then a one-line edit, and the prefers-color-scheme media query switches to light text on a dark background for visitors whose device is set to dark mode. Check the contrast in both modes.

What about <font color>?

You may find old code like <font color="red">. The <font> tag was removed from HTML: browsers still display it so old pages don’t break, but it’s invalid and can’t be changed from a stylesheet. Replace it with a <span> and a CSS class.

Common text color mistakes

  • Low contrast. Light grey on white or colored text on a similar background. Check the ratio.
  • Misspelled color names. colour: blue (British spelling) or color: gray-blue is ignored; check the property and the value.
  • Missing # in hex codes. color: 2563eb doesn’t work; it must be #2563eb.
  • Expecting links to inherit the color. Links keep their own color unless you style a.
  • Too many colors. One or two accent colors plus a dark text color look more professional than a rainbow.
  • Inline styles everywhere. Use classes so you can change a color in one place.

Frequently asked questions

How do I change text color in HTML?
Use the CSS color property, for example <p style="color: blue;"> for one paragraph or p { color: #1e40af; } in a <style> block or stylesheet for every paragraph.
How do I change the color of one word?
Wrap the word in a <span> with a class, such as <span class="brand">word</span>, and give the class a color in CSS: .brand { color: teal; }.
What is a hex color code?
A hex code is a # followed by six characters, two each for red, green and blue, from 00 (none) to ff (full). #000000 is black, #ffffff is white and #ff0000 is red.
How do I change the color of a link?
Style the a element in CSS, for example a { color: #7c3aed; }. Links do not take the color of the surrounding paragraph because browsers give them their own default color.
Why is my text color not changing?
Check the spelling of color and the value, include the # in hex codes, and make sure a more specific rule is not overriding yours. For links, style the a element directly.
What text color contrast is accessible?
WCAG asks for a contrast ratio of at least 4.5:1 between normal text and its background, and 3:1 for large text. Browser DevTools and online contrast checkers show the ratio.