Sub topic 2
A quick refresher on length units
This chapter assumes you know basic CSS syntax and the cascade, the box model, and the width property. It reviews the length units that min(), max(), and clamp() combine later in the tutorial.
Absolute and relative units
A length unit tells the browser how big a value is. An absolute unit means the same size everywhere. A relative unit is resolved against something else, such as a font size, a parent box, or the viewport.
The only absolute unit you will use often is px. It is a CSS pixel, which is not always one device pixel. Everything else in this tutorial is relative.
| Unit | Relative to | Typical use |
|---|---|---|
| `px` | Nothing (fixed) | Borders, hairlines, shadows |
| `em` | Font size of the current element (for `font-size`, the parent's) | Padding that scales with text |
| `rem` | Font size of the root `html` element | Type and spacing scales |
| `%` | A property-specific reference, often the parent's size | Widths inside a container |
| `vw` / `vh` | 1% of the viewport width / height | Sizes tied to the window |
| `ch` | Width of the "0" glyph in the current font | Line lengths for text |
What each unit looks at
Each relative unit reads a different input. That input decides how the value reacts when the page changes.
Why the reference point matters
Fluid layouts mix units that react to different things. A rem value changes when the user changes their browser font size. A vw value changes when the window is resized. A percentage changes when the parent changes.
Here is a short example that uses all three:
html {
font-size: 100%; /* respects the user's browser setting, usually 16px */
}
.card {
width: 50%; /* half of the parent's content width */
padding: 1.5rem; /* 24px when the root size is 16px */
font-size: 1.25rem; /* 20px when the root size is 16px */
margin-top: 2vw; /* 2% of the viewport width */
}Percentages are the trickiest. For width, a percentage refers to the parent's width. For padding and margin, it also refers to the parent's width, even for vertical sides. For height, it refers to the parent's height, and that only works if the parent's height is defined.
Choosing rem over px for type
Prefer rem for font sizes and most spacing. A px font size ignores the user's preferred text size, but a rem size scales with it. Use px only when you want a value that must not scale.
Setting html { font-size: 62.5%; } to make 1rem equal 10px is a common trick, but it makes the arithmetic less clear. This tutorial leaves the root size alone and assumes 1rem is 16px.
Viewport units in brief
vw and vh are percentages of the viewport. 100vw is the full viewport width. On some systems it includes the scrollbar, so a box set to 100vw can cause a horizontal scrollbar. Use 100% for "fill the parent" and keep vw for sizes that should follow the window.
Exercise
A .box sits in a parent that is 800px wide. The root font size is 16px and the viewport is 1000px wide. What are the computed sizes of width: 25%, padding-left: 2rem, and margin-left: 3vw?
Show answer
The width is 200px (25% of 800px). The left padding is 32px (2 × 16px). The left margin is 30px (3% of 1000px).