Skip to content

Search

Articles, tools, solutions and knowledge base guides

Start typing to search across articles, tools and apps.

CSS Units: px vs rem vs em vs vh vs vw Explained

A CSS unit defines what a dimension depends on. The same numeric value in rem, em or vw can produce very different lengths. In a production interface, choose units based on what should scale: text, a component, available space or the viewport. Examples assume horizontal writing unless noted otherwise.

Published Written by Reading time 7 min read
On this page Contents: quick navigation between sections

CSS pixels and rem

<div class="card">Settings</div>
html {
  font-size: 100%;
}

.card {
  box-sizing: border-box;
  padding: 0.75rem;
  border: 1px solid #64748b;
  font-size: 1.25rem;
  line-height: 1.5;
}

A CSS pixel is an absolute CSS length, not necessarily a physical display pixel. Browser zoom and high-density screens can change the relationship between CSS and device pixels. rem is relative to the computed font size of the root element. [1][2]

With a 16px root, 1.25rem and 0.75rem are 20px and 12px; with a 20px root, they become 25px and 15px. A 1px border remains one CSS pixel. html { font-size: 100%; } preserves the browser's base font preference. Do not assume every user has a 16px base.

em and nested components

<div class="menu">
  <div class="menu__level">
    Menu
    <div class="menu__level">Submenu</div>
  </div>
</div>
.menu {
  font-size: 20px;
}

.menu__level {
  font-size: 0.8em;
  padding: 0.5em;
}

.menu--stable .menu__level {
  font-size: 1rem;
}

For font-size, em resolves against the inherited font size. For other properties, such as padding, it uses the element's computed font size. This makes em useful for scaling spacing in proportion to a button label. [1][2]

If a parent is 20px and its child uses font-size: 0.8em, the child is 16px. A further nested element with the same multiplier becomes 12.8px. Padding of 0.5em on the first child is 8px. Use rem for text and em for local spacing when nested menus should not compound the scale.

Percentages, ch and available width

<article class="reading-panel">
  <p>Example article content.</p>
</article>
.reading-panel {
  box-sizing: border-box;
  inline-size: min(100%, 65ch);
  margin-inline: auto;
  padding: 1rem;
  overflow-wrap: anywhere;
  line-height: 1.5;
}

A percentage is not a universal length unit: its reference depends on the property. A percentage width in normal block layout refers to the containing block's width. In ordinary horizontal writing, ch is based on the advance width of the zero glyph in the font, not the average letter width. [1][2]

An article container with inline-size: min(100%, 65ch) is limited by available width and a chosen reading measure. 65ch does not mean exactly 65 characters per line. box-sizing: border-box includes padding and borders in the declared width. Avoid width: 100vw inside an ordinary container because classic scrollbars can cause horizontal overflow.

vh, vw and mobile viewport sizes

<main class="page">
  <h1>Application panel</h1>
</main>
.page {
  box-sizing: border-box;
  min-block-size: 100vh;
  min-block-size: 100dvh;
  padding: 1rem;
}

.page--stable {
  min-block-size: 100vh;
  min-block-size: 100svh;
}

1vw is 1% of the large viewport width; 1vh is 1% of its height. On devices with retractable browser UI, svh uses the small viewport, lvh the large viewport and dvh the dynamic viewport. The default vh is equivalent to lvh. [1][2]

For illustrative small and large heights of 700px and 800px, 100svh is 700px, 100lvh is 800px and 100dvh can vary between them. Prefer min-block-size to a fixed height so content can grow. dvh is not a guarantee of virtual keyboard handling, and browsers need not update it on every frame of a toolbar animation.

clamp() and fluid typography

<h1 class="page-title">Sales report</h1>
.page-title {
  margin-block: 0 1rem;
  font-size: 1.25rem;
  font-size: clamp(1.25rem, 1rem + 2vw, 2.5rem);
  line-height: 1.2;
  overflow-wrap: anywhere;
}

clamp(minimum, preferred, maximum) constrains a calculation within a range when its bounds are correctly ordered. Combining rem and vw makes the value respond to both the root font size and viewport width. [3]

For clamp(1.25rem, 1rem + 2vw, 2.5rem) with a 16px root, a 320px viewport gives 22.4px, 768px gives 31.36px, and 1280px gives 40px (the upper bound). Neither rem nor clamp guarantees WCAG compliance: the viewport term can decrease as the page is zoomed. Test actual resizing and content clipping. [6]

Container units: cqi instead of vw

