Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1079242 of <length>

  • Revision slug: Web/CSS/length
  • Revision title: <length>
  • Revision id: 1079242
  • Created:
  • Creator: phistuck
  • Is current revision? No
  • Comment

Revision Content

{{CSSRef}}

The <length> CSS data type denotes distance measurements. It is a {{cssxref("<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. The length unit is optional after the {{cssxref("<number>")}} 0.

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. Please note that although {{cssxref("<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 {{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 have values expressed 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
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
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-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 browsers are updating the viewport 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 conjunction with overflow:auto, space taken by eventual scrollbars is not subtracted from the viewport, whereas in the case of overflow:scroll, it is.

In a {{cssxref("@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.
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/72th of an inch).
pc
One pica (12 points).
mozmm {{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.

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.

Specifications

Specification Status Comment
{{SpecName('CSS3 Values', '#lengths', '<length>')}} {{Spec2('CSS3 Values')}} Added ch, rem, vw, vh, vmin, vmax and q
{{SpecName('CSS2.1', 'syndata.html#length-units', '<length>')}} {{Spec2('CSS2.1')}} pt, pc, px are explicitly defined (were implicitly defined in CSS1)
{{SpecName('CSS1', '#length-units', '<length>')}} {{Spec2('CSS1')}} Initial definition

Browser compatibility

{{CompatibilityTable}} 

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1 {{ CompatGeckoDesktop("1.0")}} 3.0 3.5 1.0
ch

27

{{ CompatGeckoDesktop("1.0")}}[1] 9.0 20.0 7.0
ex {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
rem 4 (532.3) {{ CompatGeckoDesktop("1.9.2")}} 9.0 11.6 4.1
vh, vw 20 {{CompatGeckoDesktop("19")}} 9.0 20.0 6.0
vmin

20

{{CompatGeckoDesktop("19")}} 9.0[2] 20.0 6.0
vmax 26 {{CompatGeckoDesktop("19")}} {{CompatNo}} 20.0 {{CompatVersionUnknown}}
Viewport-percentage lengths invalid in {{cssxref("@page")}} {{CompatUnknown}} {{CompatGeckoDesktop("21")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
mozmm {{non-standard_inline}} {{CompatNo}} {{ CompatGeckoDesktop("2.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
1in always is 96dpi {{CompatVersionUnknown}} {{ CompatGeckoDesktop("2.0")}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
q {{CompatNo}} {{CompatGeckoDesktop("49.0")}} {{CompatNo}} {{CompatNo}} {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
ch {{CompatNo}} {{CompatVersionUnknown}} 7.8 {{CompatUnknown}} 7.1.1
ex {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
rem 2.1 {{CompatVersionUnknown}} {{CompatUnknown}} 12.0 4.0
vh, vw, vmin {{CompatVersionUnknown}} {{CompatGeckoMobile("19")}} {{CompatUnknown}} {{CompatNo}} 6.0
vmax 1.5 {{CompatGeckoMobile("19")}} {{CompatUnknown}} {{CompatNo}} 4.0
Viewport-percentage lengths invalid in {{cssxref("@page")}} {{CompatUnknown}} {{CompatGeckoMobile("21.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
q {{CompatUnknown}} {{CompatGeckoMobile("49.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

[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 {{CSSxref("border-width")}} and {{CSSxref("outline-width")}} CSS properties.

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

Revision Source

<div>{{CSSRef}}</div>

<p>The <code>&lt;length&gt;</code> <a href="/en-US/docs/Web/CSS">CSS</a> data type denotes distance measurements. It is a {{cssxref("&lt;number&gt;")}} 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.&nbsp;The length unit is optional after the {{cssxref("&lt;number&gt;")}} <code>0</code>.</p>

<p>Many CSS properties take<code> &lt;length&gt; </code>values, such as {{Cssxref("width")}}, {{Cssxref("margin")}},&nbsp; {{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. Please note that although {{cssxref("&lt;percentage&gt;")}} values are also CSS dimensions and are accepted by some CSS properties that accept<code> &lt;length&gt;</code> values, they are not themselves, <code>&lt;length&gt;</code> values.</p>

<h2 id="Interpolation">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. The interpolation happens on the calculated value. The speed of the interpolation is determined by the <a href="/en-US/docs/Web/CSS/timing-function">timing function</a> associated with the animation.</p>

<h2 id="Units">Units</h2>

<h3 id="Relative_length_units">Relative length units</h3>

<h4 id="Font-relative_lengths">Font-relative lengths</h4>

<dl>
 <dt id="em"><code>em</code></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.
 <div class="note">This unit is often used to create scalable layouts, which keep the <a href="https://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 have&nbsp;values expressed in <strong>em</strong>.</div>
 </dd>
 <dt id="ex"><code>ex</code></dt>
 <dd>This unit represents the <a href="https://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 id="ch"><code>ch</code></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 id="rem"><code>rem</code></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 id="Viewport-percentage_lengths">Viewport-percentage lengths</h4>

<p>Viewport-percentage lengths defined a length relatively to the size of viewport, that is the visible portion of the document. Only Gecko-based browsers are updating the viewport 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).</p>

<p>In conjunction with <code>overflow:auto</code>, space taken by eventual scrollbars is not subtracted from the viewport, whereas in the case of <code>overflow:scroll</code>, it is.</p>

<p>In a {{cssxref("@page")}} at-rule declaration block, the use of the viewport lengths are invalid and the declaration will be dropped.</p>

<dl>
 <dt id="vh"><code>vh</code></dt>
 <dd>1/100th of the height of the viewport.</dd>
 <dt id="vw"><code>vw</code></dt>
 <dd>1/100th of the width of the viewport.</dd>
 <dt id="vmin"><code>vmin</code></dt>
 <dd>1/100th of the minimum value between the height and the width of the viewport.</dd>
 <dt id="vmax"><code>vmax</code></dt>
 <dd>1/100th of the maximum value between the height and the width of the viewport.</dd>
</dl>

<h3 id="Absolute_length_units">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>

<div class="note">
<p>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 <code>em</code> or <code>rem</code>.</p>
</div>

<dl>
 <dt id="px"><code>px</code></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 id="mm"><code>mm</code></dt>
 <dd>One millimeter.</dd>
 <dt id="q"><code>q</code></dt>
 <dd>A quarter of a millimeter (1/40th of a centimeter).</dd>
 <dt id="cm"><code>cm</code></dt>
 <dd>One centimeter (10 millimeters).</dd>
 <dt id="in"><code>in</code></dt>
 <dd>One inch (2.54 centimeters).</dd>
 <dt id="pt"><code>pt</code></dt>
 <dd>One point (1/72th of an inch).</dd>
 <dt id="pc"><code>pc</code></dt>
 <dd>One pica (12 points).</dd>
 <dt id="mozmm"><code>mozmm</code> {{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>

<h2 id="CSS_units_and_dots-per-inch">CSS units and dots-per-inch</h2>

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

<p>Some specific examples:</p>

<ul>
 <li><code>1in</code> is always <code>96px,</code></li>
 <li><code>3pt</code> is always <code>4px</code>,</li>
 <li><code>25.4mm</code> is always <code>96px.</code></li>
</ul>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSS3 Values', '#lengths', '&lt;length&gt;')}}</td>
   <td>{{Spec2('CSS3 Values')}}</td>
   <td>Added <code>ch</code>, <code>rem</code>, <code>vw</code>, <code>vh</code>, <code>vmin</code>, <code>vmax</code> and <code>q</code></td>
  </tr>
  <tr>
   <td>{{SpecName('CSS2.1', 'syndata.html#length-units', '&lt;length&gt;')}}</td>
   <td>{{Spec2('CSS2.1')}}</td>
   <td><code>pt</code>, <code>pc</code>, <code>px</code> are explicitly defined (were implicitly defined in CSS1)</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS1', '#length-units', '&lt;length&gt;')}}</td>
   <td>{{Spec2('CSS1')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}&nbsp;</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</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>1</td>
   <td>{{ CompatGeckoDesktop("1.0")}}</td>
   <td>3.0</td>
   <td>3.5</td>
   <td>1.0</td>
  </tr>
  <tr>
   <td><code>ch</code></td>
   <td>
    <p>27</p>
   </td>
   <td>{{ CompatGeckoDesktop("1.0")}}<sup>[1]</sup></td>
   <td>9.0</td>
   <td>20.0</td>
   <td>7.0</td>
  </tr>
  <tr>
   <td>ex</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>rem</code></td>
   <td>4 (532.3)</td>
   <td>{{ CompatGeckoDesktop("1.9.2")}}</td>
   <td>9.0</td>
   <td>11.6</td>
   <td>4.1</td>
  </tr>
  <tr>
   <td><code>vh, vw</code></td>
   <td>20</td>
   <td>{{CompatGeckoDesktop("19")}}</td>
   <td>9.0</td>
   <td>20.0</td>
   <td>6.0</td>
  </tr>
  <tr>
   <td><code>vmin</code></td>
   <td>
    <p>20</p>
   </td>
   <td>{{CompatGeckoDesktop("19")}}</td>
   <td>9.0<sup>[2]</sup></td>
   <td>20.0</td>
   <td>6.0</td>
  </tr>
  <tr>
   <td><code>vmax</code></td>
   <td>26</td>
   <td>{{CompatGeckoDesktop("19")}}</td>
   <td>{{CompatNo}}</td>
   <td>20.0</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Viewport-percentage lengths invalid in {{cssxref("@page")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("21")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>mozmm</code> {{non-standard_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{ CompatGeckoDesktop("2.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>1in</code> always is <code>96dpi</code></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{ CompatGeckoDesktop("2.0")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>q</code></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop("49.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</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&nbsp;Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>ch</code></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>7.8</td>
   <td>{{CompatUnknown}}</td>
   <td>7.1.1</td>
  </tr>
  <tr>
   <td>ex</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>rem</code></td>
   <td>2.1</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>12.0</td>
   <td>4.0</td>
  </tr>
  <tr>
   <td><code>vh, vw, vmin</code></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("19")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>6.0</td>
  </tr>
  <tr>
   <td><code>vmax</code></td>
   <td>1.5</td>
   <td>{{CompatGeckoMobile("19")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>4.0</td>
  </tr>
  <tr>
   <td>Viewport-percentage lengths invalid in {{cssxref("@page")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("21.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>q</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("49.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] In Gecko 1.0-1.9.0 (Firefox 1.0-3.0)<code> ch </code>was the width of 'M' and it didn't work for {{CSSxref("border-width")}} and {{CSSxref("outline-width")}} CSS properties.</p>

<p>[2] Internet Explorer implements this with the non-standard name <code>vm</code>.</p>
Revert to this revision