CSS: Length Units

By Xah Lee. Date: . Last updated: .

Pixel Unit

px

Pixel. Practically, 1/16 of default font size of a device.

Relative Units

lh

The computed value of the line-height property of the element on which it is used.

πŸ›‘ WARNING: the size of actual line height may differ based on their content, e.g. contain words with large font size.

rlh

Line height of the Root Element.

vw

1% of the view port width.

vh

1% of the view port height.

vmin

Smaller of vw or vh

vmax

Larger of vw or vh

%

Percent. Relative to parent element. Exact meaning depends on the element it is used. γ€”see CSS: Percentage Value〕

Font Based Units

cap

Height of capital letters of the current element's font.

rcap

Height of capital letters of the Root Element.

ic

Height of Chinese character for water ζ°΄ of the current element.

ic

Height of Chinese character for water ζ°΄ of the Root Element.

em

Font size of parent element.

rem

Font size of Root Element. Equivalent to 16px.

ch

Width of digit 0 of current font. If writing direction is vertical, it is the height.

rch

Width of digit 0 of font size of Root Element. If writing direction is vertical, it is the height.

ex

Height of lowercase letter x of current font.

rex

Height of lowercase letter x of font size of Root Element.

Physical Units

cm

Centimeter.

mm

Millimeter.

in

Inch.

pt

1/72 of a inch.

pc

1/6 of a inch.

Which unit to use

πŸ’‘ TIP: I recommend use only the px unit, and the following:

Do not use

Example: CSS Length Unit

div {margin:1rem}

πŸ›‘ WARNING: There must be no space before the CSS unit, and a unit is required, except when the value is 0, else it's syntax error.

rem, Root Font Size

The length of rem is:

The computed value of font-size of the Root Element .

When used as value on font-size property of the Root Element, rem refer to the property's initial value. (in browsers, it's 16px).

πŸ’‘ TIP: think of rem as the default font size, and it's basically always 16px.

em

The length of em is:

ex, x-height

ex is equal to current font's β€œx-height”, usually the height of the letter x. Typically about half of em, depending on current font.

Like em, the length of 1ex depends on parent's font-size.

γ€”see CSS: Meaning of Font Size〕

ch

ch is equal to the box width of the digit 0 (ZERO) character of the current font. It is about the same as half of em.

css spec length ch  2022-11-06 4RTnr
CSS Values and Units Module Level 4 Editor's Draft, 12 October 2022. https://w3c.github.io/csswg-drafts/css-values/#ch

vw, vh, vmin, vmax

vw

Equal to 1% of the initial containing block (the view port). It does NOT include the scrollbar width. When browser window changes size, vw also changes.

πŸ’‘ TIP: One great use of vw is to specify height as a percentage of width. You can't do this with percentage. γ€”see CSS: Fixed Aspect Ratio〕 In fact, it's useful to replace almost all CSS: Percentage Value by vw, because the meaning of percentage is different for each CSS property.

vh

Equal to 1% of the height of the initial containing block.

vmin

Equal to the smaller of vw or vh.

vmax

Equal to the larger of vw or vh.

cm, mm, in, pc, pt

cm

centimeter. 10 mm β†’ ~0.3937 inch; ~37.795 px

Physical units such as cm is useful for specifying printed style. For example:

@media print { table {font-size: 4cm} }

γ€”see CSS: Media Query〕

mm

millimeter. 0.1 cm β†’ ~3.78 px

in

Inch. 6 pc. 72 pt. 96 px. β†’ 2.54 cm

pc

1/6 inch. 12 pt. 16 px. β€œpc” stands for β€œpica”. β†’ ~0.423 cm

pt

1/72 inch. (~0.0138 inch) ~1.33 px. β€œpt” stands for β€œpoint”. β†’ 0.3527 mm

What is CSS's Default Unit?

CSS Basics