sodcalculator

10 Tech How-To

HSL Colors in CSS: A Step-by-Step Guide

Guide 10-15825 · Updated · Step by step

HSL (Hue, Saturation, Lightness) is a color model in CSS that lets you define colors by their hue (the actual color), saturation (intensity), and lightness (brightness). It is often more intuitive than hex or RGB because you can adjust brightness or saturation without changing the base color. This guide walks you through using HSL values in CSS, from the basic syntax to advanced techniques like alpha transparency.

What You’ll Need

  • A text editor (like VS Code, Sublime Text, or Notepad++)
  • A web browser (Chrome, Firefox, Safari, Edge) to test your CSS
  • Basic understanding of CSS (selectors and properties)

Step-by-Step

  1. Understand the HSL components

    HSL stands for Hue, Saturation, and Lightness. Hue is a degree on the color wheel (0–360): 0 (or 360) is red, 120 is green, 240 is blue. Saturation is a percentage: 0% is gray, 100% is full color. Lightness is also a percentage: 0% is black, 100% is white, 50% is the pure color.

  2. Use the basic hsl() syntax

    In CSS, write hsl(hue, saturation%, lightness%). For example, to make a bright red, use hsl(0, 100%, 50%). To make a soft blue, hsl(240, 70%, 60%). Place this anywhere you would use a color: color, background-color, border-color, etc.

  3. Add alpha transparency with hsla() or hsl() with a slash

    To make a color semi-transparent, use hsla(hue, sat%, light%, alpha) where alpha is a number between 0 (transparent) and 1 (opaque). For example, hsla(120, 100%, 50%, 0.3) is a translucent green. Modern CSS also allows hsl(120 100% 50% / 0.3) with space-separated values and a slash.

  4. Experiment with hue to change color

    Hue is the most powerful part. To change from red to orange, increase the hue slightly: hsl(30, 100%, 50%). To go from blue to purple, decrease it: hsl(280, 100%, 50%). You can use any number from 0 to 360; values wrap around (e.g., -30 is the same as 330).

  5. Adjust saturation for vibrancy

    Saturation controls how vivid the color is. At 100%, it’s pure; at 0%, it’s gray. For a muted, pastel look, use hsl(200, 30%, 80%). For a bold, vivid color, use hsl(200, 100%, 50%).

  6. Control lightness for shades and tints

    Lightness controls brightness. At 50%, you get the base color. Lower values (like 20%) produce dark shades; higher values (like 80%) produce light tints. For example, hsl(0, 100%, 20%) is dark red, while hsl(0, 100%, 80%) is light pink.

  7. Use HSL in real CSS rules

    Here’s a practical example:

    body {
      background-color: hsl(210, 50%, 90%);
    }
    h1 {
      color: hsl(210, 100%, 40%);
    }
    .button {
      background-color: hsl(120, 60%, 50%);
      border: 2px solid hsl(120, 60%, 30%);
    }

    This creates a light blue background, a dark blue heading, and a green button with a darker border.

  8. Try the relative color syntax (modern CSS)

    CSS Color Module 5 allows you to derive colors from existing ones. For example, --base: hsl(200, 80%, 50%); then color: hsl(from var(--base) h s calc(l - 10%)) makes it darker. This is powerful but check browser support.

Common Mistakes

  • Forgetting the % sign on saturation and lightness. Saturation and lightness must be percentages. Writing hsl(120, 100, 50) is invalid; use hsl(120, 100%, 50%).
  • Using hue values outside 0–360. While values wrap, it’s clearer to keep hue between 0 and 360. Negative values work but can confuse.
  • Confusing lightness with brightness. Lightness 50% is the pure color, not 100%. 100% is always white.
  • Using commas in the modern space-separated syntax. If you use spaces, do not use commas: hsl(120 100% 50%), not hsl(120, 100%, 50%).

Tips

  • Use HSL for color palettes. Keep hue constant and vary lightness/saturation to create a cohesive monochromatic scheme.
  • Pair HSL with CSS variables. Define --primary-hue: 200; then use hsl(var(--primary-hue) 80% 50%) to easily adjust the entire theme.
  • Use hue to shift colors on hover. A button can change from hsl(200, 80%, 50%) to hsl(180, 80%, 50%) for a subtle effect.
  • Test contrast with lightness. For text on a colored background, ensure lightness difference is large enough for readability.

Troubleshooting

  • Color not showing up at all: Check for syntax errors. Ensure saturation and lightness have % signs, and that you’re using commas or spaces correctly. Open browser dev tools (F12) to see if the property is crossed out.
  • Color looks different than expected: Your monitor may be calibrated differently, or you might be confusing hue values. Use a color picker to verify the HSL values.
  • Alpha not working: Make sure you’re using hsla() or the slash syntax. The alpha value must be between 0 and 1, not a percentage.
  • HSL not supported in older browsers: All modern browsers support HSL. For very old browsers, provide a fallback like background-color: #ff0000; before the HSL declaration.

Frequently Asked Questions

  1. What is the difference between HSL and RGB? RGB defines colors by red, green, and blue light intensity (0–255 each). HSL defines by hue, saturation, and lightness, which is more intuitive for adjusting brightness or saturation.
  2. Can I use HSL with hex colors? No, but you can convert between them. Many online tools and browser dev tools show both representations.
  3. How do I make a color darker without making it gray? Decrease the lightness value. For example, hsl(200, 80%, 50%) to hsl(200, 80%, 30%).
  4. What is the hue for black, white, and gray? They have no hue; you can set hue to any value, but saturation must be 0% and lightness 0% (black) or 100% (white). For gray, saturation 0% and lightness between 0% and 100%.
  5. Can I use HSL in CSS gradients? Yes, absolutely. For example, background: linear-gradient(hsl(0, 100%, 50%), hsl(60, 100%, 50%)).
  6. Is HSL better than hex for accessibility? It can be easier to adjust lightness for contrast, but accessibility depends on the actual color contrast ratio, not the format.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →