CSS gives you many units, and choosing well is a mark of an intermediate developer. The right unit makes your design flexible and accessible; the wrong one makes it rigid. Let us sort them out.
Absolute vs relative units
<code>px</code> is absolute and fixed. Relative units scale with something else: <code>%</code> relative to the parent, <code>em</code> relative to the element's own font-size, <code>rem</code> relative to the root font-size, and <code>vw</code>/<code>vh</code> relative to the viewport width/height.
| Unit | Relative to | Great for |
|---|---|---|
| px | Nothing (fixed) | Borders, fine details |
| % | Parent size | Fluid widths |
| rem | Root font-size | Type, spacing (consistent) |
| em | Element font-size | Spacing relative to text |
| vw / vh | Viewport | Full-screen sections |
Why rem matters for accessibility
Using <code>rem</code> for font sizes respects the user's browser font-size setting, which matters for people who increase text size. Fixed <code>px</code> fonts ignore that preference. Prefer <code>rem</code> for type and spacing.
<p class="a">1rem text</p>
<p class="b">1.5rem text</p>:root{font-size:16px}
.a{font-size:1rem}
.b{font-size:1.5rem;color:#2563eb}Fluid sizing with clamp()
The <code>clamp(min, preferred, max)</code> function gives a value that scales between a minimum and maximum — perfect for fluid typography that grows with the screen but never gets too small or too big, without media queries.
<h1 class="fluid">Fluid heading</h1>.fluid{font-size:clamp(1.5rem, 5vw, 3rem)}p { font-size: 2vw; }p { font-size: clamp(1rem, 2vw, 1.25rem); }Using vw for body font-size alone makes text uncomfortably tiny on small screens. Wrap it in clamp() with a sensible minimum.
Use rem for type and spacing, % or fr for fluid widths, px for hairline borders, and clamp() for fluid typography. This mix is both flexible and accessible.
Make a heading use clamp(1.5rem, 5vw, 3rem) and resize the preview to watch it scale. Then set a paragraph in rem and a container width in %.
1. rem is relative to…
2. Why prefer rem for fonts?
3. clamp(1rem, 2vw, 1.5rem) does what?
- px is fixed; %, em, rem, vw/vh are relative.
- Prefer rem for type/spacing — it respects user settings.
- clamp(min, preferred, max) gives fluid sizing without media queries.
- Mix units intentionally: rem for type, % / fr for widths, px for borders.