mozilla

Revision 40947 of <length>

  • Revision slug: CSS/length
  • Revision title: length
  • Revision id: 40947
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment add dpi info from en/CSS-2_Quick_Reference/Units; 107 words added, 26 words removed

Revision Content

{{ CSSRef() }}

Summary

Many CSS properties take <length> values, such as {{ Cssxref("width") }}, {{ Cssxref("margin") }},  {{ Cssxref("padding") }}, {{ Cssxref("font-size") }}, {{ Cssxref("border-width") }}, {{ Cssxref("text-shadow") }} etc.

The CSS syntax for <length> is a number followed immediately by a unit. Space between the number and the unit is not allowed.

For some properties, using negative lengths is a syntax error, but for some properties, negative lengths are allowed.

Units

Relative length units

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.
em
The {{ Cssxref("font-size") }} of the element.
ex
The x-height of the element's {{ Cssxref("font") }}. This is generally the height of lowercase letters in the font. 1ex ≈ 0.5em in many fonts.
ch
Gecko 1-1.9 (Firefox 1-3.0) had a {{ Non-standard_inline() }} implementation of ch. Used the width of "M" and the unit didn't work for border-width and outline-width.
{{ fx_minversion_inline("3") }}
The width of the character "0" (zero) in the current font, as at present drafted in CSS3 Values and Units.
rem {{ gecko_minversion_inline("1.9.2") }}
The font-size of the root element (e.g. the font-size of the html element).

Absolute length units

Are useful when the physical properties of the output medium are known, e.g. for print layout. This assumes that the display is 96 DPI (so that 1in == 96px).

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.
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") }}

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.

Notes

The syntax for {{ Xref_csspercentage() }} (number followed by % sign) is similar to the syntax for <length>. However, percentages are not a type of length, although some of the properties that take length values also take percentage values.

Browser compatibility

Browser Lowest version ch unit rem unit
Internet Explorer 3.0 - 4.0 9 (also support of vh, vw, vm) 9
Firefox (Gecko) 1.0 (1.0) 1.0 (1.0)  width of "M" 3.6 (1.9.2)
3.5 (1.9.1)  width of "0" (zero)
Opera 3.5 --- ---
Safari | Chrome | Webkit 1.0 (85) --- 5 | 5 | 533.17.8

Specifications

See also

{{ Xref_csspercentage() }}, {{ Xref_cssnumber() }}, {{ Xref_cssinteger() }}

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

Revision Source

<p>{{ CSSRef() }}</p>
<h3>Summary</h3>
<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") }} etc.</p>
<p>The CSS syntax for<code> &lt;length&gt; </code> is a number followed immediately by a unit. Space between the number and the unit is not allowed.</p>
<p>For some properties, using negative lengths is a syntax error, but for some properties, negative lengths are allowed.</p>
<h3>Units</h3>
<h4>Relative length units</h4>
<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>em</dt> <dd>The {{ Cssxref("font-size") }} of the element.</dd> <dt>ex</dt> <dd>The x-height of the element's {{ Cssxref("font") }}. 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</dt> <dd>Gecko 1-1.9 (Firefox 1-3.0) had a {{ Non-standard_inline() }} implementation of<code> ch</code>. Used the width of "M" and the unit didn't work for<code> border-width </code>and<code> outline-width</code>.<br> {{ fx_minversion_inline("3") }}<br> The width of the character "0" (zero) in the current font, as at present drafted in <em>CSS3 Values and Units</em>.</dd> <dt>rem {{ gecko_minversion_inline("1.9.2") }}</dt> <dd>The<code> font-size </code>of the root element (e.g. the <code>font-size</code> of the <code>html </code>element).</dd>
</dl>
<h4>Absolute length units</h4>
<p>Are useful when the physical properties of the output medium are known, e.g. for print layout. This assumes that the display is 96 DPI (so that 1in == 96px).</p>
<div class="note"><strong>Note:</strong> 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.</div>
<dl> <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>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>
<h3>Notes</h3>
<p>The syntax for {{ Xref_csspercentage() }} (number followed by <code>%</code> sign) is similar to the syntax for <code>&lt;length&gt;</code>. However, percentages are not a type of length, although some of the properties that take length values also take percentage values.</p>
<h3>Browser compatibility</h3>
<table class="standard-table"> <tbody> <tr> <th>Browser</th> <th>Lowest version</th> <th><code>ch </code>unit</th> <th><code>rem </code>unit</th> </tr> <tr> <td>Internet Explorer</td> <td>3.0 -<strong> 4.0</strong></td> <td>9 (also support of vh, vw, vm)</td> <td>9</td> </tr> <tr> <td rowspan="2">Firefox (Gecko)</td> <td rowspan="2"><strong>1.0</strong> (1.0)</td> <td>1.0 (1.0)  width of "M"</td> <td rowspan="2">3.6 (1.9.2)</td> </tr> <tr> <td><strong>3.5</strong> (1.9.1)  width of "0" (zero)</td> </tr> <tr> <td>Opera</td> <td><strong>3.5</strong></td> <td>---</td> <td>---</td> </tr> <tr> <td>Safari | Chrome | Webkit</td> <td><strong>1.0</strong> (85)</td> <td>---</td> <td>5 | 5 | 533.17.8</td> </tr> </tbody>
</table>
<h3>Specifications</h3>
<ul> <li><a class="external" href="http://www.w3.org/TR/CSS21/syndata.html#length-units">CSS 2.1 Syntax #length-units</a></li> <li><a class="external" href="http://www.w3.org/TR/css3-values/#lengths">CSS 3 Values and Units #lengths</a> Working draft</li>
</ul>
<h3>See also</h3>
<p>{{ Xref_csspercentage() }}, {{ Xref_cssnumber() }}, {{ Xref_cssinteger() }}</p>
<p>{{ languages( { "es": "es/CSS/length", "fr": "fr/CSS/longueur" ,"ja": "ja/CSS/length"} ) }}</p>
Revert to this revision