Colors, Backgrounds and Typography
Build polished interfaces with modern color systems, gradients, backgrounds, fonts, text metrics, and readable typography.
CSS Color Systems
CSS supports named colors, hexadecimal notation, RGB, HSL, HWB, and modern color functions. Modern interfaces often benefit from relative color manipulation, alpha channels, and perceptual color spaces where browser support permits.
Using custom properties for semantic colors makes themes easier to maintain.
:root {
--color-primary: #2563eb;
--color-text: rgb(15 23 42);
--color-muted: hsl(215 16% 47%);
--color-overlay: rgb(0 0 0 / 0.5);
}- Use semantic color variables for scalable design systems.
- Alpha channels can be expressed directly in modern RGB syntax.
Which notation represents an alpha channel in modern RGB syntax?
Backgrounds and Gradients
Backgrounds can use colors, images, gradients, multiple layers, positioning, sizing, repetition, and clipping. Gradients are generated by the browser and do not require image files.
Multiple backgrounds are painted in layers, with the first background image generally appearing closest to the user.
.hero {
background:
linear-gradient(rgb(15 23 42 / 0.72), rgb(15 23 42 / 0.72)),
url("hero.jpg") center / cover no-repeat;
}- CSS gradients can replace many decorative raster assets.
- Multiple background layers can create sophisticated visual effects.
What does `background-size: cover` generally do?
Fonts and Font Stacks
The font-family property defines the preferred typeface. A font stack provides fallbacks in case a preferred font is unavailable.
System font stacks are often excellent for performance because they avoid downloading an additional web font.
body {
font-family:
system-ui,
-apple-system,
BlinkMacSystemFont,
"Segoe UI",
sans-serif;
}- Always provide reasonable fallback fonts.
- System fonts can provide excellent performance and native appearance.
Why are multiple fonts commonly specified in `font-family`?
Font Size, Weight and Line Height
Readable typography depends on more than font size. Weight, line height, letter spacing, measure, contrast, and responsive scaling all affect readability.
Unitless line heights are often useful because the value scales naturally with the inherited font size.
.article {
font-size: 1rem;
line-height: 1.7;
letter-spacing: 0.01em;
max-inline-size: 70ch;
}
.article h1 {
font-size: clamp(2rem, 5vw, 4rem);
line-height: 1.05;
}ch unit can constrain reading width and clamp() creates responsive typography.- Line height has a major impact on readability.
clamp()is useful for fluid typography.chcan help constrain text measure.
Why can a unitless `line-height` be useful?