CSS: Units

By Xah Lee. Date: . Last updated: .

Those marked with 👍 are good unit to use for web design.

Percent. Relative to parent tag. Exact meaning depends on the element it is used.
👍 Pixel. visual angle of 0.0213 degrees. Or, 1/16 of default font size. Or, same as a physical pixel on a display with resolution of 96-pixels per inch (popular in 1990s), viewed from arm's length.
👍 1% of the view port width.
👍 1% of the view port height.
Smaller of vw or vh
Larger of vw or vh
👍 Root (default) font size. Equivalent to 16px.
Width of digit 0 of current font.
Font size of parent element.
Height of lowercase letter x. About half of em, depending on font.
1/72 of a inch.
1/6 of a inch.

Note, 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.


div {margin:1rem}

Percent Value

% is percentage.


div {width:5%}

Percentage is not technically considered a “length unit”.

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

[see HTML: the Root Element]

Tip: don't use percentage value, because its meanings is complex. Use vw, vh, vmin, vmax instead.

px, Reference Pixel

css reference pixel
CSS Reference Pixel

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

This means, the larger the screen, the larger is 1px.

px is not the display's physical pixel size.

For a monitor with 96 dot-per-inch density, popular in year 1990s, px is a physical pixel.

px, in practice, is 1/16 of default font size of a device.

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

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.


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.

typography line terms
typography line terms

[see Meaning of Font Size]


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.

[“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.

[From CSS 2.1 Specification, 2011]

Syntax and basic data types#length-units

vw, vh, vmin, vmax

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

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 percentage length by vw, because the meaning of percentage is different for each CSS property.

cm, mm, in, pc, pt

centimeter. 10 mm → ~0.3937 inch; ~37.795 px
millimeter. 0.1 cm → ~3.78 px
Inch. 6 pc. 72 pt. 96 px. → 2.54 cm
1/6 inch. 12 pt. 16 px. “pc” stands for “pica”. → ~0.423 cm
1/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]

What is CSS's Default Unit?

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

You can omit unit only when the value is 0.





Text Decoration

Box Model

Box Decoration




Line Wrap