Almost every CSS declaration depends on units — lengths for sizing, colors for color. This section reviews the most common ones, laying the groundwork for 10.2 calc / min / max / clamp and responsive layouts.
Absolute Units: px
px (pixels) is the most intuitive absolute unit; it is not affected by the font size of the parent or the root element. It suits borders, shadow offsets, and other details that do not need to scale with the font size.
em is computed against "the font size of the current element" and accumulates as it nests deeper; rem (root em) is always relative to the root element's (<html>) font size and does not accumulate through nesting, which makes it the currently recommended typographic unit.
The compounding effect of em
Outer 16px
Middle 1.5em = 24px
Inner 1.5em = 36px (computed against 24px)
html { font-size: 16px; } /* the baseline for 1rem */.title { font-size: 1.5rem; } /* always equals 24px, unaffected by nesting */.title .icon { margin-right: 0.5em; } /* relative to .icon's own font size, often used for icon spacing */
Rule of thumbUse rem for font sizes to avoid unexpected scaling from nested accumulation; for spacing such as padding/gap that you want to "scale proportionally with the current element's font size", em is actually more intuitive.
Viewport Units
Unit
Meaning
vw
1% of the viewport width
vh
1% of the viewport height
vmin
The smaller of vw and vh
vmax
The larger of vw and vh
svh / lvh / dvh
The small / large / dynamic viewport heights under the influence of the mobile browser's address bar, respectively
.hero {
min-height: 100vh; /* the mobile address bar may cause jumping */min-height: 100dvh; /* prefer the dynamic viewport height to fix it */
}
Percentages
A percentage is always computed against some "reference", and different properties have different references — this is where beginners get most confused:
width / height percentages — relative to the corresponding dimension of the containing block (usually the parent element).
margin / padding percentages (including the top and bottom) — always relative to the width of the parent; this is the principle behind the "padding-top: 100% to reserve a square" trick.
font-size percentages — relative to the parent's font size, equivalent to em.
line-height percentages — relative to the element's own font size.
Color Units
Syntax
Example
Description
Hexadecimal
#3f5d54 / #3f5d54cc
The most common; the optional last two digits express alpha
rgb()
rgb(63 93 84 / 80%)
Modern syntax supports space-separated values plus a slash alpha
hsl()
hsl(160 20% 30%)
Hue / saturation / lightness; adjusting lightness is more intuitive
Keywords
currentColor / transparent
currentColor references the element's own color value, often used so borders and SVG fills follow the text color
Overall page font sizes and typographic dimensions — prefer rem; you can scale the whole site through the root font size (e.g., users enlarging fonts for accessibility).
Details that should not scale with the font size, such as borders and shadows — use px.
Large blocks that need to track the viewport size (such as the first-screen height) — use vh/vw, and prefer dvh on mobile.
Spacing inside components that should track the element's own font size (icons, badges) — use em.