
CSS offers several ways to measure text, spacing, and layout. Choose a unit by deciding what should happen when the font size, container, or viewport changes.
4. CSS units of measurement
4.1. Absolute units
Absolute lengths have fixed relationships in CSS. A CSS pixel can be rendered using several device pixels on a high-density screen. Physical units are particularly useful in print styles.
| Unit | Meaning |
|---|---|
px | CSS pixel |
in | Inch, equivalent to 96 CSS pixels |
cm | Centimeter, 2.54 cm per inch |
mm | Millimeter |
pt | Point, 1/72 of an inch |
pc | Pica, equal to 12 points |
4.2. Relative units and values
| Unit | Reference |
|---|---|
em | When setting font-size, relative to the inherited font size. For most other properties, relative to the element’s computed font size. |
rem | Relative to the root element’s font size. |
% | The reference depends on the property. For width, it usually means a percentage of the containing block’s width. |
vw, vh | One unit represents 1% of the viewport width or height, respectively. |
fr | A fraction of the available space in a CSS Grid track definition. |
4.3. Choosing a unit
- Use
remfor typography tied to a shared scale, oremwhen an element’s dimensions should grow with its text. - Use
pxfor details where a fixed CSS length makes sense, such as a 1px border. - Use a percentage for flexible widths, and viewport units when the viewport is the intended reference. On mobile, browser toolbars can affect available height.
- Use
frto divide space among grid tracks.grid-template-columns: 1fr 2frgives the second flexible track twice the share of the first, subject to track sizing constraints. - Use
cm,mm,in, orptwhen print dimensions matter.
If the reference font size is 16px, divide the target size by 16 to convert it to em. Your browser’s default, or its user settings, may specify another size.
| Pixels | em | Percentage |
|---|---|---|
| 12 | 0.75 | 75% |
| 14 | 0.875 | 87.5% |
| 16 | 1 | 100% |
| 18 | 1.125 | 112.5% |
| 20 | 1.25 | 125% |
Practice exercise
With a 12px reference font size, how many em make 16px?
Show the calculation
Divide 16 by 12. The result is approximately 1.333em.
Compare the paragraphs below using different browser font-size settings. Then open the original comparison in CodePen.
<p style="font-size: 16px">16px paragraph</p>
<p style="font-size: 12px">12px paragraph</p>
<p style="font-size: 1em">1em paragraph</p>
<p style="font-size: 1.5em">1.5em paragraph</p>
<p style="font-size: 0.5in">Half-inch paragraph</p>
<p style="font-size: 8mm">8mm paragraph</p>
<p style="font-size: 12pt">12pt paragraph</p>