← Back to CSS
14

Color in CSS: hex, rgb, hsl and accessibility

Learn to define colors precisely and ensure good contrast.

📘 Theory

Color formats

Know when to use hex, rgb and hsl.

Hex is common in visual design, RGB is useful for manipulation and HSL for hue and light adjustments.

If you need to vary a color in a controlled way, HSL is often the best choice.

  • Hex: #1d4ed8
  • RGB: rgb(29, 78, 216)
  • HSL: hsl(221, 79%, 48%)
  • RGBA/HSLA: adds transparency

Color system with variables

Avoid loose colors and build consistency.

1

Define base colors in :root and reuse them in buttons, backgrounds and texts.

2

This way you can adjust the visual identity from a single place.

Contrast and readability

Good color also reads well.

Sufficient contrast improves reading and accessibility.

Avoid light gray text on light backgrounds: it looks elegant but reads poorly.

  • Main text: high contrast.
  • Secondary text: medium contrast.
  • Buttons: white text on dark color or vice versa.

Modern color with OKLCH and repeating gradients

🧭 Key visuals

HSL color model in CSS

Visualizes how to adjust color more intuitively than with hex values.

Diagram of hue, saturation and lightness to understand HSL.

🧪 Learn by doing

Example Card with HSL Create a card with soft background and readable text.
Example Basic color system Define variables and use them in a button.

🏁 Challenges

Challenge Challenge: accessible contrast Adjust background and text with HSL.

🧰 Resources

Test

Check your knowledge with a test about CSS.

Test for CSS

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com