CSS: Units

By Xah Lee. Date: . Last updated: .

Example of length in your browser:

5%
80px
5rem
5em
10ex
10ch
10vw
10vh
10vmin
10vmax
1cm
10mm
1in
72pt
6pc
96px

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.

Example:

div {margin:1em}

Percent Value

% is percentage.

Percentage is not technically considered a “length unit”.

Example:

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

[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

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

css reference pixel
CSS Reference Pixel

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

em

The length of em is:

ex, font 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

ch is 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.

[From CSS 2.1 Specification, 2011]

Syntax and basic data types#length-units

vw, vh, vmin, vmax

These units are not fully supported by IE11 as of

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

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

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

[see CSS: Media Query]

What's 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.

Reference

http://dev.w3.org/csswg/css-values/#font-relative-lengths

Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Reference
  5. DOM
  6. SVG
  7. Web Dev Blog