HTML Colors HSL
What is HSL?
HSL stands for Hue, Saturation, Lightness. It's a color model that represents colors based on human perception of color. HSL makes it easier to create color variations by adjusting saturation and lightness.
HSL Color Syntax
HSL colors are specified using the hsl() function with three values:
Syntax
hsl(hue, saturation%, lightness%)
HSL Components
1. Hue (H)
The hue is the actual color, represented as a degree on the color wheel (0-360):
- 0° = Red
- 60° = Yellow
- 120° = Green
- 180° = Cyan
- 240° = Blue
- 300° = Magenta
- 360° = Red (full circle)
2. Saturation (S)
Saturation is the intensity or purity of the color, expressed as a percentage:
- 0% = Grayscale (no color)
- 100% = Fully saturated (pure color)
3. Lightness (L)
Lightness is how light or dark the color is, expressed as a percentage:
- 0% = Black
- 50% = Normal (pure color)
- 100% = White
Basic HSL Colors
Example
<p style="color: hsl(0, 100%, 50%);">Red (hue: 0°, saturation: 100%, lightness: 50%)</p>
<p style="color: hsl(120, 100%, 50%);">Green (hue: 120°, saturation: 100%, lightness: 50%)</p>
<p style="color: hsl(240, 100%, 50%);">Blue (hue: 240°, saturation: 100%, lightness: 50%)</p>
<p style="color: hsl(60, 100%, 50%);">Yellow (hue: 60°, saturation: 100%, lightness: 50%)</p>
Adjusting Saturation
Changing saturation changes the intensity of the color:
Example
<p style="color: hsl(0, 100%, 50%);">Red - Fully saturated (100%)</p>
<p style="color: hsl(0, 70%, 50%);">Red - Medium saturation (70%)</p>
<p style="color: hsl(0, 40%, 50%);">Red - Low saturation (40%)</p>
<p style="color: hsl(0, 0%, 50%);">Gray - No saturation (0%)</p>
Adjusting Lightness
Changing lightness changes how light or dark the color is:
Example
<p style="background-color: #333; color: hsl(0, 100%, 90%);">Red - Very light (90%)</p>
<p style="color: hsl(0, 100%, 70%);">Red - Light (70%)</p>
<p style="color: hsl(0, 100%, 50%);">Red - Normal (50%)</p>
<p style="color: hsl(0, 100%, 30%);">Red - Dark (30%)</p>
<p style="color: hsl(0, 100%, 10%);">Red - Very dark (10%)</p>
HSLA (HSL with Alpha/Transparency)
HSLA adds an alpha channel for transparency. The alpha value ranges from 0.0 (fully transparent) to 1.0 (fully opaque):
Example
<div style="background-color: yellow; padding: 20px;">
<p style="background-color: hsla(0, 100%, 50%, 1.0);">Red - Fully opaque (1.0)</p>
<p style="background-color: hsla(0, 100%, 50%, 0.7);">Red - 70% opacity (0.7)</p>
<p style="background-color: hsla(0, 100%, 50%, 0.5);">Red - 50% opacity (0.5)</p>
<p style="background-color: hsla(0, 100%, 50%, 0.3);">Red - 30% opacity (0.3)</p>
</div>
Common HSL Color Values
| Color | HSL Value | Example |
|---|---|---|
| Red | hsl(0, 100%, 50%) | Red |
| Orange | hsl(30, 100%, 50%) | Orange |
| Yellow | hsl(60, 100%, 50%) | Yellow |
| Green | hsl(120, 100%, 50%) | Green |
| Cyan | hsl(180, 100%, 50%) | Cyan |
| Blue | hsl(240, 100%, 50%) | Blue |
| Purple | hsl(300, 100%, 50%) | Purple |
| Gray | hsl(0, 0%, 50%) | Gray |
Advantages of HSL
- Easy color variations: Adjust lightness or saturation to create color variations
- Intuitive: More intuitive than RGB for creating color palettes
- Consistent colors: Easier to create consistent color schemes
- Better for gradients: HSL works well for creating smooth gradients
Using HSL in CSS
Example
<style>
.red-text {
color: hsl(0, 100%, 50%);
}
.blue-bg {
background-color: hsl(240, 100%, 50%);
}
.green-border {
border: 2px solid hsl(120, 100%, 50%);
}
</style>
<p class="red-text">Red text</p>
<div class="blue-bg" style="color: white; padding: 10px;">Blue background</div>
<p class="green-border" style="padding: 10px;">Green border</p>
Best Practices
- Use HSL for color palettes: Great for creating consistent color schemes
- Adjust lightness for variations: Use different lightness values for hover states
- Use HSLA for transparency: When you need semi-transparent colors
- Consider browser support: HSL is well-supported in all modern browsers