Units
An overview of length units in CSS: from fixed pixels, through font-relative em and rem, to viewport, container, and grid units.
Example
#.lesson {
max-width: 45ch;
padding: 1rem 1.25rem;
border: 1px solid #3f3f46;
border-radius: 0.75rem;
}
.lesson h3 {
margin: 0 0 0.5rem;
font-size: 1.25rem;
}
.lesson .tag {
display: inline-block;
margin-bottom: 0.5rem;
padding: 0.25em 0.75em;
border-radius: 1em;
background: #3b0764;
font-size: 0.75rem;
}<article class="lesson">
<span class="tag">Module 2</span>
<h3>Relative units</h3>
<p style="margin: 0; color: #a1a1aa;">Width in ch, spacing in rem, the label in em, and a thin border in pixels.</p>
</article>Each unit has its job here: ch limits the text width, rem keeps spacing and sizes consistent, em scales the label with its font, and px draws a border that should always be thin.
Definition and usage
#Every length in CSS is a number with a unit, for example 16px, 1.5rem, or 50%. The exception is zero, for which the unit can be omitted. Units are divided into absolute ones, which always mean the same (in practice only px), and relative ones, computed from something else: the font size, the parent, the browser window, or a container.
em and rem depend on the font. 1rem is the font size of the root element <html> (16px by default), the same throughout the document. 1em is the font size of the current element, so it changes along with it, and in the font-size property itself it is computed from the parent’s font. That is why em compounds in nested elements, and rem doesn’t.
Viewport units are computed from the viewport: 1vw is 1% of its width, 1vh 1% of its height. On phones the window height changes when the address bar hides, which is why variants appeared: svh (small viewport, bar visible), lvh (large viewport, bar hidden), and dvh (dynamic, changes continuously). For full-screen sections, 100svh or 100dvh are better than 100vh.
There are also units for special tasks. ch is the width of the character “0” in the current font, so it is good for limiting line length. fr works only in CSS Grid and means a share of the free space. cqi is 1% of the width of a container with container-type. Percentages are usually computed from the parent, but whether from its width or height depends on the property: width from the width, height from the height, and padding and margin always from the width.
Syntax
#<number><unit>
absolute: px
font-relative: em, rem, ch
relative to parent, viewport, or container: %, vw, vh, svh, lvh, dvh, cqi
grid: frUnits
#a fixed value
The CSS pixel is an absolute unit. The tile is always 160 px, regardless of the parent and the window. On high-density screens one CSS pixel is several physical pixels, so sizes look similar on every device.the element’s font
Spacing and rounding inemgrow with the button’s font. Just changefont-sizeand the proportions are preserved.the html element’s font
Both labels are 20 px (1.25 × 16 px), even though their parents have different fonts.remis always computed from the<html>element, so it doesn’t compound.the parent’s size
Percentages are computed from the parent. The bars are a quarter, a half, and the whole width of the dashed frame.window width and height
50vwis half the window width, here of the preview frame, not of the dashed box, which is why the first tile is wider than half of the box.50vhis half the window height.window height on a phone
On a phone the address bar hides and shows.svhcomputes the height with the bar visible (the smallest),lvhwith it hidden (the largest), anddvhchanges continuously. The preview frame has no bar, so all three tiles are equal.width of the 0 character
The width of the character “0” in the current font. The paragraph is no wider than 24 such characters, and the postcode field fits exactly as much as needed.free space in a grid
frworks only in a grid and means a share of the free space. The middle column gets two shares, and the side ones one each.container width
Drag the frame’s bottom-right corner. The text is 9% of the container width, so it grows and shrinks with it, not with the window.
Units in practice
#px
.demo { width: 160px; }em
em grow with the button’s font. Just change font-size and the proportions are preserved..demo {
padding: 0.5em 1em;
border: 0;
border-radius: 0.5em;
background: #7c3aed;
color: #fff;
}rem
rem is always computed from the <html> element, so it doesn’t compound..demo {
font-size: 1.25rem;
font-weight: 700;
color: #a78bfa;
}%
.a { width: 25%; }
.b { width: 50%; }
.c { width: 100%; }vw / vh
50vw is half the window width, here of the preview frame, not of the dashed box, which is why the first tile is wider than half of the box. 50vh is half the window height..vw { width: 50vw; }
.vh { height: 50vh; }dvh / svh / lvh
svh computes the height with the bar visible (the smallest), lvh with it hidden (the largest), and dvh changes continuously. The preview frame has no bar, so all three tiles are equal..s { height: 50svh; }
.d { height: 50dvh; }
.l { height: 50lvh; }ch
.demo { max-width: 24ch; margin: 0 0 10px; }
.zip { width: 8ch; font-family: monospace; }fr
fr works only in a grid and means a share of the free space. The middle column gets two shares, and the side ones one each..demo {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
gap: 8px;
}cqi
.cq { container-type: inline-size; }
.cq p { font-size: 9cqi; }More examples
#.hero {
min-height: 100vh;
min-height: 100svh;
}The first declaration is a fallback for older browsers, the second overrides it where svh works. The section fills the phone screen and its bottom doesn’t hide under the address bar. This example has no preview, because the frame fits its height to the content.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Give font sizes in
rem, notpx. The text then responds to the font size setting in the browser, which people with low vision rely on. - Beware of
100vw: it also includes the vertical scrollbar, so an element can extend beyond the page and cause horizontal scrolling. For full width100%is usually enough. - Limit the line length, for example
max-width: 65ch. Lines of a few dozen characters are the most comfortable to read.
Related entries
#- calc()Computes a value from a mathematical expression and lets you combine different units, for example percentages with pixels.
- clamp()Keeps a value between a minimum and a maximum, and lets it grow smoothly in between. The basis of fluid typography.
- font-sizeSets the font size.
- widthSets the width of an element.
- @containerApplies styles based on the size of a container instead of the whole window. A component adapts to the space it gets in the layout.
- repeat()Repeats a pattern of grid columns or rows. With auto-fill or auto-fit and minmax() it creates a responsive grid without media queries.
Spotted a mistake or missing an example? Write to us.