The lengths in CSS can be specified using two main types of units: relative and absolute
Relative Units
Relative length units represent the measurement in terms of other distance. They can be based on font, viewport or query
Relative Units Based on Font
These units are based on the font size of the element/it’s parent. If the unit is prefixed with r
character the unit is relative to the font-size of root
element instead
cap/rcap
- nominal height of capital lettersch/rch
- width of0
glyph, assumes0.5em
x1em
by defaultem/rem
- calculated font size of the elementex/rex
- height of lowercase letters (x
) in the fontic/ric
- measure of the water ideograph glyphlh/rlh
- equal to computedline-height
Relative Units Based on Viewport
These units define values by percentages relative to the visible area of the document. These units represent the viewport based on browser’s default viewport size, to use other viewport sizes prefix the unit with s
for small, l
for large or d
for dynamic
vh
- percentage of viewport’s initial containing block heightvw
- percentage of viewport’s initial containing block widthvmax
- the largest ofvw
andvh
vmin
- the smallest ofvw
andvh
vb
- percentage of the size of initial containing block in the direction ofroot
’s block axisvi
- percentage of the size of initial containing block in the direction ofroot
’s inline axis
Relative Units Based on Container Query
These units define values relative to a query container.
cqw
- percentage of width of query containercqh
- percentage of height of query containercqi
- percentage of query container’s inline sizecqb
- percentage of query container’s block sizecqmin
- the smallest ofcqi
andcqb
cqmax
- the largest ofcqi
andcqb
Absolute Units
Absolute length units represent a physical measurement. For low-dpi devices units are relative to a reference pixel and might not always be equal to their actual physical counterparts.
px
- one device pixel for screen displays. For high-resolution devices it might represent more device pixelscm
-96px/2.54
mm
-1cm/10
Q
- one quarter of a millimetre1cm/40
in
-96px
pc
-1in/6
pt
-1in/72