Web Analytics

CSS Colors

Beginner ~10 min read

Color in CSS

CSS offers multiple ways to specify colors. Each format has its advantages, and you'll use different ones depending on your needs.

1. Named Colors

CSS has 140 predefined color names like red, blue, green. Easy to remember but limited.

HTML
CSS
JS

2. Hexadecimal (Hex) Colors

Hex colors use a # followed by 6 digits (or 3 for shorthand). Format: #RRGGBB where RR=red, GG=green, BB=blue.

HTML
CSS
JS
Shorthand: #fff = #ffffff (white), #000 = #000000 (black)

3. RGB and RGBA

RGB uses red, green, blue values (0-255). RGBA adds an alpha channel for opacity (0-1).

HTML
CSS
JS

4. HSL and HSLA

HSL stands for Hue (0-360°), Saturation (0-100%), Lightness (0-100%). HSLA adds opacity. More intuitive for color adjustments.

HTML
CSS
JS

When to Use Each Format

  • Hex - Most common, design tools export hex codes
  • RGB/RGBA - When you need transparency
  • HSL/HSLA - Creating color variations (lighter/darker)
  • Named - Quick prototyping or very basic colors

Quick Quiz

Which color format allows you to set transparency?

A
Hex
B
Named colors
C
RGBA
D
RGB