Skip to main content

What relative units of measurement exist in CSS?

Relative units in CSS are units that depend on the font size, the browser window size, or other parameters of the element/window. They are flexible and help create responsive layouts.


Groups of relative units

1. Relative to font

UnitRelative to
emparent's font size
remroot element's font size (html)
exthe height of the letter x in the current font
chthe width of the 0 character in the current font

Example:

css
p { font-size: 2em; } /* 2 times larger than the parent */

2. Relative to window size (viewport)

UnitWhat it means
vw1vw = 1% of window width
vh1vh = 1% of window height
vmin1% of the smaller side (width or height)
vmax1% of the larger side

Used for responsive blocks and fonts, for example:

css
h1 { font-size: 5vw; } /* font size depends on window width */

3. Percentage

UnitDescription
%depends on the parent element (for width, height, spacing, and so on)

Summary

GroupUnitsWhen to use
Font-basedem, rem, ex, chfor text, spacing, responsive interfaces
Browser windowvw, vh, vmin, vmaxresponsive blocks/headings
Percentage%sizes dependent on the container

The most commonly used in practice: rem, em, %, vw, vh.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

What relative units of measurement exist in CSS?: CSS Interview Question