length property CSS Reference



Definition and Usage

The <length> CSS data type denotes distance measurements. It is a <number> immediately followed by a length unit (px, em, pc, in, mm, ...). Like for any CSS dimension, there is no space between the unit literal and the number.

Many CSS properties take <length> values, such as width, marginpadding, font-size, border-width, text-shadow, ...

For some properties, using negative lengths is a syntax error, but for some properties, negative lengths are allowed. Please note that although <percentage> values are also CSS dimensions and are accepted by some CSS properties that accept <length> values, they are not themselves, <length> values.

Interpolation

Values of the <length> CSS data type can be interpolated in order to allow animations. In that case they are interpolated as real, floating-point, numbers. The interpolation happens on the calculated value. The speed of the interpolation is determined by the timing function associated with the animation.

Units

Relative length units

Font-relative lengths

em
This unit represents the calculated font-size of the element. If used on the font-size property itself, it represents the inherited font-size of the element.
This unit is often used to create scalable layouts, which keep the vertical rhythm of the page, even when the user changes the size of the fonts. The CSS properties line-height, font-size, margin-bottom and margin-top often has a value expressend in em.
ex
This unit represents the x-height of the element's font. On fonts with the 'x' letter, this is generally the height of lowercase letters in the font; 1ex ~= 0.5em in many fonts.
ch
This unit represents the width, or more precisely the advance measure, of the glyph '0' (zero, the Unicode character U+0030) in the element's font.
rem
This unit represents the font-size of the root element (e.g. the font-size of the <html> element). When used on the font-size on this root element, it represents its initial value.
This unit is practical in creating perfectly scalable layout. If not supported by the targeted browsers, such layout can be achieved using the em unit, though this is slightly more complex.

Viewport-percentage lengths

Viewport-percentage lengths defined a length relatively to the size of viewport, that is the visible portion of the document. Only Gecko-based browser are updating the uw3e values dynamically, when the size of the viewport is modified (by modifying the size of the window on a desktop computer or by turning the device on a phone or a tablet).

In a @page at-rule declaration block, the use of the viewport lengths are invalid and the declaration will be dropped.

vh
1/100th of the height of the viewport.
vw
1/100th of the width of the viewport.
vmin
1/100th of the minimum value between the height and the width of the viewport.
vmax
1/100th of the maximum value between the height and the width of the viewport.

Absolute length units

Absolute length units represents a physical measurement and when the physical properties of the output medium are known, such as for print layout. This is done by anchored one of the unit to a physical unit and to defined the other relatively to it. The anchor is done differently for low-resolution devices, like screens, and high-resolution devices, like printers.

For low-dpi devices, the unit px represents the physical reference pixel and the others are defined relative to it. Thus, 1in is defined as 96px which equals 72pt. The consequence of this definition is that on such devices, length described in inches (in), centimeters (cm), millimeters (mm) doesn't necessary match the length of the physical unit with the same name.

For high-dpi devices, inches (in), centimeters (cm), millimeters (mm) are defined as their physical counterparts. Therefore the px unit is defined relative to them (1/96 of 1 inch).

Users may increase font size for accessibility purpose. To allow for usable layouts whatever is the used font size, use only absolute length units when the physical characteristics of the output medium are known, such as bitmap images. When setting length related to font-size, prefer relative units like em or rem.

px
Relative to the viewing device.
For screen display, typically one device pixel (dot) of the display.
For printers and very high resolution screens one CSS pixel implies multiple device pixels, so that the number of pixel per inch stays around 96.
mm
One millimeter.
cm
One centimeter (10 millimeters).
in
One inch (2.54 centimeters).
pt
One point (which is 1/72 of an inch).
pc
One pica (which is 12 points).
mozmm
An experimental unit which attempts to render at exactly one millimeter regardless of the size or resolution of the display. This is rarely actually what you want, but may be useful in particular for mobile devices.

CSS units and dots-per-inch

The unit in doesn't represent a physical inch on screen, but represents 96px. That means that whatever is the real screen pixel density, it is assumed to be 96dpi. On devices with a greater pixel density, 1in will be smaller than 1 physical inch. Similarly mm, cm, and pt are not absolute length.

Some specific examples:

  • 1in is always 96px,
  • 3pt is always 4px,
  • 25.4mm is always 96px.

Compatibility

Feature Chrome (WebKit) Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1 1.0 (1.7 or earlier) 3.0 3.5 1.0
ch 27 1.0 (1.7 or earlier) [1] 9.0 Not supported Not supported
rem 4 (532.3) 3.6 (1.9.2) 9.0 11.6 4.1
vh, vw 20 19.0 (19) 9.0 Not supported 6.0
vmin

20

19.0 (19) 9.0 (with the non-standard name vm) Not supported 6.0
vmax 26 19.0 (19) Not supported Not supported Not supported (537.30)
Viewport-percentage lengths invalid in @page NA 21.0 (21) NA NA NA
mozmm Not supported 4.0 (2.0) Not supported Not supported Not supported
1in always is 96dpi (Yes) 4.0 (2.0) (Yes) (Yes) (Yes)
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
ch Not supported (Yes) NA NA NA
rem 2.1 (Yes) NA 12.0 4
vh, vw, vmin (Yes) 19.0 (19) NA Not supported 6.0
vmax Not supported (bug 91440) 19.0 (19) NA Not supported Not supported (bug 91440)
Viewport-percentage lengths invalid in @page NA 21.0 (21.0) NA NA NA

Relative articles