<length>

  • Revision slug: CSS/length
  • Revision title: length
  • Revision id: 40975
  • Created:
  • Creator: RAMilewski
  • Is current revision? No
  • Comment Fixed incorrect description of pt; 5 words added, 8 words removed

Revision Content

Summary

The <length> CSS data type denotes distance measurements. It is a {{ xref_cssnumber() }} 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 {{ Cssxref("width") }}, {{ Cssxref("margin") }},  {{ Cssxref("padding") }}, {{ Cssxref("font-size") }}, {{ Cssxref("border-width") }}, {{ Cssxref("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 {{ Xref_csspercentage() }} 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. 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 {{ Cssxref("font-size") }} of the element. If used on the {{ Cssxref("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 {{ cssxref("line-height") }}, {{ cssxref("font-size") }}, {{ cssxref("margin-bottom") }} and {{ cssxref("margin-top") }} often has a value expressend in em.
ex
This unit represents the x-height of the element's {{ Cssxref("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 {{ experimental_inline() }}
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 {{ Cssxref("font") }}.
rem {{ experimental_inline() }}
This unit represents the {{ Cssxref("font-size") }} of the root element (e.g. the font-size of the {{ HTMLElement("html") }} element). When used on the {{ Cssxref("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-relative length {{ experimental_inline() }}

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 {{ gecko_minversion_inline("2.0") }} {{ non-standard_inline() }}
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.

{{ h1_gecko_minversion("CSS units and dots-per-inch", "2.0") }}

Prior to Gecko 2.0 {{ geckoRelease("2.0") }}, 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 {{ Spec2('CSS3 Values') }} Added ch, rem, vw, vh, vm
CSS Level 2 (Revision 1) {{ Spec2('CSS2.1') }} pt, pc, px are explicitly defined (were implicitly defined in CSS1)
CSS Level 1 {{ Spec2('CSS1') }}  

Browser Compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1 1.0 (1.0) 3.0 3.5 1.0 (85)
ch no 1.0 (width of 'M') [*]
3.5 (correct)
9 no no
rem 1 (528) 3.6 (1.9.2) 9 no 5.0 (533.17.8)
vh, vw, vm no no 9 no no
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 {{ CSSxref("border-width") }} and {{ CSSxref("outline-width") }} CSS properties.

See also

{{ languages( { "es": "es/CSS/length", "fr": "fr/CSS/longueur" ,"ja": "ja/CSS/length"} ) }}

Revision Source

<h2>Summary</h2>
<p>The <code>&lt;length&gt;</code> <a href="/en/CSS" title="CSS">CSS</a> data type denotes distance measurements. It is a {{ xref_cssnumber() }} immediately followed by a length unit (<code>px</code>, <code>em</code>, <code>pc</code>, <code>in</code>, <code>mm</code>, ...). Like for any CSS dimension, there is no space between the unit literal and the number.</p>
<p>Many CSS properties take<code> &lt;length&gt; </code>values, such as {{ Cssxref("width") }}, {{ Cssxref("margin") }},  {{ Cssxref("padding") }}, {{ Cssxref("font-size") }}, {{ Cssxref("border-width") }}, {{ Cssxref("text-shadow") }}, ...</p>
<p>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 <code>&lt;length&gt;</code> values also accepts {{ Xref_csspercentage() }} values; though also CSS dimensions, these are not <code>&lt;length&gt;</code> values.</p>
<h2>Interpolation</h2>
<p>Values of the <code>&lt;length&gt;</code> CSS data type can be interpolated in order to allow animations. In that case they are interpolated as real, floating-point, numbers. <a class="external" href="http://dev.w3.org/csswg/css3-transitions/#animation-of-property-types-" title="http://dev.w3.org/csswg/css3-transitions/#animation-of-property-types-"></a> The interpolation happens on the calculated value. The speed of the interpolation is determined by the <a href="/en/CSS/timing-function" title="timing-function">timing function</a> associated with the animation.</p>
<h2>Units</h2>
<h3>Relative length units</h3>
<h4>Font-relative length</h4>
<dl> <dt>em</dt> <dd>This unit represents the calculated {{ Cssxref("font-size") }} of the element. If used on the {{ Cssxref("font-size") }} property itself, it represents the <em>inherited</em> font-size of the element.<br> <div class="note">This unit is often used to create scalable layouts, which keep the <a class="external" href="http://24ways.org/2006/compose-to-a-vertical-rhythm" title="http://24ways.org/2006/compose-to-a-vertical-rhythm">vertical rhythm of the page</a>, even when the user changes the size of the fonts. The CSS properties {{ cssxref("line-height") }}, {{ cssxref("font-size") }}, {{ cssxref("margin-bottom") }} and {{ cssxref("margin-top") }} often has a value expressend in <strong>em</strong>.</div> </dd> <dt>ex</dt> <dd>This unit represents the <a class="external" href="http://en.wikipedia.org/wiki/X-height" title="http://en.wikipedia.org/wiki/X-height">x-height</a> of the element's {{ Cssxref("font") }}. On fonts with the 'x' letter, this is generally the height of lowercase letters in the font;<code style="white-space: nowrap;"> 1ex ≈ 0.5em </code>in many fonts.</dd> <dt>ch {{ experimental_inline() }}</dt> <dd>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 {{ Cssxref("font") }}.</dd> <dt>rem {{ experimental_inline() }}</dt> <dd>This unit represents the {{ Cssxref("font-size") }} of the root element (e.g. the font-size of the {{ HTMLElement("html") }} element). When used on the {{ Cssxref("font-size") }} on this root element, it represents its initial value. <div class="note">This unit is practical in creating perfectly scalable layout. If not supported by the targeted browsers, such layout can be achieved using the <strong>em</strong> unit, though this is slightly more complex.</div> </dd>
</dl>
<h4>Viewport-relative length {{ experimental_inline() }}</h4>
<dl> <dt>vh</dt> <dd>1/100th of the height of the viewport.</dd> <dt>vw</dt> <dd>1/100th of the width of the viewport.</dd> <dt>vm</dt> <dd>1/100th of the minimum value between the height and the width of the viewport.</dd>
</dl>
<h3>Absolute length units</h3>
<p>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.</p>
<p>For low-dpi devices, the unit <strong>px</strong> represents the physical <em>reference pixel</em> and the others are defined relative to it. Thus, <code>1in</code> is defined as <code>96px</code> which equals <code>72pt</code>. The consequence of this definition is that on such devices, length described in inches (<code>in</code>), centimeters (<code>cm</code>), millimeters (<code>mm</code>) doesn't necessary match the length of the physical unit with the same name.</p>
<p>For high-dpi devices, inches (<code>in</code>), centimeters (<code>cm</code>), millimeters (<code>mm</code>) are defined as their physical counterparts. Therefore the <strong>px</strong> unit is defined relative to them (1/96 of 1 inch).</p>
<dl> <dt>px</dt> <dd>Relative to the viewing device.<br> For screen display, typically one device pixel (dot) of the display.<br> For <em>printers</em> and very <em>high resolution screens</em> one CSS pixel implies multiple device pixels, so that the number of pixel per inch stays around 96.</dd> <dt> </dt><dt>mm</dt> <dd>One millimeter.</dd> <dt>cm</dt> <dd>One centimeter (10 millimeters).</dd> <dt>in</dt> <dd>One inch (2.54 centimeters).</dd> <dt>pt</dt> <dd>One point (which is 1/72 of an inch).</dd> <dt>pc</dt> <dd>One pica (which is 12 points).</dd> <dt>mozmm {{ gecko_minversion_inline("2.0") }} {{ non-standard_inline() }}</dt> <dd>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.</dd>
</dl>
<p>{{ h1_gecko_minversion("CSS units and dots-per-inch", "2.0") }}</p>
<p class="note">Prior to Gecko 2.0 {{ geckoRelease("2.0") }}, 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.</p>
<p>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.</p>
<p>Some specific examples:</p>
<ul> <li>1in is now always 96px</li> <li>3pt is now always 4px</li> <li>25.4mm is now always 96px</li>
</ul>
<p>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.</p>
<h2 name="Specifications">Specifications</h2>
<table class="standard-table"> <thead> <tr style="background-color: rgb(255, 204, 255);"> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> </thead> <tbody> <tr> <td><a class="external" href="http://www.w3.org/TR/css3-values/#lengths" title="http://www.w3.org/TR/css3-values/#lengths">CSS Values and Units Level 3</a></td> <td>{{ Spec2('CSS3 Values') }}</td> <td>Added <code>ch</code>, <code>rem</code>, <code>vw</code>, <code>vh</code>, <code>vm</code></td> </tr> <tr style="vertical-align: top;"> <td style="vertical-align: top;"><a class="external" href="http://www.w3.org/TR/CSS21/syndata.html#length-units" title="http://www.w3.org/TR/CSS21/syndata.html#length-units">CSS Level 2 (Revision 1)</a></td> <td style="vertical-align: top;">{{ Spec2('CSS2.1') }}</td> <td style="vertical-align: top;"><code>pt</code>, <code>pc</code>, <code>px</code> are explicitly defined (were implicitly defined in CSS1)</td> </tr> <tr> <td style="vertical-align: top;"><a class="external" href="http://www.w3.org/TR/CSS1/#length-units" title="http://www.w3.org/TR/CSS1/#length-units">CSS Level 1</a></td> <td style="vertical-align: top;">{{ Spec2('CSS1') }}</td> <td style="vertical-align: top;"> </td> </tr> </tbody>
</table>
<h2 name="Browser_Compatibility">Browser Compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Chrome</th> <th>Firefox (Gecko)</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari (WebKit)</th> </tr> <tr> <td>Basic support</td> <td>1</td> <td>1.0 (1.0)</td> <td>3.0 <a class="external" href="http://msdn.microsoft.com/en-us/library/cc351024(v=vs.85).aspx" title="http://msdn.microsoft.com/en-us/library/cc351024(v=vs.85).aspx"></a></td> <td>3.5</td> <td>1.0 (85)</td> </tr> <tr> <td>ch</td> <td>no</td> <td>1.0 (width of 'M') [*]<br> 3.5 (correct)</td> <td>9 <a class="external" href="http://msdn.microsoft.com/en-us/library/cc351024(v=vs.85).aspx" title="http://msdn.microsoft.com/en-us/library/cc351024(v=vs.85).aspx"></a></td> <td>no</td> <td>no</td> </tr> <tr> <td>rem</td> <td>1 (528) <a class="link-https" href="https://bugs.webkit.org/show_bug.cgi?id=27283" title="https://bugs.webkit.org/show_bug.cgi?id=27283"></a></td> <td>3.6 (1.9.2)</td> <td>9 <a class="external" href="http://msdn.microsoft.com/en-us/library/cc351024(v=vs.85).aspx" title="http://msdn.microsoft.com/en-us/library/cc351024(v=vs.85).aspx"></a></td> <td>no</td> <td>5.0 (533.17.8)</td> </tr> <tr> <td>vh, vw, vm</td> <td>no <a class="link-https" href="https://bugs.webkit.org/show_bug.cgi?id=27160" title="https://bugs.webkit.org/show_bug.cgi?id=27160"></a></td> <td>no <a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=503720" title="https://bugzilla.mozilla.org/show_bug.cgi?id=503720"></a></td> <td>9 <a class="external" href="http://msdn.microsoft.com/en-us/library/cc351024(v=vs.85).aspx" title="http://msdn.microsoft.com/en-us/library/cc351024(v=vs.85).aspx"></a></td> <td>no</td> <td>no <a class="link-https" href="https://bugs.webkit.org/show_bug.cgi?id=27160" title="https://bugs.webkit.org/show_bug.cgi?id=27160"></a></td> </tr> </tbody> </table>
</div>
<div id="compat-mobile"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Android</th> <th>Firefox Mobile (Gecko)</th> <th>IE Phone</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>yes</td> <td>yes</td> <td>yes</td> <td>yes</td> <td>yes</td> </tr> <tr> <td>ch</td> <td>no</td> <td>yes</td> <td>?</td> <td>no</td> <td>no</td> </tr> <tr> <td>rem</td> <td>2.1</td> <td>yes</td> <td>?</td> <td>no</td> <td>4.0</td> </tr> <tr> <td>vh, vw, vm</td> <td>no</td> <td>no</td> <td>?</td> <td>no</td> <td>no</td> </tr> </tbody> </table>
</div>
<p>[*] Also, didn't work for {{ CSSxref("border-width") }} and {{ CSSxref("outline-width") }} CSS properties.</p>
<h2>See also</h2>
<ul> <li><a href="/en/CSS/CSS_Reference" title="CSS Reference">CSS Reference</a> index</li> <li>CSS Data Types : {{ CSSDataTypes() }}</li>
</ul>
<p>{{ languages( { "es": "es/CSS/length", "fr": "fr/CSS/longueur" ,"ja": "ja/CSS/length"} ) }}</p>
Revert to this revision