Length Units: px, em, rem, ch, lh and the Other Font-Relative Units
What CSS lengths mean: absolute units and the CSS pixel, em and why it behaves differently in font-size, rem and root-relative units, ch, ex, cap, ic and lh, how user font settings and zoom affect each, and rules for choosing units.
kvi5hnuCode Sprout team member9 min readOn this page
A length is a number with a unit: 16px, 1.5rem, 60ch. CSS has more than thirty length units. They fall into three groups: absolute units, which are fixed; font-relative units, which depend on a font size or a font's measurements; and viewport units, which depend on the size of the window (article 25). Choosing well between them is what makes a design scale with the user's settings and the container it's in.
Absolute units and the CSS pixel
| Unit | Name | Equals |
|---|---|---|
px | pixel | 1/96 in |
pt | point | 1/72 in (so 12pt = 16px) |
pc | pica | 12pt |
in | inch | 96px |
cm, mm | centimeter, millimeter | 1in = 2.54cm = 25.4mm |
Q | quarter-millimeter | 0.25mm |
All absolute units are fixed multiples of each other, and they're all anchored to the CSS pixel. A CSS pixel isn't a device pixel. It's a reference pixel: roughly the size that one pixel on a 96-dpi screen appears at arm's length. A phone with three device pixels for every CSS pixel draws a 1px line three device pixels thick, and a browser zoomed to 200% draws each CSS pixel twice as large.
That's why 1in on a screen is rarely a real inch: it's 96 CSS pixels, whatever size those have on the device. Physical units like cm, mm, in and pt are useful mainly for print stylesheets (article 81), where the browser does know the paper size.
em: relative to the font size
1em equals the element's own computed font-size. A button with font-size: 20px and padding: 0.5em 1em gets 10px and 20px of padding. Change the font size, and the padding follows:
.button { padding: 0.5em 1em; border-radius: 0.3em; }
.button.large { font-size: 1.25rem; } /* everything scales with the text */
That makes em the right unit for spacing that should stay proportional to the text of a component: padding in buttons and tags, gaps between an icon and its label, the size of an inline icon (width: 1em).
The exception: em in font-size
An element can't size its font relative to itself, so in the font-size property, em refers to the parent's font size. font-size: 1.5em means 1.5 times the inherited size.
When an element uses em for both its font size and other properties, the font size is computed first, then used for the rest:
.slogan { font-size: 1.25em; padding: 1em; }
/* parent 16px: font-size 20px, padding 20px (1em of the element's own 20px) */
Compounding
Because each level multiplies its parent's size, em font sizes compound in nested elements:
body { font-family: system-ui, sans-serif; display: flex; flex-wrap: wrap; gap: 1.5rem; }
ul { padding-left: 0.9rem; }
.em ul { font-size: 0.85em; }
.rem ul { font-size: 0.85rem; }
h3 { font-size: 1rem; margin: 0; }
Each nested em list is 85% of the one around it: 13.6px, 11.6px, 9.8px, 8.4px. The rem lists stay at 13.6px at every level.

The measured sizes show the compounding, and the buttons show the useful side of em: one padding rule gives every size proportional spacing.
rem: relative to the root
1rem equals the font size of the root element, <html>. It's the same value everywhere in the document, so it doesn't compound. If nothing sets a font size on <html>, 1rem is the user's default font size, usually 16px.
That makes rem the usual choice for font sizes and for spacing that should be consistent across the page:
h1 { font-size: 2.25rem; }
h2 { font-size: 1.5rem; }
.card { padding: 1.5rem; margin-block-end: 2rem; }
Users' font settings
Browsers let people change the default font size in their settings, and many do, especially people with low vision. That setting changes the root font size, so it scales everything sized in rem and em. Lengths in px ignore it.
| Unit for font sizes | Scales with page zoom | Scales with the default font size setting |
|---|---|---|
px | yes | no |
rem, em, % | yes | yes |
So don't set font sizes in pixels, and don't set the root font size in pixels either: html { font-size: 16px; } overrides the user's choice for the whole page. If you want a different base size, use a percentage or rem of the default: html { font-size: 112.5%; } gives 18px for a user with the 16px default, and scales for everyone else.
An old habit was html { font-size: 62.5%; }, which makes 1rem 10px so that pixel values are easy to convert (1.6rem = 16px). It works, but body text then needs its font size set again (body { font-size: 1.6rem; }), it breaks third-party components that assume a normal rem, and the arithmetic isn't needed anymore. Think in rem directly, or let calc() do conversions.
Units based on font measurements
Some units measure the font itself:
| Unit | Equals | Typical use |
|---|---|---|
ex | the x-height: height of a lowercase "x" | aligning with lowercase text |
cap | the cap height: height of capital letters | sizing icons to match capitals |
ch | the advance width of the digit "0" | text measure: max-width: 65ch |
ic | the advance width of the CJK ideograph "水" | measure in Chinese, Japanese and Korean text |
lh | the element's computed line-height | spacing in whole lines: margin-block: 1lh |
Each has a root-relative version that uses the root element's font instead: rex, rcap, rch, ric and rlh.
article { max-width: 65ch; } /* about 65 characters per line */
.icon { height: 1cap; vertical-align: 0; } /* as tall as the capital letters */
.stack > * + * { margin-block-start: 1lh; } /* one line of space between blocks */
textarea { min-height: 4lh; } /* room for four lines of text */

