Calc() - Function CSS

calc()

Summary of characteristics of the calc() function

Quick description
Performs a calculation. Can be used in place of a value for a property.
Status
Standard
Percentages
Peuvent être insérés dans l'expression à calculer.
W3C Module
CSS Values and Units Module
 🡇  
 🡅  
Document status: WD (Working Draft)

Document status: CR (Candidate Recommendation)

Description of the calc() function.

The function calc() performs a calculation whose result can be used as the value of a property. The calculation can involve the four basic operations, include parentheses, or use other fonctions mathématiques or fonctions trigonométriques.
The four basic operators are:
    +: addition,
    -: subtraction,
    *: multiplication,
    /: division.

Calculations are performed according to the usual order of priority:
    - First, calculations within parentheses,
    - Next, multiplications and divisions,
    - Finally, additions and subtractions.

The function calc() can also call the function var() , which provides the interesting possibility of inserting the value of a variable into the calculation.

The expression to be calculated can mix units, which is not correct mathematically but is quite practical in our case. However, the expression must remain consistent: you can add or subtract different units as long as they correspond to the same type of physical quantity; for example, it is possible to add pixels (px) and centimeters (cm), but not pixels and angles (deg).
Multiplications and divisions must include at least one operand without a unit.

The result of the expression must be of a type that matches what the property expects. For example, the width property expects a dimension, so the calculated expression must return a dimension if it is used with this property.

Examples of correct expressions:

width:calc(100% - 100px);
If 100% is resolved in one dimension, it is therefore possible to add it to another dimension.
width:calc(100% / 2);
A dimension divided by a number gives a dimension.
width:calc(100 * 1px);
Tip for converting a unitless value into a typed value (here a dimension).

Examples of incorrect expressions:

width:calc(100px - 100);
100px is a dimension, which cannot be added to a number without a dimension.
width:calc(100px - 0);
Even the value 0 must have a unit, even though most properties accept 0 without a unit.
width:calc(45deg - 50px);
The two values are of incompatible types (an angle and a dimension).
width:calc(20px * 20px);
The result would be an area, but the width property does not expect an area but a dimension. In general, multiplication and division must include at least one number without a unit.

Note: spaces before and after the + and - operators are necessary for the expression to be evaluated correctly. Without spaces, the + and - operators are considered to be the sign of the value that follows them.
The * and / operators can be written without spaces, either before or after.

10px - 2px
is 8px.
-10px - 2px
is -12px.
10px + -2px
is 8px.
10px-2px
is incorrect.

Improve CSS code readability.

calc() can be used simply to make the author's intentions clearer. For example, if you want to rotate an element by 1/12 of a turn, you can write rotate:30deg;, but the syntax rotate:calc(1turn / 12); better highlights the fact that it is a rotation of one twelfth of a turn.

calc() and the colors.

Currently, the function calc(), when used alone, cannot calculate with colors, but by using functions like rgb() or hsl(), it is possible to perform calculations on each component of a color.
Example:
color:rgb(calc(255 - 41), calc(255 - 120), calc(255 - 18));

The calc() value can be used with the following properties:

  • Instead of any numeric value, provided that there is a match between the type expected in that place and the type of the result obtained by the function calc().

Syntaxes of the calc() function.

  • property: calc(75% + 30px);

    The function calc() accepts any combination of values and algebraic operators ( + - * / ). The operators + et - must be preceded and followed by a space (they should not be attached to the value before or after them).

  • property: calc(2 * min(300px, 30%, 500px));

    Other calculation functions can be inserted into the expression provided to calc().

Examples of use of the function calc().

Position a gradient.

The example below shows an element with a linear gradient on its right and left sides. We wanted this gradient to be 10 pixels wide, regardless of the total width of the element.

Calculation of the placement of elements in fixed or absolute position.

Centering a block positioned absolutely (position:absolute;) or fixed (position:fixed;) is not easy because the block's position no longer depends on its margins, but only on its four properties top, right, bottom, and left.

