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.
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.
Relative lengths represent a measurement 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 define 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
font
.ch
font
.em
font-size
of the element. If used on the font-size
property itself, it represents the inherited font-size of the element.ex
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
lh
line-height
property of the element on which it is used, converted to an absolute length.rem
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
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 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.
vh
vw
vi
vb
vmin
vw
and vh
.vmax
vw
and vh
.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: Many users increase their user agent's default font size to make text more legible. Absolute lengths can cause accessibility problems, since they are fixed and do not scale according to user settings. For this reason, prefer relative lengths (such as em
or rem
) when setting font-size
.
px
1px
= 1/96th of 1in
.cm
1cm
= 96px/2.54
.mm
1mm
= 1/10th of 1cm
.Q
1Q
= 1/40th of 1cm
.in
1in
= 2.54cm
= 96px
.pc
1pc
= 12pt
= 1/6th of 1in
.pt
1pt
= 1/72nd of 1in
.mozmm
, removed in Firefox 59When 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.
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 em , pt , pc , and px units. |
CSS Level 1 The definition of '<length>' in that specification. | Recommendation | Initial definition. Implicit definition of the em , pt , pc , and px units. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 1 | Yes | 1 | 3 | 3.5 | 1 |
cap unit
|
No | No | No | No | No | No |
ch unit |
27 | 12 | 1
|
9 | 20 | 7 |
ex unit |
Yes | Yes | Yes | Yes | Yes | Yes |
ic unit
|
No | No | No | No | No | No |
lh unit
|
No | No | No | No | No | No |
mozmm unit
|
No | No | 4 — 59 | No | No | No |
Q unit
|
63 | No | 49 | No | 50 | No |
rem unit |
4 | 12 | 3.6 | 9 | 11.6 | 4.1 |
rlh unit
|
No | Yes | No | No | No | No |
vb unit
|
No | No | No | No | No | No |
vh unit |
20 | 12 | 19 | 9 | 20 | 6 |
vi unit
|
No | No | No | No | No | No |
vmax unit |
26 | ? | 19 | No | 20 | Yes |
vmin unit |
20 | ? | 19 | 10
|
20 | Yes |
vw unit |
20 | 12 | 19 | 9 | 20 | 6 |
Viewport-percentage lengths invalid in @page
|
? | ? | 21 | ? | ? | ? |
1in is always equal to 96px
|
Yes | ? | 4 | No | No | No |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 4 | Yes | Yes | Yes |
cap unit
|
No | No | No | No | No | No | No |
ch unit |
Yes | Yes | 12 | 4 | Yes | 7.1 | Yes |
ex unit |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
ic unit
|
No | No | No | No | No | No | No |
lh unit
|
No | No | No | No | No | No | No |
mozmm unit
|
No | No | No | ? | No | No | No |
Q unit
|
63 | 63 | No | 49 | 50 | No | No |
rem unit |
2 | Yes | 12 | 4 | 12 | 4 | Yes |
rlh unit
|
No | No | Yes | No | No | No | No |
vb unit
|
No | No | No | No | No | No | No |
vh unit |
Yes | Yes | 12 | 19 | Yes | 6 | Yes |
vi unit
|
No | No | No | No | No | No | No |
vmax unit |
1.5 | Yes | ? | 19 | No | 4 | Yes |
vmin unit |
Yes | Yes | ? | 19 | No | 6 | Yes |
vw unit |
Yes | Yes | 12 | 19 | Yes | 6 | Yes |
Viewport-percentage lengths invalid in @page
|
? | ? | ? | 21 | ? | ? | ? |
1in is always equal to 96px
|
? | ? | ? | ? | ? | ? | ? |
© 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