HTML color Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 6 Examples
Layout & Styling

Introduction

The color attribute in HTML is used to specify the text color inside various legacy HTML elements. By setting the color attribute, developers can customize the appearance of text to enhance the visual design of a web page.

What You’ll Learn

01

Color Names

red, blue, green.

02

Hex Codes

#FF0000, #00FF00.

03

font Element

Where color applies.

04

CSS Alternative

CSS color property.

05

JavaScript

Dynamic text colors.

06

Deprecated

Use CSS instead.

Purpose of color

The primary purpose of the color attribute is to define the color of text content within an HTML element. This allows for a more personalized and aesthetically pleasing presentation of textual information on a webpage.

⚠️
Deprecated in HTML5

The HTML color attribute is not recommended for new projects. Use the CSS color property instead for better control, accessibility, and maintainability.

📝 Syntax

Add color to a supported legacy element with a color name or hex code:

color.html
<font color="red">Red text</font>
<font color="#0000FF">Blue text</font>

Syntax Rules

  • Value is a color name (e.g. red) or hex code (e.g. #FF0000).
  • Valid on legacy elements such as <font> and <basefont>.
  • Not valid on generic elements like <p> or <div>.
  • For rgb() and hsl() colors, use CSS color instead.

💎 Values

The color attribute accepts values that represent different colors. These values can be specified using various methods, including:

  • Color Names — Common color names like red, green, or blue can be used.
  • Hexadecimal Notation — Hex codes such as #RRGGBB provide a wide range of color options.
  • RGB Values — Using the RGB format with syntax rgb(red, green, blue) is best applied with CSS color, not the legacy HTML attribute.
  • HSL Values — The HSL format hsl(hue, saturation, lightness) allows specifying colors based on hue, saturation, and lightness via CSS.
color-values.html
<font color="red">Named color</font>
<font color="#00FF00">Hex color</font>
<p style="color: rgb(0, 0, 255);">RGB via CSS</p>
<p style="color: hsl(120, 100%, 50%);">HSL via CSS</p>

⚡ Quick Reference

FormatExampleCSS Alternative
Color namecolor="red"color: red
Hex codecolor="#FF0000"color: #FF0000
RGBUse CSS insteadcolor: rgb(255, 0, 0)
HSLUse CSS insteadcolor: hsl(0, 100%, 50%)
On font<font color>Primary legacy use
HTML5 statusDeprecatedUse CSS for new code

Applicable Elements

ElementSupported?Notes
<font>Yes (legacy)Primary use for text color
<basefont>Yes (legacy)Default text color for document
<body>Yes (legacy)Page text color in HTML4
<p>, <div>NoUse CSS color
<input type="color">NoDifferent feature — color picker input

Examples Gallery

Color names, hex codes, RGB and HSL via CSS, a basic example, and dynamic JavaScript color changes.

👀 Live Preview

Text colored with the HTML color attribute on <font> elements:

Red text · Blue hex text · Green text

Color Names

Common color names like red, green, or blue can be used:

color-names.html
<font color="red">This text is in red color.</font>
Try It Yourself

Hexadecimal Notation

Hex codes such as #RRGGBB provide a wide range of color options:

hexadecimal-notation.html
<font color="#00ff00">This text is in green color.</font>
Try It Yourself

RGB Values

Using the RGB format, you can define colors with the syntax rgb(red, green, blue) via CSS:

rgb-values.html
<p style="color: rgb(0, 0, 255);">This text is in blue color.</p>
Try It Yourself

HSL Values

The HSL format (hsl(hue, saturation, lightness)) allows for specifying colors based on their hue, saturation, and lightness:

hsl-values.html
<p style="color: hsl(120, 100%, 50%);">This text is in green color using HSL.</p>
Try It Yourself

Example

Let’s see how the color attribute can be applied to an HTML element:

color.html
<font color="blue">This text is in blue color.</font>
Try It Yourself

How It Works

In this example, the color attribute is set to blue, changing the color of the text inside the <font> element.

Dynamic Values with JavaScript

You can dynamically set the color attribute using JavaScript, allowing for interactive and dynamic color changes. Here’s a brief example:

dynamic-color.html
<font id="dynamicText">This text will turn red using JavaScript.</font>

<script>
  // Dynamically set color for an element
  document.getElementById("dynamicText").setAttribute("color", "#ff0000");
  // Modern alternative:
  // document.getElementById("dynamicText").style.color = "#ff0000";
</script>
Try It Yourself

How It Works

In this script, the color attribute is set to red (#ff0000) for an element with the id dynamicText. This dynamic approach can be beneficial when responding to user actions or other events. For new code, prefer style.color.

♿ Accessibility

  • Contrast matters — Ensure sufficient contrast between text color and background color for readability.
  • Do not rely on color alone — Pair color cues with text labels or icons for color-blind users.
  • Avoid low-contrast combinations — Light gray text on white backgrounds fails WCAG contrast guidelines.
  • Use CSS for theming — CSS classes make it easier to support high-contrast and dark modes.
  • Test with screen readers — Text color alone does not convey meaning to assistive technology.

🧠 How color Works

1

Author sets color attribute

Add a color name or hex value to a supported legacy element.

Markup
2

Browser parses color

The user agent resolves the color value to a display color.

Parse
3

Text is painted

The element’s text renders in the chosen foreground color.

Render
=

Colored text displayed

In modern projects, CSS color achieves the same result with more flexibility.

Browser Support

The HTML color attribute is deprecated in HTML5 but still rendered by browsers on legacy elements like <font>.

⚠️ HTML4 legacy · Deprecated

Still renders in browsers

Use CSS color for all new text styling.

Legacy Rendering support
Google Chrome Legacy rendering
Deprecated
Mozilla Firefox Legacy rendering
Deprecated
Apple Safari Legacy rendering
Deprecated
Microsoft Edge Legacy rendering
Deprecated
color attribute Deprecated

Bottom line: Browsers still paint the HTML color attribute on font elements, but CSS is the standard for new code.

💡 Best Practices

✅ Do

  • Use CSS color for all new projects
  • Ensure strong contrast between text and background
  • Experiment with color combinations for readability
  • Use hex codes for precise brand colors
  • Keep styling in stylesheets for maintainability

❌ Don’t

  • Use the HTML color attribute on <p> or <div>
  • Confuse the HTML attribute with CSS color
  • Use clashing colors that reduce readability
  • Depend on text color alone to convey meaning
  • Rely on deprecated <font> tags for layout
  • Experiment with different color combinations to find the most visually appealing and readable text.
  • Ensure there is enough contrast between text color and background color for readability and accessibility.
  • Consider using CSS stylesheets for a more organized and maintainable approach to styling.

Conclusion

The color attribute is a valuable tool for customizing the appearance of text in HTML, especially in legacy markup. By using different color values and techniques, you can create visually engaging and well-designed web pages.

For modern development, reach for CSS color instead. It works on any element, supports rgb(), hsl(), and custom properties, and keeps styling separate from structure.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about color

Bookmark these before coloring your HTML text.

5
Core concepts
📄 02

font Element

Legacy scope.

Scope
💻 03

CSS Wins

color property.

Modern
⚙️ 04

Dynamic JS

setAttribute.

Script
05

Contrast

Readability.

A11y

❓ Frequently Asked Questions

It sets the foreground text color on legacy elements such as <font> and <basefont>.
No. The HTML attribute is deprecated. CSS color is the modern property used in stylesheets and inline styles.
No. Use CSS color on <p>, <div>, and other modern elements.
Not reliably. Use color names or hex in the HTML attribute, or apply rgb() via CSS color.
Use element.style.color = "#ff0000" for modern code, or setAttribute("color", "#ff0000") on legacy font elements.
The CSS color property in a stylesheet or inline style attribute.

Style HTML text the modern way

Practice the legacy color attribute and compare it with CSS color in the Try It editor.

Try basic color example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful