Back to all guides

CSS Units Guide: When to Use PX, REM, EM, and Viewport Units in Modern Web Design

Master responsive typography, layout stability, and accessibility by understanding the mathematical relationships between CSS measurement units.

DK
Dipanshu Kumar
Founder & Software Architect at Tool Yaar
Updated: 2026-11-068 min read

Absolute vs Relative Units in Modern CSS

In CSS layout and web typography, sizing values are split into two major categories: absolute length units (such as `px`, `cm`, `pt`, `in`) and relative length units (such as `rem`, `em`, `%`, `vw`, `vh`, `ch`).

Absolute units are fixed to a physical measurement or hardware reference pixel. In CSS, 1 pixel (`1px`) is formally defined as 1/96th of an inch. While pixels are intuitive and predictable for borders and decorative shadows, relying on fixed pixels for text sizing and container padding causes significant accessibility and responsive layout breakdowns.

Why Hardcoding PX Harms Web Accessibility

Web accessibility guidelines (WCAG 2.2) require that web applications accommodate users with visual impairments who increase their browser’s default font size. By default, major browsers set the root font size to 16 pixels.

However, millions of low-vision users configure their operating system or browser font preference to 20px, 24px, or larger. When a website declares `font-size: 16px` on body elements using hardcoded pixels, it forcefully overrides the user’s deliberate browser preference, rendering text unreadably small for visually impaired visitors.

The Accessibility Imperative

Always use `rem` for typography and spacing. When the user scales their browser font setting, `rem`-based typography scales proportionally and harmoniously.

REM (Root EM): The Standard for Responsive Typography

The `rem` unit stands for "Root EM". It calculates its computed value relative to the font-size of the root HTML element (`<html>`).

Assuming standard browser defaults (where root is 16px):

• 1rem = 16px

• 1.25rem = 20px

• 1.5rem = 24px

• 2rem = 32px

Because all `rem` values across your entire application reference a single global anchor point (the root element), your UI components remain predictable and immune to unintended compounding scaling bugs.

EM: Component-Relative Sizing and Compounding Hazards

Unlike `rem`, which always looks at the root element, `em` is relative to the font-size of the immediate parent element (or the element itself when calculating padding and margins).

This makes `em` ideal for scalable component tokens: if you build a button with `padding: 0.5em 1.0em`, the padding automatically expands or shrinks whenever you modify that specific button’s font-size.

However, using `em` for nested font sizes triggers the notorious "compounding inheritance" trap: if a `<ul>` has `font-size: 0.9em`, three levels of nested lists will shrink successively (0.9 × 0.9 × 0.9 = 0.729em), rendering innermost items microscopic.

Modern Viewport Units: Solving the Mobile Address Bar (dvh/svh)

Historically, CSS `100vh` was intended to represent 100% of the viewport height. However, on mobile browsers (Safari on iOS, Chrome on Android), dynamic browser chrome (the URL address bar and bottom navigation toolbar) slides in and out as the user scrolls.

Using classic `100vh` caused bottom modal buttons to be cut off beneath the mobile address bar. Modern CSS introduced three specialized viewport units to solve this:

• svh (Small Viewport Height): Height when the dynamic address bar is fully expanded.

• lvh (Large Viewport Height): Height when the address bar is retracted/hidden.

• dvh (Dynamic Viewport Height): Automatically adjusts in real-time as the address bar animates on scroll.

Comprehensive CSS Unit Comparison Matrix

Reference table for selecting the optimal unit for each styling requirement:

UnitRelative ToCompounding RiskBest Recommended Use Case
pxFixed hardware reference pixelNone1px hairline borders, box shadows, canvas dimensions
remRoot <html> element font sizeNone (safe)All typography, global margins, layout padding, gap
emImmediate parent element font sizeHigh (on font-size)Component-internal padding, scalable badge icons
%Parent container dimensionContextualFluid column widths, modal dialog max-widths
vw / vhFull viewport width / heightNoneFull-screen hero banners, background split layouts
dvhDynamic mobile viewport heightNoneMobile app shell layouts, full-height slide-over drawers

Frequently Asked Questions

Q:Should I set html { font-size: 62.5% } to make 1rem equal 10px?

While popular in legacy codebases, the 62.5% trick can cause third-party UI component libraries (which assume a 16px baseline) to render unexpectedly small. Sticking to standard 16px math (0.25rem = 4px, 0.5rem = 8px, 1rem = 16px) is modern best practice.

Q:When is it acceptable to use pixels in modern CSS?

Pixels are completely fine for subtle decorative properties that should never scale with font preferences, such as a `1px solid #e2e8f0` border, a `4px` border-radius, or box-shadow offsets.

Q:What is the "ch" unit in CSS?

1ch equals the advance measure width of the character "0" in the current font. It is the premier unit for limiting reading line length (e.g., `max-width: 65ch` creates optimal editorial readability).

Q:How do you convert pixels to rem quickly?

Divide the pixel value by 16. For example: 24px / 16 = 1.5rem. 32px / 16 = 2rem. 12px / 16 = 0.75rem.

Interactive Tools

Try the Free Tools Mentioned in this Guide

DK
About the Author

Dipanshu Kumar

Dipanshu is the founder and lead software architect of Tool Yaar. He designs high-performance client-side web utilities, WebAssembly engines, and offline Android tools focused on zero-upload user privacy and universal digital accessibility.