Use absolute, relative, viewport, and container units.
A unit gives a numeric length its meaning. In 20px, the number is 20 and the unit is px. The useful question is not only “how large is the number?” but also “what does this number measure against?”
Write a length without a space between its number and unit. For zero lengths, the unit can usually be omitted, as in margin: 0.
Use the reference that should control the size:
| If the size should follow... | Start with... | Why |
|---|---|---|
| A fixed CSS measurement | px | The value stays the same CSS length as its context changes. |
| A font size | em or rem | The value scales from a local or root font size. |
| The browser viewport | vw, vh, svh, or dvh | The value follows the viewport rather than a component. |
| An ancestor container’s inline size | cqi | The value follows a size-query container around the component. |
Percentages are another relative option, but their reference depends on the property. The preceding lesson’s rule still applies: ask “a percentage of what?” before calculating one.
px.note {
border: 2px solid green;
}px is an absolute length unit in CSS. A CSS pixel is an abstract CSS measurement, so it is not necessarily one physical screen pixel; device resolution and zoom affect how it is displayed.
Other absolute units include cm, mm, in, and pt. They have fixed relationships to each other in CSS.
em and remem: Uses the element's computed font size for most properties. On font-size itself, it uses the inherited font size instead.rem: Uses the root element's font size, which is the font size on html in an HTML document.html {
font-size: 16px;
}
.theme {
font-size: 20px;
}
.note {
font-size: 1.5em;
padding: 1em;
margin-top: 1rem;
}For a .note directly inside .theme:
1.5 × 20px = 30px.1em uses the note's own 30-pixel font size, so it is 30px.1rem uses the root's font size, so it is 16px.These results use the font sizes set in the example. A visitor’s root font size may differ. Nested em font sizes can multiply at each level, while rem always refers to the root.
Raise the parent’s font size to 24px and keep the root at 16px. Which two measurements change?
html {
font-size: 16px;
}
.theme {
font-size: 24px;
}
.note {
font-size: 1.5em;
padding: 1em;
margin-top: 1rem;
}The note’s font size becomes 36px (1.5 × 24px). Its padding becomes 36px too, because 1em now reads that new font size. The top margin stays at 16px because its reference is the root.
The viewport is the area through which the browser displays the document:
1vw: One percent of the viewport width.1vh: One percent of the large viewport height in current browsers.1svh: One percent of the small viewport height, accounting for expanded browser controls.1dvh: One percent of the dynamic viewport height, which can change as browser controls appear or disappear..banner {
width: 50vw;
min-height: 100dvh;
}At a viewport width of 1000 pixels, 50vw is 500 pixels. Dynamic height can resize as mobile browser controls appear or disappear, while the small viewport gives a stable reference when those controls are expanded. Viewport units and percentages have different bases.
Check the reference before calculating:
25vw is 250px.50vh is 300px.The calculation is always “number × 1% of the named reference.”
Container units measure a size-query container around the element. 1cqi is 1% of that container’s inline size, which is its width in horizontal writing:
.panel {
container-type: inline-size;
}
.panel h2 {
font-size: 5cqi;
}The panel establishes the size-query container. If its inline size is 400 pixels, 5cqi gives the heading a 20-pixel font size. This responds to the panel, rather than the whole browser window.
If there is no suitable container, these units fall back to the small viewport size for that direction. You will use containers more fully in responsive design.
The important sequence is: create the container, identify the axis, then calculate the percentage. In the example, inline-size is the width because the writing direction is horizontal. 5cqi therefore means 5% × 400px = 20px.
Try it yourself
HTML + CSS
em can follow a local font size; rem follows the root font size.