Sub topic 6
Absolute Units and Why px Is Common
This chapter assumes you know basic HTML, CSS selectors and the cascade, and the box model (margin, padding, border). You should also know how to link or embed a stylesheet.
What makes a unit absolute
An absolute unit is a length that always means the same size in CSS. It doesn't depend on a parent element, a font, or the viewport. A relative unit such as em or % is resolved against something else. Relative units are covered in the next chapter.
CSS defines these absolute units:
| Unit | Name | Value in CSS pixels |
|---|---|---|
| `px` | pixel | 1 |
| `in` | inch | 96 |
| `cm` | centimeter | about 37.8 |
| `mm` | millimeter | about 3.78 |
| `Q` | quarter-millimeter | about 0.94 |
| `pt` | point (1/72 inch) | 1.333 |
| `pc` | pica (12 points) | 16 |
These ratios are fixed by the specification. 1in is always exactly 96px, and 1cm is always 96px / 2.54. Because of this, the other units are just different names for the same lengths.
A CSS pixel is not a screen pixel
A px in CSS is a reference unit, not one physical dot on the display. Browsers map it to device pixels using the device pixel ratio. On a high-density screen with a ratio of 2, 1px is drawn with a 2 by 2 block of device pixels. The result is that a 16px box looks about the same size on a standard monitor and on a high-density phone.
The same logic explains why 1in rarely measures an inch on your screen. The browser keeps the 96-to-1 ratio, but the physical size depends on the display and the zoom level. Real-world units only match reality in print.
Why px is the common choice
Most CSS you read uses px for these reasons:
- It is predictable.
border: 1pxis the same thickness wherever it appears. - It is easy to reason about. Design tools such as Figma export values in px.
- It suits details that should not scale with text, such as borders, outlines, and shadows.
- It works well with images and icons that have a fixed pixel size.
.card {
width: 320px;
border: 1px solid #c9ced6;
border-radius: 8px;
box-shadow: 0 2px 8px rgb(0 0 0 / 0.15);
outline-offset: 2px;
}Each of these values describes a fine visual detail, so a fixed length is the right fit.
Where absolute units cause problems
Setting text size in px is the classic mistake. Browsers let users choose a default font size, and many people with low vision raise it. A root size in rem follows that setting, as you will see in the next chapter. A size in px does not.
/* Ignores the user's default font size */
body {
font-size: 14px;
}
/* Respects it */
body {
font-size: 1rem;
}Browser zoom still scales px text, so px is not completely inaccessible. But zoom is a separate action from the font-size preference, and not every user knows to use it. Fixed widths cause a second problem. A width: 960px container overflows on a 375px-wide phone and forces horizontal scrolling.
/* Overflows narrow screens */
.layout {
width: 960px;
}
/* Fixed ceiling, flexible below it */
.layout {
max-width: 960px;
width: 100%;
}The other absolute units
The physical units mostly matter in print stylesheets. In @media print, points are a natural fit because printed type is traditionally measured in them.
@media print {
body {
font-size: 11pt;
margin: 2cm;
}
}On screen, cm, mm, in, pc, and Q rarely add anything over px. Pick one absolute unit per context and stay with it. Mixing pt and px in the same rules makes values harder to compare.
Exercise
A stylesheet sets body { font-size: 14px; } and .wrapper { width: 1200px; }. Name one accessibility problem and one layout problem, and give a fix for each.
Show answer
The 14px body size ignores the user's default font size setting, so change it to 1rem (or another rem value). The fixed 1200px width overflows screens narrower than 1200px and causes horizontal scrolling, so use max-width: 1200px; width: 100%;.
Exercise
How many CSS pixels are in 2in, and how many in 12pt?
Show answer
2in is 192px (2 × 96). 12pt is 16px (12 × 96 / 72).