Skip to main content

CSS Units: Complete Reference

CSS has a rich set of units for expressing lengths. Choosing the right unit is one of the most important decisions for responsive, accessible web design.

1. Absolute Units​

Fixed, physical measurements. Results are identical regardless of screen size or user settings.

UnitNameEquivalent
pxPixels1px = 1/96 of 1 inch (on standard displays)
ptPoints1pt = 1/72 of 1 inch
cmCentimeters1cm = 37.8px
mmMillimeters1mm = 3.78px
inInches1in = 96px

[!IMPORTANT] Use absolute units sparingly in web design. px is acceptable for border widths, box shadows, and fine details, but using px for font-size or layout dimensions overrides user browser preferences for accessibility.

2. Relative Units: Font-Based​

UnitRelative ToBest Used For
emElement's own font-sizePadding/margin that scales with its text
remRoot <html> font-size (usually 16px)Typography, consistent global spacing
exx-height of the current fontRarely used
chWidth of the 0 characterOptimal text line lengths

em vs rem in Practice​

/* rem — predictable, doesn't compound */
h1 { font-size: 3rem; } /* Always 48px (if root = 16px) */
.card { padding: 1.5rem; } /* Always 24px */

/* em — compounds with parent (use intentionally) */
.btn {
font-size: 1rem;
padding: 0.75em 1.5em; /* Scales with the button's own font-size */
}
.btn--sm { font-size: 0.875rem; } /* padding automatically adjusts too */

3. Relative Units: Viewport-Based​

UnitMeaningNotes
vw1% of viewport width100vw = full screen width
vh1% of viewport height100vh bug on mobile (see below)
vmin1% of the smaller of vw/vhUseful for square elements
vmax1% of the larger of vw/vh
svhSmall viewport heightMobile: excludes browser chrome
lvhLarge viewport heightMobile: includes browser chrome
dvhDynamic viewport height✅ Best for mobile hero sections
svw, lvw, dvwWidth variantsSame pattern
cqw1% of container query widthFor container queries

The Mobile vh Fix​

/* ❌ 100vh is broken on iOS Safari (browser chrome is included) */
.hero { min-height: 100vh; }

/* ✅ dvh = dynamic viewport height — adjusts when browser chrome shows/hides */
.hero {
min-height: 100vh; /* Fallback */
min-height: 100dvh; /* Progressive enhancement */
}

4. The ch Unit for Readable Lines​

/* Optimal reading line length: 45–75 characters */
.article-body {
max-width: 72ch; /* About 72 characters per line */
font-size: 1.125rem;
line-height: 1.7;
}

5. CSS Math Functions with Units​

/* clamp(min, preferred, max) — the fluid sizing workhorse */
font-size: clamp(1rem, 2.5vw + 0.5rem, 2rem);
padding: clamp(1rem, 4vw, 3rem);

/* calc() — mix units in expressions */
.sidebar { width: calc(100% - 320px); }
.element { margin-top: calc(var(--space-4) * 2 + 1px); }

/* min() and max() */
.container { width: min(100%, 1200px); } /* Responsive max-width */
.image { width: max(300px, 50%); } /* At least 300px */

6. PX to REM Conversion Table​

Assuming the default root font-size of 16px:

pxrempxrem
4px0.25rem40px2.5rem
8px0.5rem48px3rem
12px0.75rem56px3.5rem
14px0.875rem64px4rem
16px1rem80px5rem
18px1.125rem96px6rem
20px1.25rem128px8rem
24px1.5rem160px10rem
28px1.75rem192px12rem
32px2rem256px16rem

Formula: rem = pixels ÷ 16

7. When to Use Which Unit​

PropertyUnit to UseWhy
font-size (global)remRespects user browser settings
font-size (fluid)clamp()Smooth scaling
padding, marginrem or emScales with text
border, outlinepxFine details should be crisp
box-shadow offset/blurpxPhysical shadow dimensions
Full-width layout% or vwRelative to container/viewport
Hero section heightdvhMobile-safe viewport unit
Text line widthchContent-relative measure
Grid/flex gapsremConsistent spacing