docs.rodeo

MDN Web Docs mirror

calc()

{{CSSRef}} 

The calc() CSS function lets you perform calculations when specifying CSS property values. It can be used with {{cssxref("<length>")}} , {{cssxref("<frequency>")}} , {{cssxref("<angle>")}} , {{cssxref("<time>")}} , {{cssxref("<percentage>")}} , {{cssxref("<number>")}} , {{cssxref("<integer>")}} , and {{cssxref("color_value", "<color-function>")}}  values.

{{EmbedInteractiveExample("pages/css/function-calc.html")}} 

Syntax

/* calc(expression) */
calc(100% - 80px)

/* Expression with a CSS function */
calc(100px * sin(pi / 2))

/* Expression containing a variable */
calc(var(--hue) + 180)

/* Expression with color channels in relative colors */
lch(from aquamarine l c calc(h + 180))

The calc() function takes a single expression as its parameter, and the expression’s result is used as the value for a CSS property. In this expression, the {{Glossary("operand", "operands")}}  can be combined using the {{Glossary("operator", "operators")}}  listed below. When the expression contains multiple operands, calc() uses the standard operator precedence rules:

All operands, except those of type {{cssxref("<number>")}} , must be suffixed with an appropriate unit string, such as px, em, or %. You can use a different unit with each operand in your expression. You may also use parentheses to establish computation order when needed.

Description

There are a few points to note about calc(), which are detailed in the sections below.

Resulting values

The calc() function must stand in place of a full CSS value of one of the following types:

calc() cannot only replace the numeric part of percentage values, length values, etc., without also replacing the unit after it. For example, calc(100 / 4)% is invalid, while calc(100% / 4) is valid.

The resulting value of calc() must be compatible with the context in which it is used. For example, margin: calc(1px + 2px) is valid, but margin: calc(1 + 2) is not: it is equivalent to specifying margin: 3, which results in the property being ignored.

When an {{cssxref("&lt;integer&gt;")}}  is expected, the calc() expression can also evaluate to a <number>, which gets rounded to the nearest integer. So, calc(1.4) will result in a value of 1. If the fractional part of the value is exactly 0.5, the value is rounded towards positive infinity. For example, calc(1.5) will result in a value of 2, while calc(-1.5) will round to -1.

calc() performs floating point math following the IEEE-754 standard, which results in some considerations concerning the infinity and NaN values. For more details on how constants are serialized, see the calc-keyword page.

Input considerations

Support for computing color channels in relative colors

The calc() function can be used to manipulate color channels directly within the context of relative colors. This allows for dynamic adjustments of color channels in color models such as rgb(), hsl(), and lch().

The relative color syntax defines several color-channel keywords, each of which represents the value of the color channel as a {{cssxref("&lt;number&gt;")}}  (see Channel values resolve to <number> values for more information). The calc() function can use these color-channel keywords to perform dynamic adjustments on the color channels, for example, calc(r + 10).

Formal syntax

{{csssyntax}} 

Accessibility

When calc() is used for controlling text size, be sure that one of the values includes a relative length unit, for example:

h1 {
  font-size: calc(1.5rem + 3vw);
}

This ensures that text size will scale if the page is zoomed.

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: calc(100% - 80px);
  border: solid black 1px;
  box-shadow: 1px 2px;
  background-color: yellow;
  padding: 6px;
  text-align: center;
  box-sizing: border-box;
}
<div class="banner">This is a banner!</div>

{{EmbedLiveSample('Positioning_an_object_on_screen_with_a_margin', 'auto', '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: calc(100% - 1em);
}

#form-box {
  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="form-box">
    <label for="misc">Type something:</label>
    <input type="text" id="misc" name="misc" />
  </div>
</form>

{{EmbedLiveSample('Automatically_sizing_form_fields_to_fit_their_container', '700', '80')}} 

Nesting with CSS variables

You can use calc() with CSS variables. Consider the following code:

.foo {
  --widthA: 100px;
  --widthB: calc(var(--widthA) / 2);
  --widthC: calc(var(--widthB) / 2);
  width: var(--widthC);
}

After all variables are expanded, widthC’s value will be calc(calc(100px / 2) / 2). When it’s assigned to .foo’s width property, all inner calc() functions (no matter how deeply nested) will be flattened to just parentheses. Therefore, the width property’s value will eventually be calc((100px / 2) / 2), which equals 25px. In short, a calc() inside of a calc() is identical to using parentheses.

Adjusting color channels in relative colors

The calc() function can be used to adjust individual color channels in relative colors without the need for storing color channel values as variables.

In the example below, the first paragraph uses a <named-color>. In the paragraphs that follow, calc() is used with the rgb() and hsl() functions to adjust the values of each color channel relative to the original named color.

<p class="original">Original text color in rebeccapurple</p>
<p class="increase-hue">Hue increased by 80</p>
<p class="increase-lightness">Lightness increased by 20</p>
<p class="decrease-lightness">Lightness decreased by 10</p>
p {
  font-family: monospace;
  font-size: 16px;
}
.original {
  color: rebeccapurple;
}

.increase-hue {
  color: lch(from rebeccapurple l c calc(h + 80));
}

.increase-lightness {
  color: lch(from rebeccapurple calc(l + 20) c h);
}

.decrease-lightness {
  color: lch(from rebeccapurple calc(l - 10) c h);
}

{{EmbedLiveSample('Adjusting color channels in relative colors', '700', '300')}} 

For another example of using the calc() function to derive relative colors, see the Using math functions section on the Using relative colors page.

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN