Skip to main content

CSS Units Cheatsheet

Complete reference for all CSS length, angle, and time units.
Click any value to copy it to your clipboard.

Visual Comparison

100px
1em
(relative to this container's font-size)
1rem
(relative to root font-size: 16px default)
10vw
(10% of viewport width)
50%
(relative to parent width)

em vs rem - Nested Elements

See how em compounds with nesting while rem stays consistent.

Using em (compounds)

Parent: 16px (font-size: 1em)

Child: 24px (font-size: 1.5em)

Grandchild: 36px (font-size: 1.5em)

Using rem (consistent)

Parent: 16px (font-size: 1rem)

Child: 24px (font-size: 1.5rem)

Grandchild: 24px (font-size: 1.5rem)

Viewport Units Explained

vhvw

vw / vh

Standard viewport

svh

svw / svh

Small viewport (UI visible)

lvh

lvw / lvh

Large viewport (UI hidden)

dvw/dvh (Dynamic) adjusts automatically as browser UI appears/disappears - best for mobile full-screen layouts.

px to rem Converter

1rem

Formula: rem = px / base-font-size (default base is 16px)

Absolute Units

Fixed size, not relative to anything else. Use for print or fixed-size elements.

UnitNameDescriptionExample
pxPixels1px = 1/96th of 1in
cmCentimeters1cm = 37.8px
mmMillimeters1mm = 3.78px
inInches1in = 96px
ptPoints1pt = 1/72 of 1in
pcPicas1pc = 12pt = 16px

Relative Units (Font-based)

Relative to font metrics. Great for responsive typography and spacing.

UnitNameDescriptionExample
emEmRelative to parent font-size
remRoot EmRelative to root font-size (html)
exExx-height of current font
chCharacterWidth of '0' character
capCap HeightHeight of capital letters
icIdeographicIdeographic character width (CJK)
lhLine HeightLine height of the element
rlhRoot Line HeightLine height of root element

Viewport Units

Relative to the browser viewport. Perfect for full-screen layouts.

UnitNameDescriptionExample
vwViewport Width1% of viewport width
vhViewport Height1% of viewport height
vminViewport Min1% of smaller dimension
vmaxViewport Max1% of larger dimension
svwSmall Viewport Width1% of small viewport width
svhSmall Viewport Height1% of small viewport height
lvwLarge Viewport Width1% of large viewport width
lvhLarge Viewport Height1% of large viewport height
dvwDynamic Viewport Width1% of dynamic viewport width
dvhDynamic Viewport Height1% of dynamic viewport height

Container Query Units

Relative to the nearest ancestor with container-type set.

UnitNameDescriptionExample
cqwContainer Width1% of container width
cqhContainer Height1% of container height
cqiContainer Inline1% of container inline size
cqbContainer Block1% of container block size
cqminContainer MinSmaller of cqi or cqb
cqmaxContainer MaxLarger of cqi or cqb

Percentage

Relative to the parent element's corresponding property.

UnitNameDescriptionExample
%PercentageRelative to parent (width for width, font-size for font-size)

Angle Units

Used for gradients, transforms (rotate), and other angle-based properties.

UnitNameDescriptionExample
degDegreesFull circle = 360deg
radRadiansFull circle = 2pi rad
gradGradiansFull circle = 400grad
turnTurnsFull circle = 1turn
0deg
45deg
90deg
180deg
270deg

Time Units

Used for animations and transitions.

UnitNameDescriptionExample
sSecondsDuration in seconds
msMilliseconds1s = 1000ms

When to Use What

Use px for:

  • - Fixed-size elements (borders, shadows)
  • - Fine-grained control needed
  • - Small decorative details
  • - Images with specific dimensions

Use rem for:

  • - Font sizes (accessibility!)
  • - Spacing (padding, margin)
  • - Global consistency
  • - Responsive scaling with root font

Use em for:

  • - Component-relative sizing
  • - Button padding (scales with text)
  • - Icon sizes relative to text
  • - Nested typography modules

Use vw/vh for:

  • - Full-screen sections
  • - Hero banners (100vh)
  • - Viewport-relative typography
  • - Fluid layouts without breakpoints

Use % for:

  • - Fluid grid layouts
  • - Container-relative widths
  • - Flexible images
  • - Aspect ratio containers

Use ch for:

  • - Optimal line length (45-75ch)
  • - Input field widths
  • - Code blocks
  • - Monospace content

Pro Tips

Accessibility: Always use rem for font sizes. Users who increase their browser's base font size will benefit from proper scaling.

Mobile Safari: Use dvh instead of vh to avoid issues with the URL bar changing the viewport height.

Line Length: For readability, set max-width: 65ch on text containers to maintain optimal character count per line.

Clamp: Combine units with clamp(min, preferred, max) for fluid typography: font-size: clamp(1rem, 2.5vw, 2rem)