HTML Colors

Beginner
10 min read
Updated: Nov 2026
6 examples
name / HEX / RGB / HSL

Definition and Usage

HTML colors are CSS color values you apply to text, backgrounds, and borders. Specify them with a color name, a HEX code, RGB/RGBA, or HSL/HSLA.

Where do colors go in HTML?

Colors are not a separate HTML language—they are CSS values. Set them with the style attribute, a <style> block, or an external stylesheet. Prefer CSS over obsolete presentational attributes such as bgcolor.

What can you color?

Common properties: color (text), background-color, and border-color (or the shorthand border).

When should you use each format?

Names are quick for demos. HEX is common in design tools. RGB/RGBA suit channel math and transparency. HSL/HSLA shine when you tweak hue or lightness while keeping a related palette.

Prefer CSS.

Keep color rules in a stylesheet (or a <style> block) so HTML stays about structure. Use inline style for tiny demos only.

140 named colors.

Names like Tomato, DodgerBlue, and MediumSeaGreen work in every modern browser.

Contrast matters.

Light gray on white may look fine on your monitor but fail accessibility checks. Aim for readable contrast.

HTML Color Syntax

Apply a CSS color value through the style attribute (or better, a stylesheet):

color.html
<element style="property: color-value;">
HTML
<!-- Color name -->
<h1 style="background-color:Tomato;">Tomato</h1>

<!-- HEX -->
<h1 style="background-color:#ff6347;">#ff6347</h1>

<!-- RGB -->
<h1 style="background-color:rgb(255, 99, 71);">rgb(255, 99, 71)</h1>

<!-- HSL -->
<h1 style="background-color:hsl(9, 100%, 64%);">hsl(9, 100%, 64%)</h1>

With alpha (transparency): rgba(255, 99, 71, 0.5) and hsla(9, 100%, 64%, 0.5). HEX is written as #RRGGBB (or short #RGB). RGB channels are integers from 0 to 255. HSL uses hue 0–360, saturation and lightness as percentages.

Core HTML Concepts

Formats and where to put them:

FormatExampleNotes
Color nameTomato140 standard names
HEX#ff6347Common in design tools; case-insensitive
RGBrgb(255, 99, 71)Red, green, blue 0–255
RGBArgba(255, 99, 71, 0.5)Adds alpha 0–1 (or %)
HSLhsl(9, 100%, 64%)Hue, saturation, lightness
HSLAhsla(9, 100%, 64%, 0.5)HSL plus alpha

Property vs value

PropertyWhat it colors
colorText (foreground)
background-colorElement background
border-colorBorder (needs a border width/style)

How to Use Colors in HTML

Practical habits for readable, maintainable color.

  1. Pick a primary palette (1–3 brand colors) before styling every element.
  2. Put shared colors in a stylesheet (or CSS variables) instead of repeating inline styles.
  3. Use names for quick prototypes; switch to HEX/RGB/HSL for production precision.
  4. Check text/background contrast for accessibility.
  5. Use RGBA/HSLA when you need translucent overlays—not for hiding important text.
  6. Avoid obsolete HTML attributes like bgcolor and <font color>.
GoalPattern
Quick demostyle="color:Tomato;"
Design-token matchstyle="color:#ff6347;"
Semi-transparent boxbackground-color:rgba(255,99,71,0.5);
Shared themeCSS file + classes (see Add CSS to HTML)

Live Preview

Switch formats for the same Tomato-like color and see the background update.

All four values describe roughly the same tomato red.

Rendered page
Tomato
<div style="background-color:Tomato;">Tomato</div>

HTML Examples

Six patterns: color names, HEX, RGB, HSL, alpha channels, and a full page with preferred CSS.

Example 1 — Color Names

HTML
<h1 style="background-color:Tomato;">Tomato</h1>
<h1 style="background-color:Orange;">Orange</h1>
<h1 style="background-color:DodgerBlue;">DodgerBlue</h1>
<h1 style="background-color:MediumSeaGreen;">MediumSeaGreen</h1>
Try It Yourself

How It Works

  1. background-color accepts a named color.
  2. 140 names are standardized across browsers.
  3. Readable demos — names are easy to remember while learning.
  4. Same property works with HEX, RGB, or HSL later.

Example 2 — HEX Colors

HTML
<p style="background-color:#ff6347;color:#ffffff;padding:8px;">
  Background #ff6347 with white text
</p>
<p style="color:#1e90ff;">Dodger-blue text with HEX</p>
Try It Yourself

