W3cubDocs

/CSS

<length>

The <length> CSS data type represents a distance value. Lengths can be used in numerous CSS properties, such as width, height, margin, padding, border-width, font-size, and text-shadow.

Note: Although <percentage> values are also CSS dimensions, and are usable in some of the same properties that accept <length> values, they are not themselves <length> values.

Syntax

The <length> data type consists of a <number> followed by one of the units listed below. As with all CSS dimensions, there is no space between the unit literal and the number. The length unit is optional after the number 0.

Note: Some properties allow negative <length>s, while others do not.

Units

Relative length units

Relative lengths represent a <length> in terms of some other distance. Depending on the unit, this can be the size of a specific character, the line height, or the size of the viewport.

Font-relative lengths

Font-relative lengths specify the <length> value in terms of the size of a particular character or font attribute in the font currently in effect in an element or its parent.

Note: These units, especially em and rem, are often used to create scalable layouts, which maintain the vertical rhythm of the page even when the user changes the font size.

cap
Represents the "cap height" (nominal height of capital letters) of the element’s font.
ch
Represents the width, or more precisely the advance measure, of the glyph "0" (zero, the Unicode character U+0030) in the element's font.
em
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.
ex
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.
ic
Equal to the used advance measure of the "水" (CJK water ideograph, U+6C34) glyph found in the font used to render it.
lh
Equal to the computed value of the line-height property of the element on which it is used, converted to an absolute length.
rem
Represents the font-size of the root element (typically <html>). When used within the root element font-size, it represents its initial value (a common browser default is 16px, but user-defined preferences may modify this).
rlh
Equal to the computed value of the line-height property on the root element (typically <html>), converted to an absolute length. When used on the font-size or line-height properties of the root element, it refers to the properties' initial value.
Viewport-percentage lengths

Viewport-percentage lengths define the <length> value relative to the size of the viewport, i.e., the visible portion of the document. Viewport lengths are invalid in @page declaration blocks.

Note: If the <html> and <body> elements are set as overflow:auto, space taken by scrollbars is not subtracted from the viewport, whereas it will be subtracted if set as overflow:scroll.
vh
Equal to 1% of the height of the viewport's initial containing block.
vw
Equal to 1% of the width of the viewport's initial containing block.
vi
Equal to 1% of the size of the initial containing block, in the direction of the root element’s inline axis.
vb
Equal to 1% of the size of the initial containing block, in the direction of the root element’s block axis.
vmin
Equal to the smaller of vw and vh.
vmax
Equal to the larger of vw and vh.

Absolute length units

Absolute length units represent a physical measurement when the physical properties of the output medium are known, such as for print layout. This is done by anchoring one of the units to a physical unit, and then defining the others relative to it. The anchor is done differently for low-resolution devices, such as screens, versus high-resolution devices, such as printers.

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

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

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

px
Depends on the viewing device. For screen displays, it traditionally represents one device pixel (dot). However, for printers and high-resolution screens, one CSS pixel implies multiple device pixels, so that the number of pixels per inch stays around 96.
mm
One millimeter.
q
A quarter of a millimeter (1/40th of a centimeter).
cm
One centimeter (10 millimeters).
in
One inch (2.54 centimeters).
pt
One point (1/72nd of an inch).
pc
One pica (12 points).
mozmm
An experimental unit that 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 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 lengths.

Some specific examples:

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

Interpolation

When animated, values of the <length> data type 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.

Specifications

Specification Status Comment
CSS Values and Units Module Level 4
The definition of '<length>' in that specification.
Editor's Draft Adds the vi, vb, ic, lh, and rlh units.
CSS Values and Units Module Level 3
The definition of '<length>' in that specification.
Candidate Recommendation Adds the ch, rem, vw, vh, vmin, vmax, and q units.
CSS Level 2 (Revision 1)
The definition of '<length>' in that specification.
Recommendation Explicit definition of the pt, pc, and px units.
CSS Level 1
The definition of '<length>' in that specification.
Recommendation Initial definition. Implicit definition of the pt, pc, and px units.

Browser compatibility

Feature Chrome 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 20.0 7.0
ex (Yes) (Yes) (Yes) (Yes) (Yes)
rem 4 (532.3) 3.6 (1.9.2) 9.0 11.6 4.1
vh, vw 20 19 (19) 9.0 20.0 6.0
vmin 20 19 (19) 9.0[2]
10.0
20.0 6.0
vmax 26 19 (19) No support 20.0 (Yes)
Viewport-percentage lengths invalid in @page ? 21 (21) ? ? ?
mozmm No support 4.0 (2.0) No support No support No support
1in is always 96px (Yes) 4.0 (2.0) (Yes) (Yes) (Yes)
q No support 49.0 (49.0) No support No support No support
vi, vb, ic, lh, rlh, cap No support No support No support No support No support
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
ch No support (Yes) 7.8 ? 7.1.1
ex ? (Yes) ? ? ?
rem 2.1 (Yes) ? 12.0 4.0
vh, vw, vmin (Yes) 19.0 (19) ? No support 6.0
vmax 1.5 19.0 (19) ? No support 4.0
Viewport-percentage lengths invalid in @page ? 21.0 (21.0) ? ? ?
q ? 49.0 (49.0) ? ? No support
vi, vb, ic, lh, rlh, cap No support No support No support No support No support

[1] In Gecko 1.0-1.9.0 (Firefox 1.0-3.0), ch was the width of 'M', and it didn't work for the border-width and outline-width CSS properties.

[2] Internet Explorer 9 implements this with the non-standard name vm.

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/length