calc

  • Revision slug: Web/CSS/calc
  • Revision title: calc
  • Revision id: 486649
  • Created:
  • Creator: bigbossSNK
  • Is current revision? No
  • Comment

Revision Content

{{CSSRef}}{{SeeCompatTable}}

Summary

The calc() CSS function can be used anywhere a {{Xref_csslength}}, {{Xref_cssfrequency}}, {{Xref_cssangle}}, {{Xref_csstime}}, {{Xref_cssnumber}}, or {{Xref_cssinteger}} is required. With calc(), you can perform calculations to determine CSS property values.

It is possible to use calc() inside another calc().

Syntax

calc(expression)

Values

expression
A mathematical expression, the result of which is used as the value.

Expressions

The expression can be any simple expression combining the following operators:

+
Addition.
-
Subtraction.
*
Multiplication. At least one of the arguments must be a {{Xref_cssnumber}}.
/
Division. The right-hand side must be a {{Xref_cssnumber}}.

The operands in the expression may be any length syntax value. You can use different units for each value in your expression, if you wish. You may also use parentheses to establish computation order when needed.

Note: Division by zero results in an error being generated by the HTML parser.
Note: The + and - operators must always be surrounded by whitespace. The operand of calc(50% -8px) for instance will be parsed as a percentage followed by a negative length, an invalid expression, while the operand of calc(50% - 8px) is a percentage followed by a minus sign and a length. Even further, calc(8px + -50%) is treated as a length followed by a plus sign and a negative percentage.
The * and / operators do not require whitespace, but adding it for consistency is allowed, and recommended.

Examples

Positioning an object on screen with a margin

calc() makes it easy to position an object with a set margin. In this example, the CSS creates a banner that stretches across the window, with a 40-pixel gap between both sides of the banner and the edges of the window:

.banner {
  position: absolute;
  left: 40px;
  width: 90%;               /* fallback for browsers without support for calc() */
  width: calc(100% - 80px);
  border: solid black 1px;
  box-shadow: 1px 2px;
  background-color: yellow;
  padding: 6px;
  text-align: center;
}
<div class="banner">This is a banner!</div>

{{ EmbedLiveSample('Positioning_an_object_on_screen_with_a_margin', '100%', '60') }}

Automatically sizing form fields to fit their container

Another use case for calc() is to help ensure that form fields fit in the available space, without extruding past the edge of their container, while maintaining an appropriate margin.

Let's look at some CSS:

input {
  padding: 2px;
  display: block;
  width: 98%;               /* fallback for browsers without support for calc() */
  width: calc(100% - 1em);
}

#formbox {
  width: 130px;             /* fallback for browsers without support for calc() */
  width: calc(100% / 6);
  border: 1px solid black;
  padding: 4px;
}

Here, the form itself is established to use 1/6 of the available window width. Then, to ensure that input fields retain an appropriate size, we use calc() again to establish that they should be the width of their container minus 1em. Then, the following HTML makes use of this CSS:

<form>
  <div id="formbox">
  <label>Type something:</label>
  <input type="text">
  </div>
</form>

{{ EmbedLiveSample('Automatically_sizing_form_fields_to_fit_their_container', '100%', '80') }}

Specifications

Specification Status Comment
{{SpecName('CSS3 Values', '#calc-notation', 'calc()')}} {{Spec2('CSS3 Values')}}  

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 19 (WebKit 536.3) {{property_prefix("-webkit")}}
26
{{CompatGeckoDesktop("2")}} {{property_prefix("-moz")}}
{{CompatGeckoDesktop("16")}}
9 - 6 {{property_prefix("-webkit")}} (buggy)
On gradients' color stops 19 (WebKit 536.3) {{property_prefix("-webkit")}}
27 (maybe 26)
{{CompatGeckoDesktop("19")}} 9 - 6 {{property_prefix("-webkit")}} (buggy)
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support <= 2.3.6 not supported {{CompatGeckoMobile("2")}} {{property_prefix("-moz")}}
{{CompatGeckoMobile("16")}}
{{CompatUnknown}} {{CompatUnknown}} <= IOs 6.1.3 not supported
On gradients' color stops {{CompatUnknown}} {{CompatGeckoMobile("19")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

Revision Source

<div>
 {{CSSRef}}{{SeeCompatTable}}</div>
<h2 id="Summary">Summary</h2>
<p>The <code>calc()</code> CSS function can be used anywhere a {{Xref_csslength}}, {{Xref_cssfrequency}}, {{Xref_cssangle}}, {{Xref_csstime}}, {{Xref_cssnumber}}, or {{Xref_cssinteger}} is required. With <code>calc()</code>, you can perform calculations to determine CSS property values.</p>
<p>It is possible to use <code>calc()</code> inside another <code>calc()</code>.</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">
calc(<em>expression</em>)</pre>
<h3 id="Values">Values</h3>
<dl>
 <dt>
  expression</dt>
 <dd>
  A mathematical expression, the result of which is used as the value.</dd>
