Introduction to HTML Colors

HTML colors are specified using predefined color names, or RGB, HEX, HSL, RGBA, HSLA values. Colors can be applied to text, backgrounds, borders, and other elements.

Color Systems in HTML

1. Named Colors

HTML supports 140+ predefined color names.

red
blue
green
orange
purple
teal

2. HEX Colors

Hexadecimal colors with format #RRGGBB where RR, GG, BB are hexadecimal values (00-FF).

#ff0000
#00ff00
#0000ff
#ffa500
#800080
#008080

3. RGB Colors

RGB colors with format rgb(red, green, blue) where values range from 0-255.

rgb(255,0,0)
rgb(0,255,0)
rgb(0,0,255)
rgb(255,165,0)

4. RGBA Colors

RGB with alpha channel for transparency (0.0 - 1.0).

rgba(255,0,0,0.5)
rgba(0,255,0,0.3)
rgba(0,0,255,0.7)

5. HSL Colors

HSL: Hue (0-360), Saturation (0%-100%), Lightness (0%-100%).

hsl(0,100%,50%)
hsl(120,100%,50%)
hsl(240,100%,50%)

6. HSLA Colors

HSL with alpha channel for transparency.

hsla(0,100%,50%,0.5)
hsla(120,100%,50%,0.3)

Implementation Examples

Text Colors

<p style="color: red;">This is red text</p>
<p style="color: #ff0000;">This is HEX red text</p>
<p style="color: rgb(255, 0, 0);">This is RGB red text</p>
<p style="color: rgba(255, 0, 0, 0.7);">This is transparent red text</p>

Background Colors

<div style="background-color: lightblue; padding: 10px;">
  Light blue background
</div>

<span style="background-color: yellow; padding: 5px;">
  Yellow highlighted text
</span>

Border Colors

<div style="border: 2px solid #3498db; padding: 10px;">
  Blue bordered element
</div>

<button style="border: 1px solid green; background: lightgreen;">
  Green button
</button>

Best Practices

  • Use consistent color schemes throughout your website
  • Choose colors with sufficient contrast for accessibility
  • Consider color blindness when selecting color combinations
  • Use CSS variables for maintainable color schemes
  • Test colors on different devices and screens
  • Use RGBA/HSLA for smooth transparency effects
  • Consider using CSS preprocessors for complex color manipulation

Accessibility Considerations

Color Contrast

Ensure sufficient contrast between text and background colors:

  • WCAG AA: 4.5:1 for normal text, 3:1 for large text
  • WCAG AAA: 7:1 for normal text, 4.5:1 for large text

Don't Rely on Color Alone

Use additional indicators besides color:

  • Add text labels to color-coded information
  • Use patterns or icons with colors
  • Provide text alternatives for color meanings

Testing Tools

  • WebAIM Color Contrast Checker
  • Chrome DevTools Accessibility features
  • Color blindness simulator tools