How to Change Text Color in HTML and CSS (5 Easy Ways)

Featured graphic for “How to Change Text Color in HTML and CSS (5 Easy Ways)” showing color swatches and HEX codes

To change text color in HTML, use the CSS color property — for example, <p style="color: #1D4ED8;">Hello</p>. The old HTML <font color> tag is obsolete and shouldn’t be used in modern websites. Below are five practical ways to set text color, from quick inline styles to reusable CSS variables.

Method 1: Inline Style

Add a style attribute directly to an element. It’s quick for one-off changes but hard to maintain on larger sites.

<p style="color: #1D4ED8;">This text is blue.</p>

Method 2: Internal or External CSS With an Element Selector

Style every element of one type at once — for example, all paragraphs or all headings.

<style>
  p  { color: #374151; }
  h1 { color: #111827; }
</style>

On real websites, put this CSS in an external stylesheet (for example style.css) and link it in the <head>.

Method 3: A Class

Classes are the most flexible approach: create a reusable style and apply it wherever you need it.

.text-brand { color: #6C5CE7; }

<p class="text-brand">Highlighted text</p>
<span class="text-brand">Brand word</span>

Method 4: An ID

An ID targets one unique element. It works, but classes are usually preferred because they can be reused.

#intro { color: #0F766E; }

<p id="intro">Welcome!</p>

Method 5: CSS Variables

Define your colors once and reuse them everywhere. Changing a brand color later becomes a one-line edit.

:root {
  --text-main: #111827;
  --text-muted: #6B7280;
  --text-brand: #6C5CE7;
}
body  { color: var(--text-main); }
small { color: var(--text-muted); }
.lead { color: var(--text-brand); }

Color Value Formats You Can Use

FormatExampleNotes
Named colorcolor: navy;About 140 named colors; see HTML Color Codes
HEXcolor: #1D4ED8;Most common format
RGB / RGBAcolor: rgb(29, 78, 216);RGBA adds transparency
HSL / HSLAcolor: hsl(224, 76%, 48%);Easy to make lighter or darker

Find any color and copy its code in the Color Picker, or convert between formats with the Color Converter. For more on HEX in CSS, read How to Use HEX Color Codes in CSS.

Bonus: Change Text Color on Hover

a { color: #1D4ED8; }
a:hover,
a:focus-visible { color: #1E3A8A; }

Including :focus-visible helps keyboard users see the same effect.

Change the color of part of a sentence

<p>Save <span class="text-brand">20% today</span> on all plans.</p>

Make Sure Your Text Is Readable

A color can look great and still be hard to read. WCAG AA requires a contrast ratio of at least 4.5:1 for normal text. For example, against a white background (calculated with the WCAG 2 formula):

  • #1D4ED8 blue: 6.70:1 — passes
  • #DC2626 red: 4.82:1 — passes
  • #6C5CE7 violet: 4.85:1 — passes
  • #FF0000 pure red: 3.99:1 — fails for normal text

Check any combination in the Color Contrast Checker. Also avoid using color as the only signal for meaning — for example, mark errors with text or an icon, not just red.

Common Problems and Fixes

  • The color doesn’t change: another rule may be more specific. Check your browser’s developer tools (right-click → Inspect) to see which rule wins.
  • Links stay blue: links have their own default color. Target a directly.
  • Using <font color>: it is obsolete. Replace it with CSS.
  • Typo in the HEX code: HEX values need a # and 3, 4, 6 or 8 valid characters (0–9, A–F).

Understanding the CSS Cascade and Specificity

When you change text color, you may find that your new color does not appear. This usually happens because another rule with higher priority is overriding it. CSS decides which rule wins using the cascade and specificity.

The order of importance

  1. Inline styles written in the style attribute usually win over stylesheet rules.
  2. ID selectors such as #intro are stronger than classes.
  3. Class, attribute and pseudo-class selectors such as .highlight, [type="text"] and :hover come next.
  4. Element selectors such as p and h1 are the weakest.

When two rules have the same specificity, the one that appears later in the CSS wins. This is why your theme’s stylesheet sometimes overrides your custom CSS if it loads after it.

Example

p { color: #374151; }
.note { color: #2563EB; }
#special { color: #DC2626; }
<p class="note" id="special">Which color am I?</p>

The text will be red, because the ID selector has the highest specificity.

About !important

Adding !important forces a declaration to win, for example color: #DC2626 !important;. It can solve a stubborn problem, but overusing it makes CSS hard to maintain, because the only way to override it is with another !important. Use it as a last resort, and prefer more specific selectors instead.

How Color Inheritance Works

The color property is inherited. If you set a color on a parent element, all child elements use it unless they have their own color rule. That is why setting body { color: #1F2937; } changes almost all the text on a page at once.

<div style="color: #047857;">
  <p>This paragraph is green.</p>
  <span>So is this span.</span>
</div>

Some elements do not inherit color by default in every browser. Links have their own browser styles, and form controls such as inputs and buttons often use system colors. To make them follow the parent color, use the inherit keyword:

a, button, input { color: inherit; }

The currentColor keyword

currentColor reuses the element’s text color for other properties, such as borders and SVG icons:

.tag {
  color: #7C3AED;
  border: 1px solid currentColor;
}

Change the text color once, and the border follows automatically.

Links have several states, and each can have its own color. The order of the rules matters; a helpful memory aid is “LoVe HAte”: link, visited, hover, active.

a:link    { color: #2563EB; }
a:visited { color: #6D28D9; }
a:hover   { color: #1D4ED8; text-decoration: underline; }
a:active  { color: #1E3A8A; }
a:focus-visible { outline: 2px solid #2563EB; outline-offset: 2px; }

Keep links clearly distinguishable from regular text, ideally with an underline, so users do not have to rely on color alone to recognize them.

Coloring Part of a Text

To change the color of just a few words inside a paragraph, wrap them in a span element:

<p>Our sale ends <span class="urgent">tonight at midnight</span>.</p>
.urgent { color: #DC2626; font-weight: 600; }

For semantically important text, consider using strong or em and styling those elements, because they also communicate meaning to screen readers. Use mark for highlighted text; by default it has a yellow background that you can customize:

mark { background: #FEF08A; color: #1F2937; }

The Old font Tag (and Why Not to Use It)

Older tutorials sometimes show the <font color="red"> tag. This element was removed from modern HTML standards years ago. Browsers still display it for backward compatibility, but it mixes content with presentation, is hard to maintain and may cause validation errors. Always use CSS instead.

WordPress block editor

Select a paragraph or heading, open the block settings sidebar and choose a text color under Color. To color only certain words, highlight them and use the Highlight option in the toolbar. For site-wide changes, use the Site Editor’s Styles panel or the Customizer, depending on your theme.

Page builders

Builders such as Elementor provide a text color control in each widget’s Style tab. Using global colors instead of typing HEX codes in each widget keeps the site consistent and lets you update every instance at once.

Email templates

Many email clients ignore style blocks, so inline styles are the safest choice for HTML emails: <p style="color:#1F2937;">. Test emails in several clients, because support varies.

Markdown

Standard Markdown has no syntax for color. If the platform allows HTML, you can use a span with an inline style; otherwise, colored text is not possible.

Text Color and Dark Mode

If your site supports dark mode, define text colors with CSS variables so they can change automatically:

:root { --text: #1F2937; --text-muted: #6B7280; }
@media (prefers-color-scheme: dark) {
  :root { --text: #E5E7EB; --text-muted: #9CA3AF; }
}
body { color: var(--text); }
.muted { color: var(--text-muted); }

Hard-coded colors in inline styles will not switch for dark mode, which is another good reason to keep colors in stylesheets.

Advanced Text Color Effects

Gradient text

.gradient-text {
  background: linear-gradient(90deg, #6366F1, #EC4899);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

Text selection color

::selection { background: #C7D2FE; color: #1E1B4B; }

Placeholder color

input::placeholder { color: #6B7280; }

Caret color

input { caret-color: #2563EB; }

Transitions

a { transition: color .2s ease; }

A smooth transition makes hover color changes feel more polished.

Choosing Good Text Colors

Knowing how to change text color is only half the job; choosing the right colors matters just as much.

  • Body text: use a very dark gray rather than pure black. #1F2937 or #222222 on white feels softer and more modern while still offering excellent contrast.
  • Headings: use your darkest neutral or a deep shade of your brand color, such as navy #1E3A8A, to add personality.
  • Secondary text: captions, dates and helper text can use a medium gray such as #4B5563 or #6B7280. Avoid going lighter than about #767676 on white for normal-sized text.
  • Links: use a clear brand color that contrasts with both the background and surrounding text.
  • Error, warning and success messages: use darker versions of red, amber and green so they remain readable, for example #B91C1C, #B45309 and #15803D.

Limit the number of text colors on a page. Two or three neutrals plus a link color and status colors are usually enough. Too many text colors make a page look disorganized.

Debugging Text Color Problems Step by Step

  1. Inspect the element. Right-click the text and choose Inspect. The Styles panel shows every rule that applies, with overridden rules crossed out.
  2. Find the winning rule. Look for the color declaration that is not crossed out. Note its selector and file.
  3. Check specificity. If your rule is crossed out, make your selector more specific or move your CSS so it loads later.
  4. Look for typos. A missing hash, semicolon or misspelled property name will make the browser ignore your rule.
  5. Clear caches. Browser caches, caching plugins and CDNs can serve old CSS. Clear them and reload.
  6. Check for opacity or filters. A parent with opacity or a filter can change how the color appears.

Changing Text Color With JavaScript

Sometimes text color needs to change in response to user actions, such as validating a form or switching themes. JavaScript can update styles directly:

document.querySelector('.status').style.color = '#15803D';

A cleaner approach is to toggle a class and keep the colors in CSS:

.status.is-error { color: #B91C1C; }
.status.is-success { color: #15803D; }
el.classList.add('is-error');

Keeping colors in CSS means designers can adjust them without touching JavaScript, and dark mode variables continue to work.

Best Practices Summary

  • Use CSS classes and variables rather than inline styles for maintainable code.
  • Set a base text color on the body and override only where needed.
  • Keep links visually distinct and style all their states.
  • Check contrast for every text color you use.
  • Avoid the obsolete font tag and overuse of !important.
  • Plan for dark mode from the start with variables.

Complete Example: A Styled Article

Here is a small but complete example that brings together everything covered above: a base text color, heading color, muted text, links and a highlighted note, all defined with variables.

<style>
  :root {
    --text: #1F2937;
    --heading: #111827;
    --muted: #6B7280;
    --link: #2563EB;
    --note-bg: #EFF6FF;
    --note-text: #1E3A8A;
  }
  body { color: var(--text); }
  h1, h2 { color: var(--heading); }
  .meta { color: var(--muted); }
  a { color: var(--link); }
  .note { background: var(--note-bg); color: var(--note-text); padding: 12px 16px; border-radius: 8px; }
</style>

<h1>Planning Your Garden</h1>
<p class="meta">Published March 3</p>
<p>Spring is the perfect time to plan.</p>
<p class="note">Tip: start seeds indoors six weeks early.</p>

Every color in this example can be changed in one place, and the whole article updates instantly. This is the same structure professional websites use, just on a smaller scale.

Accessibility Reminders for Text Color

Color should never be the only way to communicate meaning. If an error message is shown in red, also include an icon or the word “Error”. If required form fields are marked in a color, also add an asterisk or the word “required”. About one in twelve men has some form of color vision deficiency, and these extra cues help them understand your content.

Also remember that text contrast requirements depend on size. Normal text needs at least 4.5:1 contrast against its background, while large text (around 24px, or about 19px when bold) needs at least 3:1. Light gray text on white is one of the most common accessibility failures on the web, so check secondary text colors carefully. A quick test with a contrast checker takes seconds and can make your content readable for many more visitors.

Which Method Should You Use?

For a one-off change in a quick test, an inline style is fine. For anything on a real website, use a class in an external stylesheet, and store the color values in CSS variables. Use IDs only for unique elements. This combination gives you clean HTML, easy updates and built-in support for themes.

Frequently Asked Questions

How do I change text color in HTML without CSS?

Modern HTML doesn’t provide a non-CSS way. The old <font color> tag is obsolete. Use the CSS color property, even as an inline style attribute.

What is the default text color in HTML?

Browsers usually display text in black and links in blue unless your CSS changes them.

Target the a element: a { color: #0F766E; }. Add a:hover and a:focus-visible for interaction states.

Can I use color names instead of codes?

Yes. CSS supports named colors like teal, navy and tomato, but HEX codes give exact control.

How do I make text semi-transparent?

Use RGBA or HSLA, for example color: rgba(17, 24, 39, 0.7);, and check that it is still readable.

Final Thoughts

The CSS color property is all you need. Use classes or CSS variables for maintainable sites, choose colors that meet contrast requirements, and keep hover and focus states clear. Find the perfect text color in the Color Picker.

Written by the Mage Color Picker Team

We build free online color tools and write practical guides on color theory, color codes and accessible design for designers and developers.

Scroll to Top