<percentage>

  • Revision slug: CSS/percentage
  • Revision title: percentage
  • Revision id: 69299
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment 54 words added

Revision Content

{{ CSSRef() }}

Summary

Many CSS properties can take percentage values, often to define sizes in terms of parent objects. Percentages are formed by a number immediately followed by the percentage sign "%".

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 {{ Cssxref("length") }} value, will be accessible on the inherited property, not the percentage value.

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
CSS Values and Units Level 3 {{ Spec2('CSS3 Values') }} No significant change from CSS Level 2 (Revision 1)
CSS Level 2 (Revision 1) {{ Spec2('CSS2.1') }} No change from CSS Level 1
CSS Level 1 {{ Spec2('CSS1') }}  

See also

{{ languages( { "fr": "fr/CSS/pourcentage" } ) }}

Revision Source

<p>{{ CSSRef() }}</p>
<h3 name="Summary">Summary</h3>
<p>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 number immediately followed by the percentage sign "%".</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 {{ Cssxref("length") }} value, will be accessible on the inherited property, not the percentage value.</div><h3>Examples</h3>
<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> </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><h3 name="Specifications">Specifications</h3>
<table border="1" cellpadding="1" cellspacing="1" style="width: 1053px; border-style: solid; border-width: 1px; height: 75px; border-color: rgb(0,0,0);"> <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/#percentages" title="http://www.w3.org/TR/css3-values/#percentages">CSS Values and Units Level 3</a></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;"><a class="external" href="http://www.w3.org/TR/CSS2/syndata.html#percentage-units" title="http://www.w3.org/TR/CSS2/syndata.html#percentage-units">CSS Level 2 (Revision 1)</a></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;"><a class="external" href="http://www.w3.org/TR/CSS1/#percentage-units" title="http://www.w3.org/TR/CSS1/#percentage-units">CSS Level 1</a></td> <td style="vertical-align: top;">{{ Spec2('CSS1') }}</td> <td style="vertical-align: top;"> </td> </tr> </tbody>
</table>
<h3 name="See_also">See also</h3> <p>{{ languages( { "fr": "fr/CSS/pourcentage" } ) }}</p>
Revert to this revision