EDT
Everyday Dev Tools
Fast tools, no fuss
Home / Tutorials / Color in Web Development: A Practical Guide
Color

Color in Web Development: A Practical Guide

2024-06-20 · 15 min read

Learn about color formats, conversion, accessibility, and best practices for working with colors.

Color Formats You'll See (And How They Work)

Let's start with the basics: The different ways we represent colors in code.

**Hex Colors (#RRGGBB or #RGB):**

The most common format on the web. Two hex digits (00-FF) for red, green, blue. Example: `#FF5733` is orange. Short form: `#F53` is the same as `#FF5533`.

**RGB and RGBA (rgb(R, G, B) or rgba(R, G, B, A)):**

Red, Green, Blue values from 0-255, plus optional alpha (0 = transparent, 1 = opaque). Example: `rgb(255, 87, 51)` or `rgba(255, 87, 51, 0.5)`.

**HSL and HSLA (hsl(H, S%, L%) or hsla(...)):**

Hue (0-360 on a color wheel), Saturation (0-100%), Lightness (0-100%). More intuitive for humans! Example: `hsl(12, 100%, 60%)`.

**Named Colors:**

Names like `red`, `blue`, `goldenrod`, `rebeccapurple` — there are 140 standard ones. Good for quick prototypes.

Our Color Converter can translate between all these formats instantly — it's great for seeing how the same color looks in different systems!

HSL: My Favorite Format for Designing Colors

I used to use hex colors for everything, but now I almost always use HSL. Here's why:

**Hue (0-360):** Where the color is on the color wheel. 0 is red, 120 is green, 240 is blue, 360 is red again.

**Saturation (0-100%):** How vibrant the color is. 0% is gray, 100% is fully saturated.

**Lightness (0-100%):** How light or dark the color is. 0% is black, 100% is white, 50% is "normal" brightness.

Why is this so great? Because you can easily create variations of a color!

- Want a lighter shade? Increase lightness!

- Want a darker shade? Decrease lightness!

- Want a less intense version? Decrease saturation!

- Want a complementary color? Add 180 to the hue!

This makes creating color schemes so much easier. Once you start using HSL, you'll never want to go back to guessing hex codes!

Color Accessibility (Contrast Matters!)

Accessibility isn't optional. If your text doesn't have enough contrast against its background, some users won't be able to read it — and that's a problem.

The Web Content Accessibility Guidelines (WCAG) say:

- AA level requires 4.5:1 contrast for normal text, 3:1 for large text

- AAA level requires 7:1 for normal text, 4.5:1 for large text

How do you measure this? I use tools like WebAIM Contrast Checker, and I always check in my design system.

Some quick tips:

- Dark text on white almost always works (if it's dark enough!)

- Light text on dark backgrounds needs more lightness difference than you think

- Avoid colored text on colored backgrounds unless you check the contrast

- Don't use color alone to convey information (add icons or text labels too)!

And if you're not sure — check! Accessibility benefits everyone, not just users with disabilities.

Building Color Palettes (My Process)

Here's how I build color palettes for projects:

**1. Pick a primary color** — This is your brand color, the main color you'll see everywhere.

**2. Create light and dark variations** — Use HSL! Keep the same hue, adjust lightness (and maybe saturation a bit). I usually make 3 light, 3 dark variations plus the base color.

**3. Pick an accent color** — Something that complements your primary. Often the complementary hue (add 180), or adjacent (analogous).

**4. Define your grays** — Use desaturated colors, not pure black/white. A cool gray with a touch of blue, or a warm gray with a touch of orange, looks nicer than flat gray.

**5. Add status colors** — Green for success, yellow for warning, red for error, blue for info. Keep these consistent!

**6. Check accessibility** — Make sure your text colors work on your backgrounds.

Our Color Picker can help you experiment with different palette ideas before committing!

Colors in CSS (Modern Features You'll Love)

CSS has great modern color features — here are my favorites:

**CSS Variables (Custom Properties):**

:root {
  --primary: hsl(220, 85%, 57%);
  --primary-light: hsl(220, 85%, 70%);
  --primary-dark: hsl(220, 85%, 40%);
}
.button { background: var(--primary); }
.button:hover { background: var(--primary-dark); }

This changed my workflow completely. Define colors once, use everywhere!

**Color Functions (coming soon!):**

The CSS `color-mix()` and `color-contrast()` functions will let you modify colors right in CSS. They're supported in most browsers now!

**OKLCH:**

A new perceptually uniform color space. Colors are consistent in perceived lightness across hues. The future of color on the web!

One pro tip: Never hardcode the same color in 50 places. Use CSS variables or a design system.

Common Color Pitfalls to Avoid

I've made all these mistakes so you don't have to:

**Pitfall 1: Too many colors** — You probably only need 1-2 primary colors, an accent, status colors, and grays. Too many colors look messy.

**Pitfall 2: Not checking contrast** — I've shipped blue text on blue backgrounds before. Check contrast, don't guess!

**Pitfall 3: Inconsistent use** — If you use green for success, don't use green elsewhere. Keep your color semantics consistent.

**Pitfall 4: Ignoring system preferences** — Respect `prefers-color-scheme` for dark/light mode, and `prefers-reduced-motion` if you have color animations.

**Pitfall 5: Pure black/white** — Pure black (`#000000`) on pure white (`#FFFFFF`) can be harsh. Try `#111111` on `#F9F9F9` instead — much easier on the eyes!

Colors are emotional! Good color choices make your product feel cohesive and professional. Take the time to get them right.

Tools

Try These Tools

Open the tools that go with this tutorial and practice what you learned.

Navigation

Keep Learning