Sub topic 2
A quick refresher on length units
This chapter assumes you know basic CSS syntax, the box model, and the width property. It reviews the length units you will combine inside min(), max(), and clamp() in later chapters. The goal is to know what each unit is measured against, because that decides how a value behaves when the container, viewport, or user's font settings change.
Why units matter for intrinsic layouts
A comparison function takes several values and picks one. Those values often use different units, such as 100% against 40rem. The function can only choose well if you know what each one resolves to. A % value depends on a parent box. A rem value depends on the root font size. A vw value depends on the viewport. When a layout misbehaves, the cause is usually a unit measuring something other than what you assumed.
Units fall into two groups:
- Absolute units have a fixed size that does not depend on context. The common one is
px. - Relative units are computed from something else: a font size, a parent box, or the viewport.
Pixels: the absolute unit
A CSS pixel (px) is a reference unit defined as 1/96 of an inch. It is not a physical screen pixel. On a high-density display, one CSS pixel may be drawn with four or more device pixels. Browser zoom scales CSS pixels, so a 300px box looks larger when you zoom in, and it still takes up 300 CSS pixels in layout terms.
Other absolute units exist (pt, cm, in), but they are rarely useful on screens. Use px for things that should not scale with text, such as hairline borders and shadows.
Font-relative units
Font-relative units tie a length to a font size. This keeps spacing and widths in proportion to the text.
rem
rem means "root em". It equals the computed font-size of the root element (html). Browsers default this to 16px, so 1rem is normally 16px and 40rem is 640px. If the user changes their default font size in browser settings, every rem value changes with it.
em
em is relative to the font size of the element it is used on. There is one exception: when you use em in the font-size property itself, it refers to the parent's font size, because the element's own size is what you are defining.
This makes em useful for components that should scale as a unit, such as a button whose padding grows with its text. It also makes em compound when you use it for font sizes in nested elements:
html { font-size: 100%; } /* 16px by default */
ul { font-size: 1.25em; } /* 20px */
ul ul { font-size: 1.25em; } /* 25px: 1.25 x 20px */
ul ul ul { font-size: 1.25em; } /* 31.25px: 1.25 x 25px */Each nested list is larger than the one before it. rem avoids this because it always refers back to the root.
ch
ch is the advance width of the "0" glyph in the element's font. It approximates the width of one character, so it suits readable line lengths:
article p {
max-width: 65ch;
}This limits a paragraph to about 65 characters per line, whatever the font size.
Percentages
A % value is not a unit with one meaning. Its reference depends on the property, which is a frequent source of confusion.
| Property | Percentage is relative to |
|---|---|
| `width`, `max-width`, `min-width` | Width of the containing block |
| `height` | Height of the containing block, if that height is definite |
| `padding`, `margin` (all sides) | Width of the containing block |
| `font-size` | Parent's font size |
| `line-height` | The element's own font size |
| `grid-template-columns` | Width of the grid container's content box |
Two rows deserve extra attention:
- Vertical padding and margin as percentages use the containing block's width, not its height.
padding-top: 10%in a 600px-wide parent is 60px, however tall the parent is. - A percentage
heightdoes nothing useful unless the parent has a height that does not depend on its content. If the parent's height isauto, the percentage behaves as if it wereauto.
Viewport units
Viewport units measure the browser window's layout viewport, not any parent box:
vwis 1% of the viewport width.vhis 1% of the viewport height.vminandvmaxare 1% of the smaller or larger of the two dimensions.
Viewport units are what make type and spacing fluid, so they are central to the later chapters on clamp(). They have gotchas, though.
The scrollbar problem
100vw includes the width of a classic, always-visible vertical scrollbar. If a page has such a scrollbar, a box set to width: 100vw is wider than the space available and causes horizontal scrolling:
/* Can overflow by the scrollbar's width (often 15-17px) */
.banner {
width: 100vw;
}
/* Fills the available width without overflow */
.banner-safe {
width: 100%;
}Overlay scrollbars, common on phones and macOS, take no layout space, so the bug may not appear on your own machine. When you want "as wide as the parent", prefer % or auto.
Dynamic viewport units on mobile
On mobile browsers the address bar slides away as the user scrolls, so the visible viewport height changes. Plain vh is defined by the largest case, which means 100vh can be taller than what is visible at first. Modern CSS adds three variants:
| Unit | Meaning |
|---|---|
| `svh` | Small viewport: address bar and toolbars shown |
| `lvh` | Large viewport: toolbars retracted (the same as `vh`) |
| `dvh` | Dynamic viewport: updates as toolbars appear and disappear |
.hero {
min-height: 100vh; /* fallback for older browsers */
min-height: 100dvh; /* used where supported */
}The same pattern exists for width (svw, lvw, dvw), though width rarely changes on the fly.
Seeing what each unit follows
The diagram shows where each family of units gets its size. When you later mix these units in one function, ask which of these sources each argument depends on.
Units and user preferences
Units also differ in how they respond when the user changes how they read the page:
- Browser zoom scales everything, including
px, because it changes the size of the CSS pixel itself. Zooming to 200% also halves the CSS-pixel width of the viewport, which can trigger layout changes. - A larger default font size (a browser setting) changes the root font size.
remandemfollow it.pxdoes not. - Viewport units follow the window size, not the user's font settings.
A font size written as 16px ignores the user's preferred default size. Writing it as 1rem respects it. The accessibility chapter returns to this in detail. For now, treat rem as the default unit for font sizes and for widths tied to text, and avoid sizing text in px or only in vw.
Choosing a unit
These rules of thumb cover most cases:
- Use
remfor font sizes and for sizes that should scale with the user's text preference. - Use
emwhen a component's inner spacing should scale with its own font size. - Use
%when a box should depend on its parent, and check the table above for what the percentage refers to. - Use
vwandvh(ordvh) when a value should depend on the window, and watch for scrollbar and mobile toolbar effects. - Use
chfor line lengths. - Use
pxfor thin decorative details.
Mixing these in one declaration is the next step. A plain width: 100% - 2rem is invalid because CSS does not allow arithmetic outside math functions. The next chapters introduce calc() and the comparison functions that make such expressions possible.
Common errors
- Compounding
em. Nested elements keep growing. Switch toremfor font sizes. width: 100vwwith a scrollbar. The page scrolls sideways. Use100%.- Percentage height with an auto-height parent. The height has no effect. Give the parent a definite height, or use
min-heightwith a viewport unit. - Expecting
padding-top: 10%to follow height. It follows the containing block's width. - Sizing text in
px. The text ignores the user's default font size.
Exercise
A page has a vertical scrollbar that takes up 15px of layout width. A box inside a 1000px-wide, non-scrolling parent has padding-top: 5%, and the browser's default font size is 16px. What is the box's top padding in pixels, and what happens if the user raises their default font size to 20px and the page uses max-width: 40rem on the same box?
Show answer
The top padding is 50px, because percentage padding is measured against the containing block's width (5% of 1000px), not its height. The scrollbar does not change this value. With max-width: 40rem, the maximum width grows from 640px (40 x 16px) to 800px (40 x 20px), because rem follows the root font size. The 5% padding stays 50px, because it depends on the parent's width and not on font size.