Table of contents
Summary
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
, margin
, padding
, font-size
, border-width
, text-shadow
, ...
For some properties, using negative lengths is a syntax error, but for some properties, negative lengths are allowed. Note also that some CSS properties accepted <length> values also accepts <percentage>
values; though also CSS dimensions, these are not <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. [1] 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 length
- em
- This unit represents the calculated
font-sizeof the element. If used on thefont-sizeproperty 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 propertiesline-height,font-size,margin-bottomandmargin-topoften 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.5emin many fonts. - ch Experimental
- 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 Experimental
- This unit represents the
font-sizeof the root element (e.g. the font-size of the<html>element). When used on thefont-sizeon 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-relative length Experimental
- vh
- 1/100th of the height of the viewport.
- vw
- 1/100th of the width of the viewport.
- vm
- 1/100th of the minimum 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).
- 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 Requires Gecko 2.0 Non-standard
- 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
Prior to Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) , Gecko attempted (often incorrectly) to actually compute the real size to render for these, even though most other rendering engines assumed 96 DPI. Now Gecko is consistent with the other browsers.
Starting in Gecko 2.0, changes to how CSS units translate to dots-per-inch (DPI) have been made to become consistent with the behavior of other popular browsers including Safari, Internet Explorer, and Google Chrome.
Some specific examples:
- 1in is now always 96px
- 3pt is now always 4px
- 25.4mm is now always 96px
In addition, Gecko has improved ability to correctly scale absolute lengths to the actual resolution of the rendering medium, whether that's the screen or paper.
Specifications
| Specification | Status | Comment |
|---|---|---|
| CSS Values and Units Level 3 | Working Draft | Added ch, rem, vw, vh, vm |
| CSS Level 2 (Revision 1) | Recommendation | pt, pc, px are explicitly defined (were implicitly defined in CSS1) |
| CSS Level 1 | Recommendation |
Browser Compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | 1 | 1.0 (1.0) | 3.0 [2] | 3.5 | 1.0 (85) |
| ch | no | 1.0 (width of 'M') [*] 3.5 (correct) | 9 [3] | no | no |
| rem | 1 (528) [4] | 3.6 (1.9.2) | 9 [5] | 11.6 [6] | 5.0 (533.17.8) |
| vh, vw, vm | no [7] | no [8] | 9 [9] | no | no [10] |
| Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | yes | yes | yes | yes | yes |
| ch | no | yes | ? | no | no |
| rem | 2.1 | yes | ? | no | 4.0 |
| vh, vw, vm | no | no | ? | no | no |
[*] Also, didn't work for border-width
and outline-width
CSS properties.
See also
- CSS Reference index
- CSS Data Types :
<angle>,<color>,<frequency>,<gradient>,<identifier>,<image>,<integer>,<length>,<number>,<percentage>,<ratio>,<resolution>,<shape>,<string>,<time>,<timing-function>,<uri>


Mozilla Developer Network