CSS: Units

By Xah Lee. Date: . Last updated: .

Example of length in your browser:


Note, there must be no space before the CSS unit. Example:

div {margin:1em}

「%」, Percent Value

% is percentage.

Percentage is not technically a CSS length unit.

div {width:5%}

Meaning of percent value is dependent on the specific property.

Percentage values are always relative to another value, for example a length. Each property that allows percentages also defines the value to which the percentage refers. The value may be that of another property for the same element, a property for an ancestor element, or a value of the formatting context (e.g., the width of a containing block).

When a percentage value is set for a property of the root element and the percentage is defined as referring to the inherited value of some property, the resultant value is the percentage times the initial value of that property.

Syntax and basic data types#percentage-units

「px」, Reference Pixel

px is defined as “reference pixel”, which is a visual angle of 0.0213 degrees.

px is not the display's physical pixel size.

For a monitor with 96 dot-per-inch monitor, popular in 1999, px is a physical pixel.

px is practically 1/16 of default font size of a device.

css reference pixel
CSS Reference Pixel

[see CSS: What's a CSS Pixel? What's Reference Pixel?]

「rem」, Root Font Size

rem → the computed value of font-size on the root element.

Effectively, 1rem is 16px.

When specified on the font-size property of the root element, the rem units refer to the property's initial value.

「em」, M-box Font Size

em → Length of the m-box of the font size of current element.

Note: em is NOT the width of “M”.

The length of em is:

Note: Child elements do not inherit the relative values specified for their parent; they inherit the computed values.

[see CSS: What's Computed Style?]

font size em height
The blue box is the font size, also known as em height. The actual height of letter M, is different depending on font. 〔font_size_em_height.svg

[see Meaning of Font Size and Em Height]

「ex」, font x-height

ex → A font's “x-height”, usually the height of the letter x. Typically about half of em, depending on current font.

typography line terms
typography line terms

[see Meaning of Font Size and Em Height]


ch → roughly equal to the width of the digit 0 (ZERO) character of the current font. Roughly half of em.

The ch unit is from CSS3, and is not widely supported.

[“ch” is] Equal to the used advance measure of the “0” (ZERO, U+0030) glyph found in the font used to render it. In the cases where it is impossible or impractical to determine the measure of the “0” glyph, a value of 0.5em must be assumed.

Syntax and basic data types#length-units

「vw」, 「vh」, 「vmin」, 「vmax」

vwEqual 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.
vhEqual to 1% of the height of the initial containing block.
vminEqual to the smaller of vw or vh.
vmaxEqual to the larger of vw or vh.

These units are not fully supported by IE11 as of

「cm」, 「mm」, 「in」

cmcentimeter. 10 mm~0.3937 inch; ~37.795 px
mmmillimeter. 0.1 cm~3.78 px
inInch. 6 pc. 72 pt. 96 px.2.54 cm
pc1/6 inch. 12 pt. 16 px. “pc” stands for “pica”.~0.423 cm
pt1/72 inch. (~0.0138 inch) ~1.33 px. “pt” stands for “point”.0.3527 mm

Units like cm is useful for specifying printed style. For example:

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

[see CSS: Media Query (Responsive Design)]

What's CSS's Default Unit?

CSS doesn't have a default unit. Omitting unit is syntax error.



Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon