mozilla

Revision 446947 of <percentage>

  • Revision slug: Web/CSS/percentage
  • Revision title: percentage
  • Revision id: 446947
  • Created:
  • Creator: roryokane
  • Is current revision? No
  • Comment fix CSS3 spec link

Revision Content

{{ CSSRef() }}

Summary

The <percentage> CSS data types represent a percentage value. Many CSS properties can take percentage values, often to define sizes in terms of parent objects. Percentages are formed by a {{ Xref_cssnumber() }} immediately followed by the percentage sign %. Just as is the case with all other units in CSS, there isn't a space between the '%' and the number.

Many length properties use percentages, such as {{ Cssxref("width") }}, {{ Cssxref("margin") }} and {{ Cssxref("padding") }}. Percentages can also be seen in {{ Cssxref("font-size") }}, where the size of the text is directly related to the size of its parent.

Note: only calculated values are inherited. So, even if a percentage value is used on the parent property, a real value, like a width in pixel for a {{ Xref_csslength() }} value, will be accessible on the inherited property, not the percentage value.

Interpolation

Values of the <percentage> CSS data type can be interpolated in order to allow animations. In that case they are interpolated as real, floating-point numbers. The speed of the interpolation is determined by the timing function associated with the animation.

Examples

<div style="background-color:#0000FF;">
  <div style="width:50%;margin-left:20%;background-color:#00FF00;">Width: 50%, Left margin: 20%</div>
  <div style="width:30%;margin-left:60%;background-color:#FF0000;">Width: 30%, Left margin: 60%</div>
</div> 

The above HTML will output:

Width: 50%, Left margin: 20%
Width: 30%, Left margin: 60%

 

<div style="font-size:18px;">
  Full size text (18px)
  <span style="font-size:50%;">50%</span>
  <span style="font-size:200%;">200%</span>
</div> 

The above HTML will output:

Full size text (18px) 50% 200%

Specifications

Specification Status Comment
{{ SpecName('CSS3 Values', '#percentages', '<percentage>') }} {{ Spec2('CSS3 Values') }} No significant change from CSS Level 2 (Revision 1)
{{ SpecName('CSS2.1', 'syndata.html#percentage-units', '<percentage>') }} {{ Spec2('CSS2.1') }} No change from CSS Level 1
{{ SpecName('CSS1', '#percentage-units', '<percentage>') }} {{ Spec2('CSS1') }}  

Browser Compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 1.0 (1.0) <=5.0 yes 1.0 (85)
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support yes yes yes yes yes

See also

  • CSS Data Types : {{ CSSDataTypes() }}

Revision Source

<p>{{ CSSRef() }}</p>
<h2 id="Summary">Summary</h2>
<p>The <code>&lt;percentage&gt;</code> <a href="/en/CSS" title="CSS">CSS</a> data types represent a percentage value. Many <a href="/en/CSS_Reference" title="en/CSS_Reference">CSS properties</a> can take percentage values, often to define sizes in terms of parent objects. Percentages are formed by a {{ Xref_cssnumber() }} immediately followed by the percentage sign <code>%</code>. Just as is the case with all other units in CSS, there isn't a space between the '%' and the number.</p>
<p>Many length properties use percentages, such as {{ Cssxref("width") }}, {{ Cssxref("margin") }} and {{ Cssxref("padding") }}. Percentages can also be seen in {{ Cssxref("font-size") }}, where the size of the text is directly related to the size of its parent.</p>
<div class="note">
  <strong>Note:</strong> only calculated values are inherited. So, even if a percentage value is used on the parent property, a real value, like a width in pixel for a {{ Xref_csslength() }} value, will be accessible on the inherited property, not the percentage value.</div>
<h2 id="Interpolation">Interpolation</h2>
<p>Values of the <code>&lt;percentage&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 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 id="Examples">Examples</h2>
<pre class="brush: html">
&lt;div style="background-color:#0000FF;"&gt;
  &lt;div style="width:50%;margin-left:20%;background-color:#00FF00;"&gt;Width: 50%, Left margin: 20%&lt;/div&gt;
  &lt;div style="width:30%;margin-left:60%;background-color:#FF0000;"&gt;Width: 30%, Left margin: 60%&lt;/div&gt;
&lt;/div&gt; 
</pre>
<p>The above HTML will output:</p>
<div style="background-color:#0000FF;">
  <div style="width:50%;margin-left:20%;background-color:#00FF00;">
    Width: 50%, Left margin: 20%</div>
  <div style="width:30%;margin-left:60%;background-color:#FF0000;">
    Width: 30%, Left margin: 60%</div>
</div>
<p>&nbsp;</p>
<pre class="brush: html">
&lt;div style="font-size:18px;"&gt;
  Full size text (18px)
  &lt;span style="font-size:50%;"&gt;50%&lt;/span&gt;
  &lt;span style="font-size:200%;"&gt;200%&lt;/span&gt;
&lt;/div&gt; 
</pre>
<p>The above HTML will output:</p>
<div style="font-size:18px;">
  Full size text (18px) <span style="font-size:50%">50%</span> <span style="font-size:200%">200%</span></div>
<h2 id="Specifications" 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>{{ SpecName('CSS3 Values', '#percentages', '&lt;percentage&gt;') }}</td>
      <td>{{ Spec2('CSS3 Values') }}</td>
      <td>No significant change from CSS Level 2 (Revision 1)</td>
    </tr>
    <tr style="vertical-align: top;">
      <td style="vertical-align: top;">{{ SpecName('CSS2.1', 'syndata.html#percentage-units', '&lt;percentage&gt;') }}</td>
      <td style="vertical-align: top;">{{ Spec2('CSS2.1') }}</td>
      <td style="vertical-align: top;">No change from CSS Level 1</td>
    </tr>
    <tr>
      <td style="vertical-align: top;">{{ SpecName('CSS1', '#percentage-units', '&lt;percentage&gt;') }}</td>
      <td style="vertical-align: top;">{{ Spec2('CSS1') }}</td>
      <td style="vertical-align: top;">&nbsp;</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_Compatibility" 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.0</td>
        <td>1.0 (1.0)</td>
        <td>&lt;=5.0</td>
        <td>yes</td>
        <td>1.0 (85)</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>yes</td>
        <td>yes</td>
        <td>yes</td>
        <td>yes</td>
        <td>yes</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li>CSS Data Types : {{ CSSDataTypes() }}</li>
</ul>
Revert to this revision