<div class="widget-shell">
  <h2 class="widget-title">Summary</h2>
</div>
.widget-shell {
  container-type: inline-size;
}

.widget-title {
  margin: 0;
  font-size: 1.25rem;
  font-size: clamp(1rem, 0.75rem + 2cqi, 1.5rem);
  line-height: 1.3;
  overflow-wrap: anywhere;
}

cqi is 1% of the inline size of the nearest eligible query container. Without an eligible container it falls back to the small viewport unit for that axis. [2][4]

With a 400px container, 2cqi is 8px. With a 16px root, the example's preferred value of 0.75rem + 2cqi is 20px; at 800px it is capped at 1.5rem, or 24px. Establish container-type: inline-size on the wrapper and use the unit on a descendant. The declaration before clamp is a simple fallback for browsers without cqi support.

Media queries and relative units

<div class="dashboard">
  <aside>Filters</aside>
  <section>Results</section>
</div>
.dashboard {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 1rem;
}

@media (min-width: 48em) {
  .dashboard {
    grid-template-columns: minmax(0, 1fr) minmax(0, 2fr);
  }
}

In media queries, em and rem refer to the initial font-size value, not the font size assigned to html by your stylesheet. [5]

With a 16px initial font size, 48em is 768px; with a 20px browser preference it is 960px. Setting html { font-size: 20px; } alone does not move the breakpoint if the initial value remains 16px. Choose breakpoints where content stops fitting rather than by device names.

Accessibility and text resizing

<div class="action-panel">
  <button class="action-panel__button" type="button">
    Download the detailed sales report
  </button>
</div>
.action-panel {
  box-sizing: border-box;
  max-inline-size: 100%;
  padding: 1rem;
  font-size: 1rem;
  line-height: 1.5;
}

.action-panel__button {
  box-sizing: border-box;
  max-inline-size: 100%;
  min-block-size: 2.75rem;
  padding: 0.5em 0.75em;
  border: 1px solid #475569;
  border-radius: 0.375rem;
  background: #1e3a5f;
  color: #fff;
  font: inherit;
  white-space: normal;
  overflow-wrap: anywhere;
}

.action-panel__button:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: -4px;
}

WCAG 2.2 requires text to be resizable to 200% without loss of content or functionality, and ordinary horizontal-language content to reflow at a width equivalent to 320 CSS pixels, subject to defined exceptions. Units alone do not establish compliance. [6][7]

Use relative font sizes, unitless line-height, flexible dimensions and wrapping for long values. The example button inherits the font, permits text wrapping and avoids a fixed height. Pixels do not prevent full-page zoom; relative units can better accommodate font-size preferences. Test both mechanisms and keyboard navigation. [8]

There is no universal best CSS unit. Use pixels for small details, rem for a global scale, em for component proportions, percentages for available space, and viewport or container units for dimensions that depend on their surroundings. Evaluate typography and layout with changed user preferences and browser zoom, not only at the default settings.

Tools for this topic

CSS CSS Units CSS Pixels rem em Viewport Units Fluid Typography Container Query Units Responsive Design Accessibility

Frequently asked questions

How many pixels is 1rem?

It equals the computed root font size: 16px with a 16px root, or 20px with a 20px root. [2]

Is px always a bad choice?

No. It works for details such as thin borders. It does not necessarily equal a physical pixel and does not by itself ensure an accessible layout. [1]

Are rem and em the same?

No. rem uses the root font size, whereas em uses the local font size, or the inherited value for font-size itself. [2]

What should replace 100vh on mobile?

For a dynamic full-height layout, use min-block-size: 100dvh with a 100vh fallback. For a stable small viewport, consider 100svh. [2]

Does clamp() guarantee accessibility?

No. Verify resizing to 200% and reflow at 320 CSS pixels, subject to WCAG exceptions. [6][7]

Sources and references

  1. W3C, CSS Values and Units Module Level 4↩1↩2↩3↩4↩5↩6
  2. MDN, CSS <length>↩1↩2↩3↩4↩5↩6↩7↩8↩9↩10
  3. MDN, clamp()↩
  4. MDN, Container query length units↩
  5. W3C, Media Queries Level 4↩1↩2
  6. W3C WAI, Understanding Resize Text↩1↩2↩3
  7. W3C WAI, Understanding Reflow↩1↩2
  8. MDN, line-height↩

Was this helpful?

Get new articles by email

One short email per new Learning article. No spam, unsubscribe in one click.

We only use your email to send new articles. No third-party sharing.

Back to Learning