calc

  • Revision slug: CSS/calc
  • Revision title: calc
  • Revision id: 355699
  • Created:
  • Creator: trevorh
  • Is current revision? No
  • Comment Minor fix

Revision Content

{{CSSRef}}{{SeeCompatTable}}

Summary

The calc() CSS function can be used anywhere a {{Xref_csslength}} is required. With calc(), you can perform calculations to determine the size and shape of objects.

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 {{Xref_csslength}}.

Expressions

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

+
Addition
-
Subtraction
*
Multiplication
/
Division. You can't divide by a length; instead, this is used to, for example, say that you want your resulting length to be a given fraction of an existing length.

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.
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")}} {{CompatGeckoDesktop("2")}} {{property_prefix("-moz")}}
{{CompatGeckoDesktop("16")}}
9 - 6 {{property_prefix("-webkit")}} (buggy)
On gradients' color stops 19 (WebKit 536.3) {{property_prefix("-webkit")}} {{CompatGeckoDesktop("19")}} 9 - 6 {{property_prefix("-webkit")}} (buggy)
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatGeckoMobile("2")}} {{property_prefix("-moz")}}
{{CompatGeckoMobile("16")}}
{{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
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}} is required. With <code>calc()</code>, you can perform calculations to determine the size and shape of objects.</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 {{Xref_csslength}}.</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</dd>
  <dt>
    <strong>/</strong></dt>
  <dd>
    Division. You can't divide by a length; instead, this is used to, for example, say that you want your resulting length to be a given fraction of an existing length.</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.<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>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")}}</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")}}</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>{{CompatUnknown}}</td>
        <td>{{CompatGeckoMobile("2")}} {{property_prefix("-moz")}}<br />
          {{CompatGeckoMobile("16")}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</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