CSS Units Reference
CSS length units grouped by type: absolute, font-relative, viewport (svh, lvh, dvh), container query units and percentages.
By the tools4free.dev team · Last reviewed 4 December 2026
How to use this reference
Every size in CSS is a number plus a unit, such as 16px, 1.5rem or 50vw. The unit decides what the number is measured against. Some units are fixed, so 1in is always 96px. Others are relative, so 1em depends on a font size, 1vw depends on the browser window and 1cqw depends on a parent container. Picking the right unit is what makes a layout adapt to different screens, user font settings and component sizes.
The tables below group the units by what they are relative to: absolute units, font-relative units, viewport units, container query units and percentages. Each row gives the meaning and a note on when to use it. A short section at the end covers the units for angles, time and image resolution, which you need for transforms, animations and media queries. The rules are defined in the CSS Values and Units specification, and the browser behaviour is documented by MDN.
If you only need a quick rule of thumb: use rem for font sizes and most spacing so that text respects the reader's browser settings, use % or fr for flexible widths, use ch to limit line length, use dvh or svh for full-screen sections on phones, and use px for things that should not scale, such as hairline borders. To build a size that scales smoothly between two limits, combine units in clamp(). The clamp generator does the arithmetic, and the guide on fluid typography with CSS clamp() explains the formula. To turn pixel values into rem, use the unit converter for general conversions, or divide by the root font size (16px in most browsers by default).
The units you will use most
rem for text and spacing, % for widths, vw/dvh for viewport-based sizes, ch for line length, px for fixed details such as borders, and cqw/cqi inside components.Absolute length units
Absolute units are fixed to each other. They are anchored to the CSS pixel, and the others are defined from it. They suit print layouts and details that must not scale. MDN warns that absolute lengths can cause accessibility problems for font sizes, because they do not scale with the user's settings, so prefer em or rem for text.
| Unit | Meaning | Notes |
|---|---|---|
px | One CSS pixel. 1px = 1in / 96. | On high-resolution screens one CSS pixel covers several device pixels. |
cm | One centimetre. 1cm = 96px / 2.54. | Mostly useful for print. |
mm | One millimetre. 1mm = 1cm / 10. | Mostly useful for print. |
Q | A quarter of a millimetre. 1Q = 1cm / 40. | Rare. The letter is upper case. |
in | One inch. 1in = 2.54cm = 96px. | Mostly useful for print. |
pc | One pica. 1pc = 12pt = 1in / 6. | Print typography. |
pt | One point. 1pt = 1in / 72. | Print typography. |
Font-relative units
These units are measured against the font. Child elements inherit the computed value of a parent, not the relative expression itself.
| Unit | Meaning | Notes |
|---|---|---|
em | The computed font-size of the element. On the font-size property itself it means the inherited font size. | Good for spacing that should scale with a component's own text. Nested em values multiply. |
rem | The font size of the root element, usually html. A common browser default is 16px, but users can change it. | The usual choice for text and layout spacing. No compounding. |
ex | The x-height of the element's font. In many fonts 1ex is about 0.5em. | Rare. |
cap | The cap height, the nominal height of capital letters, of the element's font. | Aligning icons with capital letters. |
ch | The advance width of the glyph "0" (zero) in the element's font. | Setting a readable line length, such as max-width: 65ch. |
ic | The advance of the CJK water ideograph (U+6C34) in the font. | Sizing CJK text. |
lh | The computed line-height of the element, as an absolute length. | Spacing in whole lines for vertical rhythm. |
rex, rcap, rch, ric, rlh | The same as ex, cap, ch, ic and lh, but measured on the root element's font. | Fixed reference points that do not change inside nested components. |
An example of compounding: if html is 16px and a list has font-size: 0.9em, a nested list inside it computes to 0.9 × 0.9 × 16px, about 13px. With 0.9rem every level stays at 14.4px.
Viewport units
Viewport units are percentages of the browser window (more exactly, the initial containing block). 1vw is 1% of the viewport width, so on an 800px-wide window 50vw is 400px, and on a 300px-tall window 70vh is 210px. The trouble on phones is that the visible height changes as the browser's address bar and toolbars slide in and out. To handle that, CSS defines three sets of viewport units, marked by a prefix.
Viewport unit sets
| Units | Meaning | When to use |
|---|---|---|
vw, vh, vi, vb, vmin, vmax | The default viewport units. They are currently equivalent to the large viewport units (lvw, lvh and so on). | Fine on desktop. On mobile, 100vh can be taller than the visible area. |
svw, svh, svi, svb, svmin, svmax | Small viewport: the smallest viewport, with browser interfaces expanded. Sizes are fixed unless the viewport itself is resized. | Content fits fully visible, but there can be empty space when the interface retracts. |
lvw, lvh, lvi, lvb, lvmin, lvmax | Large viewport: the largest viewport, with browser interfaces retracted. Sizes are fixed. | Fills the screen when the interface is hidden, but content may be hidden when it shows. |
dvw, dvh, dvi, dvb, dvmin, dvmax | Dynamic viewport: resizes automatically as the interface expands and retracts. | Always fits exactly, but the size changes while scrolling, which can cause resizing and a performance cost. |
What each viewport axis unit measures
| Unit | Meaning | Notes |
|---|---|---|
vw | 1% of the viewport width. | Fluid type and widths, usually inside clamp(). |
vh | 1% of the viewport height. | Full-height sections. Prefer svh or dvh on mobile. |
vi | 1% of the size in the direction of the root element's inline axis. | Writing-mode aware layouts. |
vb | 1% of the size in the direction of the root element's block axis. | Writing-mode aware layouts. |
vmin | 1% of the smaller of the viewport width and height. | Sizes that stay in proportion in both orientations. |
vmax | 1% of the larger of the viewport width and height. | Large decorative shapes. |
A common pattern for a full-screen hero is min-height: 100vh; followed by min-height: 100dvh;. Browsers that do not understand dvh ignore the second line and keep the first. Note that viewport lengths are not valid inside @page declaration blocks.
Container query units
Container query units are relative to a query container, an ancestor you have set up with container-type. They let a component size itself from the space it is given rather than from the whole window, which makes it reusable in a sidebar and in a wide column. Set up a container with container-type: inline-size (query the inline dimension) or container-type: size (query both dimensions). The default value, normal, does not create a size container.
.card-wrap { container-type: inline-size; }
.card h2 { font-size: clamp(1rem, 4cqi, 2rem); }
Container query units
| Unit | Meaning | Notes |
|---|---|---|
cqw | 1% of the query container's width. | For example, 50cqw in an 800px-wide container is 400px. |
cqh | 1% of the query container's height. | Needs a container with size containment. |
cqi | 1% of the query container's inline size. | Writing-mode aware. The usual choice with inline-size containers. |
cqb | 1% of the query container's block size. | Writing-mode aware. |
cqmin | 1% of the smaller of the container's inline and block size. | Proportional sizing in either shape. |
cqmax | 1% of the larger of the container's inline and block size. | Large decorative parts. |
If no suitable container exists for the element, the container query unit falls back to the small viewport unit for that axis (sv*), so the layout still has a sensible value.
Percentages
A percentage is a number followed by %. It is always a percentage of something, and what that something is depends on the property. This is the most common source of surprises, so check the reference for the property you are using.
What a percentage refers to
| Property | Percentage of | Notes |
|---|---|---|
width | The width of the containing block. | Also used for max-width and min-width. |
height | The height of the containing block. | Only works when the containing block has a definite height. |
padding | The inline size (width in horizontal writing) of the containing block, even for top and bottom padding. | Margins follow the same rule for percentages. |
font-size | The font size of the parent element. | 200% of 18px is 36px. |
line-height | The element's own font size. | A unitless number such as 1.5 is usually safer, because it scales with each element's font size. |
Angle, time and resolution units
These units are not lengths, but you will meet them in transform, animation, transition and media queries. As with lengths, the unit cannot be left out, except that a plain 0 is allowed for lengths. For resolution, 0 without a unit is invalid.
Other units
| Unit | Meaning | Type |
|---|---|---|
deg | Degrees. A full circle is 360deg. | Angle |
grad | Gradians. A full circle is 400grad. | Angle |
rad | Radians. A full circle is 2π rad, about 6.2832rad. | Angle |
turn | Turns. A full circle is 1turn. | Angle. Handy for rotations: rotate(0.25turn). |
s | Seconds, for example 1.5s. | Time, for transitions and animations |
ms | Milliseconds, for example 150ms. | Time |
dpi | Dots per inch. | Resolution, for media queries |
dpcm | Dots per centimetre. | Resolution |
dppx, x | Dots per px unit. 1dppx equals 96dpi. x is an alias. | Resolution. (min-resolution: 2dppx) targets high-density screens. |
Choosing a unit
- Font sizes:
rem, orclamp()with aremminimum and maximum. Avoidpxfor body text so that users who raise their browser font size are respected. - Spacing:
remfor page-level spacing,eminside components that should scale with their text. - Widths:
%,frin grid, orchfor text blocks. Usemax-widthrather than a fixed width. - Full-screen sections:
svh,lvhordvh, depending on whether you can tolerate empty space, hidden content or resizing. - Components: container query units, so a card behaves the same wherever it is placed.
- Borders and shadows:
px.
Common mistakes
- Using vw alone for text.
font-size: 5vwignores the user's font settings and can become unreadably small or huge. Put it insideclamp()withremlimits. - Using 100vh on phones. The default
vhequals the large viewport, so the bottom of the page can sit behind the browser toolbar. Usesvhordvh. - Expecting em to be predictable when nested. It compounds from parent to child. Use
remwhen you want a fixed reference. - Percentage heights that do nothing. A percentage height needs a parent with a definite height.
- Forgetting the container. Container query units fall back to small viewport units when there is no query container. Set
container-typefirst. - Leaving out the unit.
margin: 10is invalid. Only0may be written without a unit for lengths.