How It Works

  1. #RRGGBB encodes red, green, and blue in hex.
  2. #ff6347 matches the Tomato name.
  3. Case does not matter (#FF6347 equals #ff6347).
  4. Pair background and text colors for contrast.

Example 3 — RGB Colors

HTML
<div style="background-color:rgb(255, 99, 71);padding:12px;color:white;">
  rgb(255, 99, 71)
</div>
<div style="background-color:rgb(30, 144, 255);padding:12px;color:white;margin-top:8px;">
  rgb(30, 144, 255)
</div>
Try It Yourself

How It Works

  1. rgb(R, G, B) uses decimals 0–255.
  2. Equal channels (e.g. 128,128,128) make grays.
  3. Matches HEX when you convert channels.
  4. Extend to RGBA when you need transparency.

Example 4 — HSL Colors

HTML
<p style="background-color:hsl(9, 100%, 64%);padding:10px;">Tomato-like HSL</p>
<p style="background-color:hsl(9, 100%, 40%);padding:10px;color:white;">Darker lightness</p>
<p style="background-color:hsl(9, 40%, 64%);padding:10px;">Lower saturation</p>
Try It Yourself

How It Works

  1. Hue is the color wheel angle (0–360).
  2. Saturation controls vividness.
  3. Lightness lightens or darkens without changing hue.
  4. Great for building related shades of one brand color.

Example 5 — RGBA and HSLA

HTML
<div style="background-color:#222;padding:16px;">
  <p style="background-color:rgba(255, 99, 71, 0.5);padding:10px;">
    rgba — 50% transparent Tomato
  </p>
  <p style="background-color:hsla(9, 100%, 64%, 0.5);padding:10px;">
    hsla — same idea with HSL
  </p>
</div>
Try It Yourself

How It Works

  1. Alpha controls opacity of the color layer.
  2. 0 is fully transparent; 1 is opaque.
  3. Background shows through translucent overlays.
  4. Keep text opaque when readability matters.

Example 6 — Complete Page (Prefer CSS)

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>HTML Colors Example</title>
  <style>
    body { font-family: system-ui, sans-serif; margin: 1.5rem; }
    .hero {
      background-color: hsl(9, 100%, 64%);
      color: #fff;
      padding: 1rem 1.25rem;
      border-radius: 0.5rem;
    }
    .note { color: #1e90ff; }
    .card {
      border: 3px solid #ff6347;
      padding: 0.75rem;
      margin-top: 1rem;
    }
  </style>
</head>
<body>
  <div class="hero">
    <h1>Colors with CSS</h1>
    <p>Prefer a stylesheet over scattered inline styles.</p>
  </div>
  <p class="note">This paragraph uses a named-like HEX blue.</p>
  <div class="card">Border color with HEX #ff6347</div>
</body>
</html>
Try It Yourself

How It Works

  1. <style> keeps colors out of every tag.
  2. Classes reuse the same palette.
  3. HSL + HEX can live side by side.
  4. Next step move rules to an external file — see Add CSS to HTML.

Edge Cases & Pitfalls

Color bugs are usually obsolete attributes, low contrast, or invalid values.

obsolete

bgcolor and font color

Do not use presentational HTML attributes. Use CSS background-color and color.

contrast

Low-contrast text

Pale text on pale backgrounds fails accessibility. Test foreground against background.

typo

Invalid color strings

A missing #, wrong RGB range, or misspelled name makes the declaration invalid—styles may fall back.

inline

Too many inline styles

Hard to maintain. Prefer classes in a stylesheet for real pages.

alpha

Transparent text

Low alpha on color can make copy unreadable. Prefer opaque text on translucent backgrounds.

print

Backgrounds in print

Some browsers omit backgrounds when printing unless the user enables them.

Browser Support

CSS color names, HEX, RGB/RGBA, and HSL/HSLA are supported in all modern browsers.

✓ Baseline · Widely available

HTML / CSS colors

Every current browser understands named colors and the common CSS color functions used from HTML.

100% color formats
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
CSS colors in HTML Universal

Bottom line: Use modern CSS color values freely—skip obsolete HTML color attributes.

Key Takeaways

  • Colors are CSS values applied with style sheets or the style attribute.
  • Names, HEX, RGB, HSL (plus alpha variants) all work.
  • Prefer stylesheets for real projects over inline-only styling.
  • Contrast beats fancy color—keep text readable.

One line: Set color / background-color with names, HEX, RGB, or HSL—and keep the rules in CSS when you can.

Frequently Asked Questions

Use a CSS color value on a style property such as color or background-color. Prefer a stylesheet or a style element. You can also use the style attribute for quick demos.
Browsers accept named colors (like Tomato), HEX (#ff6347), RGB/RGBA, and HSL/HSLA. All of these are CSS color values used from HTML.
Modern browsers support 140 standard color names, including Tomato, DodgerBlue, MediumSeaGreen, and Gray.
No. Attributes like bgcolor and the font color attribute are obsolete. Use CSS properties instead: color, background-color, and border-color.
Both describe red, green, and blue. rgb(255, 99, 71) uses decimal 0–255 channels. #ff6347 uses the same channels in hexadecimal. They can represent the same color.
HSL is handy when you want to adjust hue, saturation, or lightness independently—for example lightening a brand color while keeping the same hue.

Practice in the editor

Open a Try It demo and swap name, HEX, RGB, and HSL values.

Open Try It editor →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful