<length>

  • Revision slug: CSS/length
  • Revision title: length
  • Revision id: 40931
  • Created:
  • Creator: Jürgen Jeka
  • Is current revision? No
  • Comment 46 words added, 158 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. Having a space between the number an the unit is forbidden.

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 px 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. In most fonts 1ex is roughly 0.5em.
ch
Gecko 1-1.9 (Firefox 1-3.0) had a {{ Non-standard_inline() }} implementation of the ch. It 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. html)

 

Absolute length units

Are useful when the physical properties of the output medium are known, e.g. for print layout. On screen displays, the number of pixels in an millimeter is determined by the system's estimate (often incorrect) of the resolution of its display.

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

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.

Specifications

Browser compatibility

Browser Lowest Version ch unit rem unit
Internet Explorer 3.0-4.0 n/a  
Firefox (Gecko) 1.0 (1.0) 1.0 (1.0)  width of "M"
3.5 (1.9.1)  width of "0" (zero)
3.6 (1.9.2)
Opera 3.5 n/a  
Safari (WebKit) 1.0 (85) n/a  

See also

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

Revision Source

<p>{{ CSSRef() }}</p>
<h3 name="Summary">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. Having a space between the number an the unit is <strong>forbidden</strong>.</p>
<p>For some properties, using negative lengths is a syntax error, but for some properties, negative lengths are allowed.</p>
<h3 name="Units">Units</h3>
<dl>
<h4>Relative length units</h4>
<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<code> px </code>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. In most fonts<code> 1ex </code>is roughly<code> 0.5em</code>. <br>
</dd><dt>ch</dt><dd>Gecko 1-1.9 (Firefox 1-3.0) had a {{ Non-standard_inline() }} implementation of the<code> ch</code>. It 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") }} 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</dt><dd>{{ gecko_minversion_inline("1.9.2") }} The<code> font-size </code>of the root element (e.g. <code>html</code>)</dd>
<p> </p>
<h4>Absolute length units</h4>
<p>Are useful when the physical properties of the output medium are known, e.g. for print layout. On screen displays, the number of pixels in an millimeter is determined by the system's estimate (often incorrect) of the resolution of its display.</p>
<dt>mm</dt><dd> One millimeter. <br>
</dd><dt>cm</dt><dd> One centimeter (10 millimeters).<br>
</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></dl>
<h3 name="Notes">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 name="Specifications">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 name="Browser_compatibility">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>n/a</td> <td> </td> </tr> <tr> <td>Firefox (Gecko)</td> <td><strong>1.0</strong> (1.0)</td> <td>1.0 (1.0)  width of "M"<br> <strong>3.5</strong> (1.9.1)  width of "0" (zero)</td> <td>3.6 (1.9.2)</td> </tr> <tr> <td>Opera</td> <td><strong>3.5</strong></td> <td>n/a</td> <td> </td> </tr> <tr> <td>Safari (WebKit)</td> <td><strong>1.0</strong> (85)</td> <td>n/a</td> <td> </td> </tr> </tbody>
</table>
<h3 name="See_also">See also</h3>
<p>{{ languages( { "es": "es/CSS/length", "fr": "fr/CSS/longueur" } ) }}</p>
Revert to this revision