TExcelTAD
Home › HTML › HTML Colors

HTML Colors

Color is one of the quickest ways to give a page personality. In HTML you do not set colors with tags. You set them with CSS properties, usually inside a style attribute or a stylesheet. The three properties you will use most are:

Property What it colors
color The text (foreground)
background-color The area behind the element
border / border-color The element's outline

Color names

Browsers understand about 140 named colors, such as tomato, steelblue, gold, seagreen and slategray. Names are easy to read, which makes them great for learning and quick experiments.

html · try it yourself
<h2 style="background-color: steelblue; color: white;">Steel blue heading</h2>
<p style="color: tomato;">This paragraph uses tomato-colored text.</p>
<p style="border: 3px solid seagreen; padding: 8px;">A paragraph with a sea green border.</p>
<p style="background-color: gold;">Gold background, default text color.</p>

Tip: Color names are case-insensitive: Tomato, TOMATO and tomato all work, but lowercase is the usual convention.

Ways to write a color

Named colors are limited, so CSS gives you several numeric formats that can describe millions of colors.

Format Example Notes
Name tomato Readable, limited choice
Hex #ff6347 Two hex digits each for red, green, blue
Short hex #f63 Each digit is doubled: #ff6633
RGB rgb(255, 99, 71) Each channel from 0 to 255
RGBA rgba(255, 99, 71, 0.5) Adds alpha (opacity) from 0 to 1
HSL hsl(9, 100%, 64%) Hue (0–360°), saturation, lightness
HSLA hsla(9, 100%, 64%, 0.5) HSL plus alpha

All of the following boxes are the same shade of tomato, written five different ways:

html · try it yourself
<style>
  div { padding: 10px; margin: 4px 0; color: white; font-family: sans-serif; }
</style>
<div style="background-color: tomato;">tomato</div>
<div style="background-color: #ff6347;">#ff6347</div>
<div style="background-color: rgb(255, 99, 71);">rgb(255, 99, 71)</div>
<div style="background-color: hsl(9, 100%, 64%);">hsl(9, 100%, 64%)</div>
<div style="background-color: rgba(255, 99, 71, 0.5); color: black;">rgba(255, 99, 71, 0.5) — half transparent</div>

Understanding RGB and hex

Screens mix light from three channels: red, green and blue. Each channel has an intensity from 0 (off) to 255 (full).

  • rgb(0, 0, 0) is black — every channel is off.
  • rgb(255, 255, 255) is white — every channel is at full strength.
  • rgb(255, 0, 0) is pure red.
  • Equal values, such as rgb(128, 128, 128), give a shade of gray.

A hex color is the same idea written in base 16. #RRGGBB uses two digits per channel, where 00 means 0 and ff means 255. So #ff0000 is red, #00ff00 is green and #0000ff is blue.

Understanding HSL

HSL describes color the way people tend to think about it:

  • Hue is a position on the color wheel, in degrees: 0 is red, 120 is green, 240 is blue.
  • Saturation goes from 0% (gray) to 100% (full color).
  • Lightness goes from 0% (black) through 50% (normal) to 100% (white).

HSL makes it easy to create families of related colors: keep the hue the same and change only the lightness.

html · try it yourself
<style>
  .swatch { display: inline-block; width: 70px; height: 70px; margin: 3px;
            border-radius: 8px; font: 12px sans-serif; color: white;
            text-align: center; line-height: 70px; }
</style>
<p>Same hue (210), different lightness:</p>
<span class="swatch" style="background: hsl(210, 70%, 20%)">20%</span>
<span class="swatch" style="background: hsl(210, 70%, 35%)">35%</span>
<span class="swatch" style="background: hsl(210, 70%, 50%)">50%</span>
<span class="swatch" style="background: hsl(210, 70%, 65%)">65%</span>
<span class="swatch" style="background: hsl(210, 70%, 80%); color: #333">80%</span>
<p>Same lightness, hue going around the wheel:</p>
<span class="swatch" style="background: hsl(0, 70%, 50%)">0</span>
<span class="swatch" style="background: hsl(60, 70%, 45%)">60</span>
<span class="swatch" style="background: hsl(120, 70%, 40%)">120</span>
<span class="swatch" style="background: hsl(240, 70%, 55%)">240</span>
<span class="swatch" style="background: hsl(300, 70%, 50%)">300</span>

Transparency

The alpha value in rgba() and hsla() controls how see-through a color is. 1 is fully solid and 0 is invisible. A semi-transparent background lets whatever is underneath show through, which is handy for overlays and highlights.

Note: Modern browsers also accept a fourth pair of hex digits for alpha (#ff634780) and a space-separated syntax like rgb(255 99 71 / 50%). You will see both in real-world code.

Choosing readable colors

Good color is not just about looks. Text needs enough contrast against its background to be readable for everyone, including people with low vision. Dark text on a light background (or the reverse) is safest. Avoid pairs like light gray on white or red on green.

Tip: Never rely on color alone to carry meaning. If an error message is red, also include a word such as "Error" or an icon.

Test yourself

Which CSS property changes the color of an element's text?

  1. background-color
  2. text-color
  3. color
  4. font-color

What color is rgb(0, 0, 0)?

  1. White
  2. Black
  3. Gray
  4. Red

In rgba(0, 128, 255, 0.3), what does 0.3 control?

  1. Brightness
  2. Opacity (alpha)
  3. Hue
  4. Saturation