Design
px vs em vs rem in CSS: Sizing Rules and Examples
Compare CSS pixels, element-relative em, and root-relative rem. Understand nested sizing, font preferences, browser zoom, and conversion assumptions.
Sizing model
CSS units carry different context
px, em, and rem respond to different parts of the page, so a numeric conversion is only one step.
Inputs
value, root size, and context
Rule
em can nest; rem follows root
Check
zoom, fonts, and responsive layout
A CSS pixel is an absolute CSS length unit. For font-size, em refers to the parent's computed font size; on most other properties it refers to the element's own font size. Rem refers to the root element's font size. The choice affects how a component responds to inherited sizes and user preferences, but no unit alone establishes accessibility.
Use the PX to REM converter or REM to PX converter when you know the root size. For related layout utilities, browse the design tools.
px, em, and rem compared
| Unit | Reference | Useful example | What to check |
|---|---|---|---|
| px | CSS reference pixel | A thin border | Browser zoom and high-density rendering |
| em on font-size | Parent's computed font size | A nested text scale | Multiplication through nested elements |
| em on padding | Element's computed font size | A button's internal spacing | The button's actual font size |
| rem | Root element's computed font size | A shared spacing or type scale | The computed root and user font preference |
A CSS pixel is not necessarily one physical display pixel. Browsers map CSS lengths to the display. The CSS Values and Units specification defines these reference relationships.
How to convert pixels and rem
Divide pixels by the root font size to obtain rem. Multiply rem by the root font size to obtain pixels.
| Root size | Pixel value | REM value |
|---|---|---|
| 16px | 16px | 1rem |
| 16px | 24px | 1.5rem |
| 16px | 32px | 2rem |
| 20px | 24px | 1.2rem |
| 20px | 30px | 1.5rem |
The base is an input, not a universal constant. A browser default is often 16px, but stylesheets and user preferences can change it. Inspect the root element's computed font size when matching an existing page:
/* Keep the root relative so user browser font preferences are respected */
:root {
font-size: 100%; /* 16px in default desktop browser settings */
}
.article-body {
font-size: 1.125rem; /* 18px at a 16px root */
line-height: 1.65;
max-width: 65ch;
}
Why nested em font sizes multiply
Start with a 16px parent and give each nested list item a font size of 1.25em. The first level becomes 20px, the next 25px, and the next 31.25px. Each child uses its parent's computed size.
/* Compounding trap: nested .menu-list elements multiply font-size */
.menu-list {
font-size: 1.25em; /* Level 1 = 20px, Level 2 = 25px, Level 3 = 31.25px */
}
/* Predictable scale: rem anchors every level to the root font-size */
.menu-list-stable {
font-size: 1.25rem; /* Every nesting level stays 20px at a 16px root */
}
With a 16px root, a font size of 1.25rem remains 20px at each level. This can be useful when a nested list should keep the same text size. It is a design decision rather than a rule that em should never be used.
Em-based padding solves a different problem. If a button has 0.75em vertical padding, its padding follows its own font size. Increasing that font size also increases the internal spacing without requiring a separate padding value:
.btn {
font-size: 1rem;
padding: 0.625em 1.25em; /* Scales proportionally with .btn font-size */
border: 1px solid currentColor; /* Hairline border stays crisp in px */
border-radius: 0.375rem;
}
.btn-large {
font-size: 1.25rem; /* Padding automatically grows without a second rule */
}
Combining rem with fluid viewport units (clamp())
When building responsive headings that scale smoothly between mobile and desktop viewports, avoid pure viewport units (font-size: 4vw) because they ignore user font-size preferences and stop responding to zoom at extreme widths. Combining rem with vw inside clamp() preserves user scaling while bounding the minimum and maximum size:
.hero-heading {
/* Minimum: 1.75rem (28px), Fluid: 1.2rem + 2.2vw, Maximum: 3rem (48px) */
font-size: clamp(1.75rem, 1.2rem + 2.2vw, 3rem);
}
Font preferences and browser zoom are different
Rem responds to the root font size. If the root follows the browser's preferred font size, rem-based text follows that preference too. If the stylesheet fixes the root in pixels, rem no longer has that same relationship to the preference.
Browser zoom is separate: it can enlarge content sized in pixels as well as content sized in relative units. Avoid claims that pixel-sized text cannot be enlarged at all. The important outcome is whether a reader can increase text size and still read content and operate controls.
WCAG's Resize Text guidance addresses resizing without loss of content or functionality. Test the actual layout rather than treating a unit conversion as a conformance check.
Choosing units for a component
- Use rem when values should share a root-based scale across typography and layout spacing.
- Use em when padding, margins, or inline icons should follow the local component's text size.
- Use px when an explicit CSS length fits the requirement (such as
1pxborders or shadow offsets), then check the result under zoom. - Avoid fixed heights that clip enlarged text, regardless of which unit sets the font.
- Check long labels and nested content before copying a pattern throughout a project.
A practical sizing review
- Inspect the computed root and component font sizes in DevTools.
- Record the intended relationship: root-based (
rem), local-text-based (em), or explicit length (px). - Convert values using the PX to REM converter or REM to PX converter rather than assuming 16px everywhere.
- Check text enlargement, wrapping, overflow, and access to controls.
- Compare narrow and wide layouts with realistic content.
When prototyping component styles alongside unit conversions, use the CSS Grid generator and Flexbox generator for track and gap layouts, the border radius generator and box shadow generator for surface geometry, and the aspect ratio calculator for responsive media containers. For deeper guidance on Flexbox, Grid, specificity, and visual compositing, see our CSS layout, specificity, and visual effects guide. For color tokens that pair with your type scale, read the HEX, RGB, HSL, and OKLCH color guide.
Keep reading
Related posts
Preparing Web Images, QR Codes, and Alt Text Audits in the Browser
Reduce image byte weight with HTMLCanvasElement, choose resilient QR code error correction, and audit missing alt attributes directly in the browser.
Read postCSS Layout, Selector Specificity, and Visual Effects Guide
Choose between Flexbox and CSS Grid, compute Selectors Level 4 specificity accurately, and ship shadows, filters, gradients, and clip paths cleanly.
Read postHEX vs RGB vs HSL vs OKLCH: CSS Color Formats Explained
Compare CSS color formats, conversion rules, gamut limits, and interpolation. Choose a notation for editing colors and check contrast separately.
Read post