HTML BasicsLesson 16 of 36

HTML Colors

Color is the fastest way to give a page personality, and HTML offers several ways to define it. The simplest is a color name like red or blue. There are over a hundred named colors, from coral to midnightblue, and you can type them anywhere a color is expected.

Color is the fastest way to give a page personality, and HTML offers several ways to define it. The simplest is a color name like red or blue. There are over a hundred named colors, from coral to midnightblue, and you can type them anywhere a color is expected.

Names run out quickly, so CSS also accepts hex codes. A hex code is a hash sign followed by six characters that mix red, green, and blue, two characters each. The code #2563eb is a specific blue. Design tools let you pick a color visually and copy its hex code, which is how most people get them.

The third common format is RGB, which writes the same three amounts as numbers from 0 to 255. All three formats set the same properties, so the choice is mostly habit. Hex codes dominate the web, and color pickers hand them to you, so expect to type a lot of hash signs.

Reading a hex code

A hex code follows the pattern hash, then six characters: two for red, two for green, two for blue. The characters run from 0 to 9 and a to f, where 00 means none and ff means full. Pure red is #ff0000, pure green is #00ff00, and pure blue is #0000ff.

HTML
<p style="color: #ff0000;">Red</p>
<p style="color: #00ff00;">Green</p>
<p style="color: #0000ff;">Blue</p>

Where colors apply

Colors attach to the same places in every format. The color property changes the text, background-color fills the area behind it, and border-color outlines elements. Whichever format you pick, keep enough contrast between text and background, because low contrast is the most common readability mistake.

HTML
<p style="color: #2563eb;">Blue text</p>
<div style="background-color: #f3f4f6; padding: 10px;">Gray box</div>

Syntax

HTML
<p style="color: #2563eb;">Text in hex blue</p>

Yellow on White Defined by Name Fails Contrast for 1 in 12 Users

Named colors like yellow, aqua, lime on white have contrast <2:1, failing WCAG AA (4.5:1) and invisible to low-vision/color-blind users (~8% men). DevTools Lighthouse flags <mark> default yellow background with white text. Never rely on color alone for meaning - pair with bold/icon and check with WebAIM contrast checker.

HTML
mark{ background: #ffeb3b; color:#000 } // not background:yellow

Good to know

  • Use color names for speed and hex codes for precision.
  • A hex code is a hash sign plus six characters for red, green, and blue.
  • RGB mixes the same colors with numbers from 0 to 255.
  • Keep enough contrast between text and background colors.
Try it Yourself Edit the code, press Run, then check what changed in the Result.
Ready
Code
Result

Practice makes it stick

Open the Playground and build your own page with the tags you just learned.

→ Open Playground