The blue block with the word "Example2" has been positioned absolutely. Yet it still remains centered in the width of the browser window. Observe how the value for its left property was determined with calc().

Example2

 

 

 

 

Vertical alignment.

As for the pink block (example3), positioned fixed on the left side of the screen, it remains vertically centered, thanks to the use of the function calc() and the vh (Viewport Height) unit.

Example3

Simulator.

The simulator adjusts the image width using the calc() function. The last option allows you to enter a formula yourself.

width :
Example for the calc() function

Browsers compatibility with calc() function.

The calc() function is now well supported by browsers.

Column 1
Support for calculations in CSS, and the function calc() to evaluate an expression.
Column 2
Support for calc() functions when they are nested.
Column 3
Support for the calc() function used in a @media at-rule.

Notes:

(1) The calc() function is bugged in Internet Explorer.

1
calc()
function
 
2
Nested
calc()
functions
3
calc()
function
in @media
Estimated overall support.
96%
95%
95%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

KaiOS Browser

Samsung Internet

Edge

Firefox

Androïd Brower

Chrome pour Androïd

Baidu Browser

QQ Browser

Safari

Safari sur IOS

Chrome

Opéra

Opéra Mobile

UC Browser pour Androïd

Firefox pour Androïd

Opéra mini

Browsers compatibility with calc() function.

See also: other calculation functions.

The calculation functions available in CSS are becoming increasingly numerous. They cover common mathematical and trigonometric calculations. Here is the list to make your search easier. You can also refer to the W3C specification CSS Values and Units Module.

Properties:

interpolate-size
Langue française
Determines whether animations or transitions should be abrupt or gradual when a keyword defining an intrinsic dimension is used.

Functions:

abs()
Calculates the absolute value of a number.
acos()
Calculates the angle at which the cosine is equal to the specified value.
asin()
Calculates the angle at which the sine is equal to the specified value.
atan()
Calculates the angle whose tangent is equal to the specified value.
atan2()
Calculates the angle between the x-axis and the line connecting the origin point to the specified point.
attr()
Returns the value of an attribute (standard or custom) from HTML or XML code.
Calc()
Performs a calculation. Can be used in place of a value for a property.
calc-size()
Langue française
calc-size() allows calculations on the intrinsic dimension of an element (defined by the content).
clamp()
Langue française
Calculates a value between two extreme values. This function is useful when the units of the parameters are different but of the same type (dimensions, angle...).
cos()
Calculate the cosine of an angle.
exp()
Calculates the exponential of a number.
hypot()
Calculates the square root of a sum of squares of numbers.
if()
Langue française
This function allows you to select a value from a proposed list, each value being associated with a condition.
log()
Calculates the logarithm of a number.
max()
Langue française
Determines the largest value among a series of values.
min()
Langue française
Determines the smallest value among a series of values.
mod()
Langue française
Calculates the remainder of the integer division between two numbers.
pow()
Calculates a power boost.
rem()
Langue française
Calculates the remainder of the integer division between two numbers.
round()
Langue française
Calculates the rounding of a number.
sibling-count()
Langue française
Returns the number of siblings of the element in the document tree, including the element itself.
sibling-index()
Langue française
Returns the index of the element among its siblings, that is, among the other elements that have the same parent.
sign()
Determines the sign of a number.
sin()
Calculate the sine of an angle.
sqrt()
Calculates the square root of a number.
src()
Langue française
Specifies the address of a resource such as an image for example. Synonym of the url() function.
tan()
Calculates the tangent of an angle.
url()
Specifies the address of a resource such as an image for example.

Values:

e
The predefined value of the number e (the basis of natural logarithms), which is approximately 2.7182818284590452354.
infinity
An error value indicating that the result of a calculation is an infinite value.
NaN
An error value indicating that a calculation could not be performed because of a non-numeric parameter.
pi
The preset value of the number pi (about 3.1415926535897932).