P2.2: HSL Colors & Gradients
Color brings emotion, layout depth, and visual interest to your web designs. In this lesson, we will master how to declare foreground and background colors using various formats and transparency settings.
Color brings emotion, layout depth, and visual interest to your web designs. In this lesson, we will master how to declare foreground and background colors using various formats and transparency settings.
Lesson: p2-2-colors
Color brings emotion, layout depth, and visual interest to your web designs. In this lesson, we will master how to declare foreground and background colors using various formats and transparency settings.
CSS color declarations target two primary areas of any HTML element box:
color): Sets the color of the text inside an element.background-color): Sets the background color of the element box. If no background color is defined, it defaults to transparent.body {
color: #f8fafc;
background-color: #0f172a;
}
A curated, harmonious color system is the signature of premium digital product interfaces.
Relative readability scores based on accessibility contrast ratios.
Every color on a screen is created by mixing red, green, and blue light (pixels). CSS supports several ways to specify these values:
CSS recognizes 147 predefined color names (e.g. DarkCyan, silver, white, black, tomato). Their usage is limited because they do not cover custom design system values.
The most common web format. Renders as a pound sign # followed by six hexadecimal digits (each pair representing Red, Green, and Blue from 00 to ff).
h1 { color: #4ade80; } /* Neon Green */
Specifies color intensity as numbers between 0 and 255 (representing Red, Green, Blue).
h1 { color: rgb(74, 222, 128); }
An intuitive format based on human color perception:
h1 { color: hsl(120, 70%, 50%); }
CSS provides two distinct ways to make elements transparent: the opacity property and alpha channels.
opacity PropertySpecifies transparency for the entire element, including its text and any nested child elements. The value ranges from 0.0 (fully transparent) to 1.0 (fully opaque).
.card {
opacity: 0.5; /* Renders card background, borders, and TEXT at 50% opacity */
}
Sets transparency for only the specific property it is applied to (e.g., just the background color), leaving the nested child elements (like text) fully opaque and readable.
rgba(red, green, blue, alpha)hsla(hue, saturation, lightness, alpha)0.0 (invisible) to 1.0 (opaque)..card {
/* Renders a semi-transparent background, but text remains solid and readable! */
background-color: rgba(15, 23, 42, 0.5);
}
When choosing foreground and background colors, it is crucial to ensure there is enough contrast for the text to be readable.
[!TIP] Typography Tip: If you reverse text (light text on a dark background), increase the line height (
line-height) and font weight (font-weight) slightly. This counteracts the visual blending of glowing white text against black.