Docs LogoDocs

CSS Units - Measurement Units

Documentation for CSS Units - Measurement Units.

CSS Units - Measurement Units

What are CSS Units?

CSS units are used to specify lengths, sizes, and other measurements. They're divided into absolute units (fixed) and relative units (based on other values).

Unit Types - Quick Reference

Absolute Units

UnitNameDescriptionExample
pxPixelsFixed pixelsfont-size: 16px;
ptPointsPrint (1pt = 1/72 inch)font-size: 12pt;
cmCentimetersPhysical measurementwidth: 10cm;
mmMillimetersPhysical measurementwidth: 100mm;
inInchesPhysical measurementwidth: 2in;

Relative Units

UnitNameRelative ToExample
%PercentageParent elementwidth: 50%;
emEmParent font-sizefont-size: 1.5em;
remRoot emRoot font-sizefont-size: 1.5rem;
vwViewport width1% of viewport widthwidth: 50vw;
vhViewport height1% of viewport heightheight: 100vh;
vminViewport minimum1% of smaller dimensionfont-size: 5vmin;
vmaxViewport maximum1% of larger dimensionfont-size: 5vmax;
chCharacterWidth of "0" characterwidth: 40ch;
exX-heightHeight of "x" characterline-height: 2ex;

Detailed Examples

1. Pixels (px)

.box {
    width: 200px;
    height: 100px;
    font-size: 16px;
}

When to use: Fixed sizes, borders, small measurements


2. Percentage (%)

.container {
    width: 100%; /* Full width of parent */
}

.half {
    width: 50%; /* Half width of parent */
}

When to use: Responsive widths, flexible layouts


3. em

.parent {
    font-size: 16px;
}

.child {
    font-size: 1.5em; /* 24px (1.5 × 16px) */
    padding: 1em; /* 24px (1 × 24px, based on own font-size) */
}

When to use: Component-based sizing, scalable spacing


4. rem

html {
    font-size: 16px; /* Root font-size */
}

.heading {
    font-size: 2rem; /* 32px (2 × 16px) */
}

.text {
    font-size: 1rem; /* 16px (1 × 16px) */
}

When to use: Consistent sizing across site, preferred for modern development


5. Viewport Units (vw, vh)

.hero {
    height: 100vh; /* Full viewport height */
    width: 100vw; /* Full viewport width */
}

.half-screen {
    height: 50vh;
}

.responsive-text {
    font-size: 5vw; /* Scales with viewport */
}

When to use: Full-screen sections, responsive typography


6. ch (Character Unit)

.readable-text {
    max-width: 60ch; /* Optimal reading width (60 characters) */
}

When to use: Text containers, optimal line length

Common Patterns

Pattern 1: Responsive Typography

html {
    font-size: 16px;
}

h1 { font-size: 2.5rem; } /* 40px */
h2 { font-size: 2rem; } /* 32px */
h3 { font-size: 1.75rem; } /* 28px */
p { font-size: 1rem; } /* 16px */
small { font-size: 0.875rem; } /* 14px */

Pattern 2: Spacing System

:root {
    --space-1: 0.25rem; /* 4px */
    --space-2: 0.5rem; /* 8px */
    --space-3: 0.75rem; /* 12px */
    --space-4: 1rem; /* 16px */
    --space-5: 1.5rem; /* 24px */
    --space-6: 2rem; /* 32px */
}

Pattern 3: Full-Screen Hero

.hero {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

Pattern 4: Responsive Container

.container {
    width: 90%;
    max-width: 1200px;
    margin: 0 auto;
}

Best Practices

  1. Use rem for font-sizes - Consistent and accessible
  2. Use % for widths - Flexible layouts
  3. Use px for borders - Precise control
  4. Use vh/vw for full-screen - Viewport-based layouts
  5. Avoid em for complex nesting - Can compound unexpectedly

Last updated on July 15, 2026

On this page