Skip to content
back to cheatsheets

CSS Units Cheatsheet — Every Unit Explained

· Reference

CSS offers a variety of units for sizing elements, spacing, and typography. Choosing the right unit affects accessibility, responsiveness, and maintenance. This reference covers every CSS unit with guidance on when each one is the best choice.

Absolute Units

Absolute units have a fixed size regardless of context. In practice, px is the only absolute unit used for screen-based design.

UnitNameEquivalentUse case
pxPixels1/96 of an inchBorders, shadows, fine-grained control
ptPoints1/72 of an inchPrint stylesheets
pcPicas12 pointsPrint stylesheets
inInches96 pixelsPrint stylesheets
cmCentimeters~37.8 pixelsPrint stylesheets
mmMillimeters~3.78 pixelsPrint stylesheets
QQuarter-millimeters0.25mmPrint stylesheets (rare)

When to use px: Borders (border: 1px solid), box shadows, and any element where you need exact pixel-level precision. Avoid px for font sizes and spacing that should scale with user preferences.

Relative Units — Font-based

These units scale relative to a font size, making them ideal for building proportional, accessible layouts.

UnitRelative toExample
emFont size of the current elementpadding: 1.5em (1.5x the element’s font size)
remFont size of the root element (<html>)font-size: 1.125rem (18px if root is 16px)
exx-height of the current fontRarely used; roughly half the em
chWidth of the “0” (zero) charactermax-width: 65ch for readable line lengths
capHeight of capital lettersExperimental; limited support
icWidth of the CJK “water” ideographCJK typography
lhLine height of the elementUseful for vertical rhythm
rlhLine height of the root elementVertical rhythm relative to root

em vs rem

emrem
Relative toParent element’s font sizeRoot <html> font size
CompoundsYes (nested elements multiply)No (always relative to root)
Best forComponent-internal spacingGlobal sizing, font sizes, layout spacing
Gotcha1.5em inside a 1.5em = 2.25emAlways predictable
/* rem for font sizes — predictable scaling */
h1 { font-size: 2rem; }      /* 32px at default 16px root */
h2 { font-size: 1.5rem; }    /* 24px */
body { font-size: 1rem; }    /* 16px */

/* em for component-internal spacing — scales with the component */
.button {
  font-size: 1rem;
  padding: 0.5em 1em;        /* scales if font-size changes */
}

Relative Units — Viewport

Viewport units are relative to the browser window dimensions.

UnitRelative toExample
vw1% of viewport widthwidth: 50vw (half the screen width)
vh1% of viewport heightheight: 100vh (full screen height)
vmin1% of the smaller dimensionUseful for elements that should fit in any orientation
vmax1% of the larger dimensionLess common
dvw1% of dynamic viewport widthAccounts for mobile browser chrome
dvh1% of dynamic viewport heightAccounts for mobile address bar showing/hiding
svw1% of small viewport widthSmallest possible viewport
svh1% of small viewport heightWith all browser UI visible
lvw1% of large viewport widthLargest possible viewport
lvh1% of large viewport heightWith browser UI hidden

The mobile viewport problem: On mobile browsers, 100vh can be taller than the visible area because the address bar is part of the viewport calculation. Use 100dvh for full-height layouts that account for the dynamic browser chrome, or 100svh for the safe minimum height.

/* Full-height hero that works on mobile */
.hero {
  min-height: 100dvh;       /* dynamic viewport height */
}

/* Fallback for older browsers */
.hero {
  min-height: 100vh;
  min-height: 100dvh;
}

Percentage (%)

Percentages are relative to the parent element’s corresponding property.

ContextRelative to
width: 50%Parent’s width
height: 50%Parent’s height (parent must have explicit height)
padding: 10%Parent’s width (even for top/bottom padding)
margin: 10%Parent’s width (even for top/bottom margin)
font-size: 120%Parent’s font size
line-height: 150%Element’s own font size
transform: translateX(50%)Element’s own width

The fact that vertical padding and margin percentages are based on the parent’s width (not height) is one of CSS’s most surprising behaviors. This quirk is actually useful for creating aspect ratios:

/* 16:9 aspect ratio using padding trick (pre-aspect-ratio) */
.video-wrapper {
  position: relative;
  padding-bottom: 56.25%;    /* 9/16 = 0.5625 */
}

/* Modern approach */
.video-wrapper {
  aspect-ratio: 16 / 9;
}

Container Query Units

These units are relative to a query container’s dimensions, enabling component-level responsive design.

UnitRelative to
cqw1% of container’s width
cqh1% of container’s height
cqi1% of container’s inline size
cqb1% of container’s block size
cqminSmaller of cqi or cqb
cqmaxLarger of cqi or cqb
.card-container {
  container-type: inline-size;
}

.card-title {
  font-size: clamp(1rem, 4cqi, 2rem);
}

The clamp() Function

clamp() lets you set a responsive value with minimum and maximum bounds. It works with any unit.

/* Fluid typography */
font-size: clamp(1rem, 2.5vw, 2rem);
/*              min    preferred   max */

/* Fluid spacing */
padding: clamp(1rem, 3vw, 3rem);

/* Fluid width */
width: clamp(300px, 50%, 800px);

Quick Reference: Which Unit When?

PropertyRecommended UnitWhy
Font sizeremRespects user preferences, predictable
Line heightUnitless number (e.g., 1.5)Scales proportionally with font size
Padding/marginrem or emScales with text size
Width%, vw, or chResponsive to container or viewport
Max-widthch or rem65ch for readable prose
Heightdvh or autoAvoid fixed heights when possible
BorderspxFine-grained control needed
Box shadowspxFine-grained control needed
Media queriesemConsistent across zoom levels
Border radiuspx or %px for subtle, % for circles

Convert between px and rem with the px to rem Converter, or explore all units in the CSS Units Reference.

#Learn More