The bars are sized only with the units, yet they line up with the glyphs. They're measured from the font, so they change when the font does. The ch bar fits the digits exactly, while proportional letters overshoot or fall short.
chis an approximation of a character's width. In a monospace font every character is1chwide, but in proportional fonts letters such as "m" are wider and "i" narrower. For limiting line length, that's close enough: readable lines are about 45 to 75 characters.lhdepends online-height. With the defaultline-height: normal, the value comes from the font's metrics. Article 93 useslhfor vertical rhythm.- All these units, including the root-relative versions, are Baseline since January 2026, when Firefox 147 added
rex,rch,rcapandric.
Units in media queries
In media queries, em and rem are based on the initial font size, the user's default, not on any font-size you set in CSS. @media (width >= 40em) therefore means 640px for a user with the 16px default, and adapts if they choose larger text, which is why many developers write breakpoints in em (article 76).
Choosing units
There's no single correct unit, but these defaults work well:
| What you're sizing | Good default |
|---|---|
| font sizes | rem (or em inside a component meant to scale) |
| padding and gaps inside a component | em if it should follow the component's text, otherwise rem |
| margins between page sections | rem |
| line length | ch |
| borders, hairlines, shadows | px |
| layout widths | percentages, fr, min(), or container units, rather than fixed lengths |
| media query breakpoints | em or rem, or px if your team prefers |
A component sized entirely in em can be rescaled with one declaration: give its root a font-size in rem, use em for everything inside, and a modifier class only needs to change that one font size.
What's current
icis Baseline since 2022, andcap,lhandrlhsince 2023. The root-relativerex,rch,rcapandricbecame Baseline in January 2026.- Container query units (
cqi,cqband others) are relative to a container's size (article 78), and viewport units to the window (article 25). Q, the quarter-millimeter, is in all browsers, though rarely used outside print.
Common questions
Is 1px always one device pixel?
No. A CSS pixel is a reference unit. On high-density screens, one CSS pixel covers several device pixels, and zooming changes how many.
What's the difference between em and rem?
em is relative to the element's own font size (the parent's, in font-size itself), so it changes in nested elements. rem is always relative to the root element's font size.
Should I use rem for everything?
It's a safe default for font sizes and page-level spacing. Use em where spacing should scale with the component's own text, px for borders, and ch for line length.
Why are my nested lists getting smaller and smaller?
They have an em (or percentage) font size below 1, which compounds at each level. Use rem, or set nested lists back to 1em.
Is font-size: 62.5% on html a good idea?
It's not harmful to accessibility if done with a percentage, but it changes the meaning of rem everywhere, including in third-party CSS. Most teams now keep the default root size and work in rem values like 0.875rem.
Summary
- Absolute units are fixed multiples of the CSS pixel, a reference unit, not a device pixel. Physical units are mainly for print.
emis relative to the element's font size, or to the parent's infont-size, and compounds in nested elements.remis relative to the root element's font size and doesn't compound.- Lengths in
remandemfollow the user's default font size;pxfont sizes don't. Never set the root font size in pixels. ex,cap,ch,icandlhmeasure the font or line height, and each has a root-relative version.emin media queries uses the user's default font size.- Use
remfor fonts,emfor component-relative spacing,pxfor borders andchfor measure.
Practice questions
A paragraph has the rule below, and its parent's font size is 16px. What are its font size and margin?
p {
font-size: 1.25em;
margin-bottom: 1em;
}
Answer
Font size 20px (1.25 × the parent's 16px). Margin 20px, because in properties other than font-size, em refers to the element's own font size, which is now 20px.
Why might this rule be a problem for some users?
html { font-size: 14px; }
Answer
It overrides the user's default font size setting, so people who set a larger default in their browser get 14px text anyway. Use a relative value, such as 87.5%, which gives 14px at the 16px default and scales with the user's choice.
Write CSS that limits paragraphs to about 60 characters per line and leaves exactly one line of space between them.
Answer
p { max-width: 60ch; margin-block: 0 1lh; }
A tag component uses the rule below. How do you make a large version with one declaration?
.tag {
font-size: 0.875rem;
padding: 0.25em 0.75em;
border-radius: 0.5em;
}
Answer
Change only its font size, for example .tag.large { font-size: 1.125rem; }. Padding and radius are in em, so they scale with it.
Previous: CSS Value Types: Keywords, Strings, URLs, Numbers and Percentages
Next: Viewport Units: vw, vh, and the Small, Large and Dynamic Viewport Units

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.