</dl>
<h3 id="Expressions">Expressions</h3>
<p>The expression can be any simple expression combining the following operators:</p>
<dl>
 <dt>
  <strong>+</strong></dt>
 <dd>
  Addition.</dd>
 <dt>
  <strong>-</strong></dt>
 <dd>
  Subtraction.</dd>
 <dt>
  <strong>*</strong></dt>
 <dd>
  Multiplication. At least one of the arguments must be a {{Xref_cssnumber}}.</dd>
 <dt>
  <strong>/</strong></dt>
 <dd>
  Division. The right-hand side must be a {{Xref_cssnumber}}.</dd>
</dl>
<p>The operands in the expression may be any length syntax value. You can use different units for each value in your expression, if you wish. You may also use parentheses to establish computation order when needed.</p>
<div class="note">
 <strong>Note:</strong> Division by zero results in an error being generated by the HTML parser.</div>
<div class="note">
 <strong>Note:</strong> The <code>+</code> and <code>-</code> operators must always be surrounded by whitespace. The operand of <code>calc(50% -8px)</code> for instance will be parsed as a percentage followed by a negative length, an invalid expression, while the operand of <code>calc(50% - 8px)</code> is a percentage followed by a minus sign and a length. Even further, <code>calc(8px + -50%)</code> is treated as a length followed by a plus sign and a negative percentage.<br />
 The <code>*</code> and <code>/</code> operators do not require whitespace, but adding it for consistency is allowed, and recommended.</div>
<dl>
</dl>
<h2 id="Examples">Examples</h2>
<h3 id="Positioning_an_object_on_screen_with_a_margin">Positioning an object on screen with a margin</h3>
<p><code>calc()</code> makes it easy to position an object with a set margin. In this example, the CSS creates a banner that stretches across the window, with a 40-pixel gap between both sides of the banner and the edges of the window:</p>
<pre class="brush: css">
.banner {
  position: absolute;
  left: 40px;
  width: 90%;               /* fallback for browsers without support for calc() */
  width: calc(100% - 80px);
  border: solid black 1px;
  box-shadow: 1px 2px;
  background-color: yellow;
  padding: 6px;
  text-align: center;
}
</pre>
<pre class="brush: html">
&lt;div class="banner"&gt;This is a banner!&lt;/div&gt;</pre>
<p>{{ EmbedLiveSample('Positioning_an_object_on_screen_with_a_margin', '100%', '60') }}</p>
<h3 id="Automatically_sizing_form_fields_to_fit_their_container">Automatically sizing form fields to fit their container</h3>
<p>Another use case for <code>calc()</code> is to help ensure that form fields fit in the available space, without extruding past the edge of their container, while maintaining an appropriate margin.</p>
<p>Let's look at some CSS:</p>
<pre class="brush: css">
input {
  padding: 2px;
  display: block;
  width: 98%;               /* fallback for browsers without support for calc() */
  width: calc(100% - 1em);
}

#formbox {
  width: 130px;             /* fallback for browsers without support for calc() */
  width: calc(100% / 6);
  border: 1px solid black;
  padding: 4px;
}
</pre>
<p>Here, the form itself is established to use 1/6 of the available window width. Then, to ensure that input fields retain an appropriate size, we use <code>calc()</code> again to establish that they should be the width of their container minus 1em. Then, the following HTML makes use of this CSS:</p>
<pre class="brush: html">
&lt;form&gt;
  &lt;div id="formbox"&gt;
  &lt;label&gt;Type something:&lt;/label&gt;
  &lt;input type="text"&gt;
  &lt;/div&gt;
&lt;/form&gt;
</pre>
<p>{{ EmbedLiveSample('Automatically_sizing_form_fields_to_fit_their_container', '100%', '80') }}</p>
<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', '#calc-notation', 'calc()')}}</td>
   <td>{{Spec2('CSS3 Values')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<div>
 {{CompatibilityTable}}</div>
<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>19 (WebKit 536.3) {{property_prefix("-webkit")}}<br />
     26</td>
    <td>{{CompatGeckoDesktop("2")}} {{property_prefix("-moz")}}<br />
     {{CompatGeckoDesktop("16")}}</td>
    <td>9</td>
    <td>-</td>
    <td>6 {{property_prefix("-webkit")}} (buggy)</td>
   </tr>
   <tr>
    <td>On gradients' color stops</td>
    <td>19 (WebKit 536.3) {{property_prefix("-webkit")}}<br />
     27 (maybe 26)</td>
    <td>{{CompatGeckoDesktop("19")}}</td>
    <td>9</td>
    <td>-</td>
    <td>6 {{property_prefix("-webkit")}} (buggy)</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 Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>&lt;= 2.3.6 not supported</td>
    <td>{{CompatGeckoMobile("2")}} {{property_prefix("-moz")}}<br />
     {{CompatGeckoMobile("16")}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>&lt;= IOs 6.1.3 not supported</td>
   </tr>
   <tr>
    <td>On gradients' color stops</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatGeckoMobile("19")}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li><a href="https://hacks.mozilla.org/2010/06/css3-calc/" title="Firefox 4: CSS3 calc() ✩ Mozilla Hacks – the Web developer blog">Firefox 4: CSS3 calc() ✩ Mozilla Hacks – the Web developer blog</a></li>
</ul>
Revert to this revision