</> ConvertAtlas Developer Tools
Level 2 · Intermediate — Layout & Responsive Lesson 16 Intermediate 13 min

CSS Units: px, %, rem, em, vh, vw and clamp

Choose the right unit for every job — fixed, relative, viewport and fluid sizing.

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.

UnitRelative toGreat for
pxNothing (fixed)Borders, fine details
%Parent sizeFluid widths
remRoot font-sizeType, spacing (consistent)
emElement font-sizeSpacing relative to text
vw / vhViewportFull-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.

HTML
<p class="a">1rem text</p>
<p class="b">1.5rem text</p>
CSS
:root{font-size:16px}
.a{font-size:1rem}
.b{font-size:1.5rem;color:#2563eb}
Result
1rem = root font-size (16px here).

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.

HTML
<h1 class="fluid">Fluid heading</h1>
CSS
.fluid{font-size:clamp(1.5rem, 5vw, 3rem)}
Result
Resize the preview — the heading scales between 1.5 and 3rem.
Common mistake
Don't
p { font-size: 2vw; }
Do
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.

Best practice

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.

Try it yourself

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 %.

Quick quiz

1. rem is relative to…

2. Why prefer rem for fonts?

3. clamp(1rem, 2vw, 1.5rem) does what?

Key takeaways
  • 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.