CSS Color Codes Cheat Sheet: Hex, RGB, HSL Explained

Published on February 1, 2026 • 10 min read

A practical, visual guide to every CSS color format — Hex, RGB, RGBA, HSL, HSLA, and named colors — with conversion tips, code examples, and interactive swatches.

CSS gives you several ways to define colors, and picking the right format can make your stylesheets cleaner, more readable, and easier to maintain. This cheat sheet covers every major color format with visual examples so you can reference it while you code.

Hex Color Codes (#RRGGBB)

Hex codes are the most widely used color format in CSS. A hex code starts with # followed by six hexadecimal digits (0–9, A–F). The six digits break into three pairs: the first pair controls red, the second controls green, and the third controls blue. Each pair ranges from 00 (none) to FF (full intensity), giving you 16.7 million possible colors.

#FF0000
#00FF00
#0000FF
#FF6B35
#2563EB
#10B981
/* Standard 6-digit hex */ color: #FF6B35; background-color: #2563EB; /* 8-digit hex with alpha (last 2 digits = opacity) */ background-color: #2563EB80; /* 50% opacity */

Shorthand Hex (#RGB)

When each pair consists of two identical digits, you can use the 3-digit shorthand. The browser doubles each digit automatically: #F00 becomes #FF0000, and #3AF becomes #33AAFF.

#F00 → #FF0000
#0CF → #00CCFF
#F80 → #FF8800
#333 → #333333
/* Shorthand hex (3 digits) */ color: #F00; /* red — same as #FF0000 */ background: #0CF; /* cyan — same as #00CCFF */ border: 1px solid #333; /* dark gray — same as #333333 */
Pro Tip: Use shorthand hex whenever possible to keep your CSS files smaller. Tools like our Regex Tester can help you find and replace 6-digit codes with their 3-digit equivalents using the pattern #([0-9A-Fa-f])\1([0-9A-Fa-f])\2([0-9A-Fa-f])\3.

RGB and RGBA Colors

The rgb() function defines colors using red, green, and blue channels, each as an integer from 0 to 255. This format is intuitive if you think in terms of mixing light: rgb(255, 0, 0) is pure red, while rgb(255, 255, 255) is white.

rgb(255, 99, 71)
rgb(100, 149, 237)
rgb(50, 205, 50)
rgb(255, 215, 0)

Adding Transparency with RGBA

Append a fourth value between 0 (fully transparent) and 1 (fully opaque) to add alpha transparency. This is perfect for overlays, shadows, and layered UI elements.

/* RGB — no transparency */ background: rgb(37, 99, 235); /* RGBA — 70% opacity */ background: rgba(37, 99, 235, 0.7); /* Modern syntax (CSS Colors Level 4) — space-separated, slash for alpha */ background: rgb(37 99 235 / 0.7);
Note: Modern CSS allows rgb() with a slash for alpha — rgb(37 99 235 / 0.7) — making rgba() technically optional. All evergreen browsers support this syntax.

HSL and HSLA Colors

HSL stands for Hue, Saturation, and Lightness. It's the most human-friendly color model because it maps to how we actually think about color: "I want a slightly darker, desaturated blue."

hsl(0, 100%, 50%)
hsl(0, 100%, 70%)
hsl(0, 100%, 30%)
hsl(0, 50%, 50%)
hsl(0, 20%, 50%)

The swatches above all use the same red hue (0°) — only lightness and saturation change. This is the superpower of HSL: creating tints, shades, and tones by tweaking just one number.

/* Base blue */ color: hsl(220, 90%, 53%); /* Lighter variant — just increase lightness */ background: hsl(220, 90%, 80%); /* Darker variant — decrease lightness */ border-color: hsl(220, 90%, 30%); /* HSLA with 60% opacity */ background: hsla(220, 90%, 53%, 0.6); /* Modern syntax */ background: hsl(220 90% 53% / 0.6);
Design Tip: HSL makes building consistent color palettes easy. Pick a hue, then generate light/dark variants by changing lightness alone. This is how most design systems create their color scales.

CSS Named Colors

CSS defines 147 named colors you can use directly. They're readable and great for prototyping, but limited for production design work. Here are the most useful ones:

Name Hex RGB Preview
tomato #FF6347 rgb(255, 99, 71)
dodgerblue #1E90FF rgb(30, 144, 255)
mediumseagreen #3CB371 rgb(60, 179, 113)
gold #FFD700 rgb(255, 215, 0)
slateblue #6A5ACD rgb(106, 90, 205)
coral #FF7F50 rgb(255, 127, 80)

Two special keywords are also worth knowing: transparent (equivalent to rgba(0,0,0,0)) and currentColor (inherits the current text color — incredibly useful for SVG icons and borders).

Converting Between Formats

You'll often need to convert between formats when working with design tools (which export hex) and CSS custom properties (where HSL shines). Here are the key relationships:

Hex ↔ RGB

Each hex pair is just a base-16 representation of the 0–255 RGB value. For example, #FF6B35 breaks down to: red = FF (255), green = 6B (107), blue = 35 (53) → rgb(255, 107, 53).

/* These three are identical */ color: #FF6B35; color: rgb(255, 107, 53); color: hsl(15, 100%, 60%);

RGB → HSL (Quick Mental Math)

  1. Divide R, G, B each by 255 to get values between 0 and 1.
  2. The hue is determined by which channel is dominant.
  3. Lightness is the average of the max and min channel values.
  4. Saturation depends on lightness and the range between max and min.

In practice, let your tools handle this — but understanding the relationship helps you tweak values by hand. You can encode color data as Base64 strings when passing it through APIs, or generate unique UUIDs to track color palette entries in your design system database.

When to Use Each Format

Format Best For Why
Hex Design hand-off, quick values Compact, universally supported, copy-paste from Figma/Sketch
RGB/RGBA Dynamic colors in JavaScript Easy to compute programmatically, great with canvas APIs
HSL/HSLA Design systems, CSS variables Human-readable, easy to create tints/shades/tones by adjusting one value
Named colors Prototyping, learning Readable, but limited palette — avoid in production

Practical Example: Building a Color Scale

Here's a real-world pattern using HSL to generate a blue color scale for a design system — the same technique used by Tailwind CSS and other frameworks:

:root { /* Blue scale — same hue, varying lightness */ --blue-50: hsl(220, 90%, 96%); --blue-100: hsl(220, 90%, 90%); --blue-200: hsl(220, 90%, 80%); --blue-300: hsl(220, 90%, 70%); --blue-400: hsl(220, 90%, 60%); --blue-500: hsl(220, 90%, 53%); /* primary */ --blue-600: hsl(220, 90%, 45%); --blue-700: hsl(220, 90%, 36%); --blue-800: hsl(220, 90%, 26%); --blue-900: hsl(220, 90%, 18%); }
50
100
200
300
400
500
600
700
800
900

This approach lets you change the entire palette by updating a single hue value. Try it with hsl(150, 80%, ...) for green or hsl(350, 90%, ...) for red.

Quick Reference Table

Color Hex RGB HSL
Black #000000 rgb(0, 0, 0) hsl(0, 0%, 0%)
White #FFFFFF rgb(255, 255, 255) hsl(0, 0%, 100%)
Red #FF0000 rgb(255, 0, 0) hsl(0, 100%, 50%)
Green #00FF00 rgb(0, 255, 0) hsl(120, 100%, 50%)
Blue #0000FF rgb(0, 0, 255) hsl(240, 100%, 50%)
Yellow #FFFF00 rgb(255, 255, 0) hsl(60, 100%, 50%)
Cyan #00FFFF rgb(0, 255, 255) hsl(180, 100%, 50%)
Magenta #FF00FF rgb(255, 0, 255) hsl(300, 100%, 50%)

Essential Developer Tools

Working with colors is easier with the right toolkit. Bookmark these free utilities for your daily workflow:

Conclusion

Each CSS color format has its sweet spot. Use hex for quick, compact values from design tools. Use RGB when computing colors in JavaScript. Use HSL for building design systems and CSS custom properties — it's the most maintainable format for creating consistent palettes. Reserve named colors for prototyping and debugging.

The key insight: you're not choosing one format forever. Most production codebases mix formats depending on context. Understand all four, use whichever fits the task at hand, and your stylesheets will be both powerful and readable.