Sub topic 6
Absolute Units and Why px Is Common
This chapter assumes you know basic HTML structure, CSS selectors and the cascade, the box model (margin, padding, border), and how to link or embed a stylesheet. It covers the absolute length units in CSS, what "absolute" really means, why px dominates real stylesheets, and where px causes accessibility problems.
What a length unit is
A CSS length is a number followed by a unit, such as 16px or 2cm. A unit with no number, or a number with no unit, is invalid for most properties. The one exception is 0, which needs no unit.
CSS splits length units into two groups:
- Absolute units map to a fixed physical or reference size. They do not depend on any other element's size.
- Relative units are computed from something else, such as a font size or the viewport. The next chapters cover these.
This chapter covers the first group.
The absolute units
CSS defines these absolute units:
| Unit | Name | Definition |
|---|---|---|
| `px` | pixel | 1/96 of an inch (the CSS reference pixel) |
| `cm` | centimeter | 96px / 2.54 |
| `mm` | millimeter | 1/10 of a centimeter |
| `Q` | quarter-millimeter | 1/40 of a centimeter |
| `in` | inch | 96px |
| `pt` | point | 1/72 of an inch |
| `pc` | pica | 12pt, or 16px |
The units are tied to each other by fixed ratios. In every browser, 1in equals 96px, 1in equals 2.54cm, and 1in equals 72pt. You can check the ratios with a small stylesheet:
.ruler {
width: 1in; /* computes to 96px */
height: 2.54cm; /* also computes to 96px */
padding: 72pt; /* also 96px on each side */
}All three declarations resolve to the same computed length. Browsers convert every absolute unit to px internally, so getComputedStyle reports lengths in pixels.
Why "absolute" does not mean "physical"
The names suggest a physical ruler, but on a screen 1in is rarely one real inch. CSS defines the px as a reference pixel, not a hardware dot. The reference pixel is the visual angle of one pixel on a 96 dpi display held at arm's length. The browser then maps that reference to the actual device.
This has two consequences.
First, a CSS pixel and a device pixel (one physical dot on the screen) are different things. On a high-density display, one CSS pixel may be drawn with 2 or 3 device pixels in each direction. The ratio is the device pixel ratio. A phone with a ratio of 3 draws a 100px wide box using 300 device pixels across.
Second, 1cm on screen is not guaranteed to measure one centimeter with a ruler. The units are exact only relative to each other. Physical accuracy is realistic only for print, where the browser knows the paper size.
Why px is so common
Most production stylesheets use px for many values. There are practical reasons.
- It is predictable.
1pxis1pxregardless of the parent's font size or the user's settings. You can reason about a value without tracing the cascade. - It suits fine detail. Borders, outlines, hairlines, and shadows are typically 1px to a few pixels. These are decorations, not content that should grow with text.
- It matches design tools. Design files are measured in pixels, so values copy straight into CSS.
- It stays sharp. The browser renders a
1pxborder using the device's pixel ratio, so thin lines remain thin on dense screens. - Convention.
pt,pc,in,cm,mm, andQsuit print. Few teams use them on screen, sopxis the only absolute unit most people write.
Here is a rule where px is a good fit:
.card {
border: 1px solid #c8cdd4;
border-radius: 8px;
box-shadow: 0 2px 8px rgb(0 0 0 / 0.15);
}None of these values should scale with the user's text size. A border should stay a hairline, and a corner radius is a visual detail.
The accessibility problem with px
The trouble starts when px is used for font sizes. Browsers let users set a default font size in their settings. People with low vision often raise it, for example from 16px to 24px. That preference is exposed to CSS through relative units such as rem and em, and through percentages.
A font size in px ignores that setting. Compare these two rules:
/* Ignores the user's browser font-size setting */
.note-fixed {
font-size: 14px;
}
/* Follows the user's browser font-size setting */
.note-scalable {
font-size: 0.875rem;
}If the user's default is 16px, both give 14px of text. If the default is 24px, .note-fixed stays at 14px, while .note-scalable becomes 21px.
Modern browsers have page zoom (Ctrl/Cmd and +), which scales px values too, so px text can still be enlarged that way. But zoom is a different action from changing a default size. It affects the whole page, may trigger horizontal scrolling, and some users never use it. WCAG success criterion 1.4.4 (Resize Text) requires text to be resizable up to 200% without loss of content, and relying on px for text makes passing harder to verify, especially with text-only resize.
Where px is also risky
Fixed lengths can break layouts, not just text.
- Fixed widths on containers.
width: 960pxforces horizontal scrolling on a 375px phone. Prefermax-width, which lets the element shrink. - Fixed heights on text containers. A
height: 40pxbox clips its text when the user enlarges the font or the text wraps. Prefermin-heightor let content size the box. - Spacing that should track text. Padding in
pxinside a button does not grow when the button's font grows. Padding inemdoes.
This rule shows a safer pattern:
.panel {
max-width: 960px; /* upper limit, not a requirement */
min-height: 40px; /* floor, not a ceiling */
border: 1px solid currentColor;
}max-width and min-height keep the pixel values as limits and still allow the box to adapt.
Choosing between px and a relative unit
Use this decision guide when deciding on a unit for a property:
| Property or job | Typical unit | Reason |
|---|---|---|
| Borders and outlines | `px` | Should stay thin |
| Box shadows | `px` | Visual detail, not content |
| Small corner radius | `px` | Decorative |
| Font size | `rem` | Honors user preference |
| Padding tied to text | `em` | Scales with the element's font size |
| Content column width | `ch` or `rem` in `max-width` | Tracks text size |
| Print stylesheet sizes | `pt`, `cm`, `mm` | Physical output |
A diagram summarizes the same decision:
Print stylesheets and the other absolute units
The physical units earn their place in print. Inside an @media print block, you can size things in points and centimeters, which map to the real paper:
@media print {
body {
font-size: 11pt;
margin: 2cm;
}
h1 {
font-size: 18pt;
}
}Point sizes are the long-standing convention in typography, so 11pt body text will look familiar to anyone who has used a word processor. Here the absolute units are the right tool because the output medium has a fixed physical size.
Common mistakes
- Expecting
1into equal a real inch on screen. It is 96 CSS pixels, drawn however the device chooses. - Mixing
ptinto screen CSS.12ptis16px. The equivalence is correct but the unit adds confusion for readers who think in pixels. - Using
pxforfont-sizeglobally. This overrides the user's default and is the most common accessibility regression from this chapter. - Setting
html { font-size: 62.5%; }as a shortcut. It makes1remequal 10px, which is friendly to compute. It still scales with the user's setting, but it is easy to forget and can produce tiny text if you later usepx-like numbers such as1.2remfor body text. Prefer leaving the root at100%. - Fixed pixel
heighton text containers. Text overflows when it grows.
Check the computed value
You can confirm the conversions in the browser console. Select an element and read its computed style:
const probe = document.createElement("div");
probe.style.width = "1in";
document.body.appendChild(probe);
console.log(getComputedStyle(probe).width); // "96px"
probe.remove();This prints 96px in every browser. It shows that absolute units are fixed conversions to pixels, not measurements of your screen.
Exercise
A stylesheet sets body { font-size: 14px; } and .button { border: 1px solid; padding: 12px 24px; }. A user changes their browser's default font size to 24px. Which of these values keeps the user's preference in mind, and how would you fix the one that does not?
Show answer
None of them follow the user's setting. The body font size of 14px is the real problem because it ignores the 24px default, so change it to font-size: 1rem (or leave it unset). The 1px border is fine as px. The button padding of 12px 24px does not grow with the text, so padding: 0.75em 1.5em would scale with the button's font size.
Exercise
How many CSS pixels are in 2in, and is that the same as 2 physical inches on your screen?
Show answer
2in is 192px, because 1in equals 96px. It is not guaranteed to be 2 physical inches on screen. A CSS pixel is a reference pixel, and the browser maps it to device pixels based on the display, so the ratio between units is exact but the physical size is not.
Exercise
Why is max-width: 960px usually better than width: 960px for a page container?
Show answer
width: 960px forces the box to be 960px wide even on a 375px phone, which causes horizontal scrolling. max-width: 960px only sets an upper limit, so the box shrinks to fit narrower screens.