calc()
Quick Summary for calc()
The calc() CSS function lets you perform calculations when specifying CSS property values. It can be used anywhere a <length>, <frequency>, <angle>, <time>, <percentage>, <number>, or <integer> is allowed.
Code Usage for calc()
/* property: calc(expression) */ width: calc(100% - 80px); 
More Details for calc()

calc()

The calc() CSS function lets you perform calculations when specifying CSS property values. It can be used anywhere a <length>, <frequency>, <angle>, <time>, <percentage>, <number>, or <integer> is allowed.

Syntax

/* property: calc(expression) */ width: calc(100% - 80px); 

The calc() function takes a single expression as its parameter, with the expression's result used as the value. The expression can be any simple expression combining the following operators, using standard operator precedence rules:

+

Addition.

-

Subtraction.

*

Multiplication. At least one of the arguments must be a <number>.

/

Division. The right-hand side must be a <number>.

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.

Notes

The + and - operators must be surrounded by whitespace. For instance, calc(50% -8px) will be parsed as a percentage followed by a negative length — an invalid expression — while calc(50% - 8px) is a percentage followed by a subtraction operator and a length. Likewise, calc(8px + -50%) is treated as a length followed by an addition operator and a negative percentage. The * and / operators do not require whitespace, but adding it for consistency is both allowed and recommended. Division by zero results in an error being generated by the HTML parser. Math expressions involving percentages for widths and heights on table columns, table column groups, table rows, table row groups, and table cells in both auto and fixed layout tables may be treated as if auto had been specified. It is permitted to nest calc() functions, in which case the inner ones are treated as simple parentheses.

Formal syntax

calc( <calc-sum> )

where <calc-sum> = <calc-product> [ [ '+' | '-' ] <calc-product> ]*

where <calc-product> = <calc-value> [ '*' <calc-value> | '/' <number> ]*

where <calc-value> = <number> | <dimension> | <percentage> | ( <calc-sum> )

Accessibility concerns

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.

MDN Understanding WCAG, Guideline 1.4 explanations Understanding Success Criterion 1.4.4 | W3C Understanding WCAG 2.0

Usage with integers

When calc() is used where an <integer> is expected, the value will be rounded to the nearest integer. For example:

.modal {   z-index: calc(3 / 2); } 

This will give .modal a final z-index value of 2.

Note: The Chrome browser currently won't accept some values returned by calc() when an integer is expected. This includes any division, even if it results in an integer. ie. z-index: calc(4 / 2); will not be accepted.

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> 

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); }  #formbox {   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> 

Nested calc() with CSS Variables

You can also 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), then when it's assigned to .foo's width property, all inner calc()s (no matter how deeply nested) will be flattened to just parentheses, so the width property's value will be eventually calc( ( 100px / 2) / 2), i.e. 25px. In short: a calc() inside of a calc() is identical to just parentheses.

Specifications

Specification
CSS Values and Units Module Level 5 # calc-func

See also

Firefox 4: CSS3 calc() ✩ Mozilla Hacks – the Web developer blog

Last modified: Nov 16, 2021, by MDN contributors

Select your preferred language English (US)DeutschEspañolFrançais日本語한국어Português (do Brasil)Русский中文 (简体) Change language

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote

"Olivia, my eldest daughter, caught measles when she was seven years old. As the illness took its usual course I can remember reading to her often in bed and not feeling particularly alarmed about it. Then one morning, when she was well on the road to recovery, I was sitting on her bed showing her how to fashion little animals out of coloured pipe-cleaners, and when it came to her turn to make one herself, I noticed that her fingers and her mind were not working together and she couldn’t do anything. 'Are you feeling all right?' I asked her. 'I feel all sleepy,' she said. In an hour, she was unconscious. In twelve hours she was dead. The measles had turned into a terrible thing called measles encephalitis and there was nothing the doctors could do to save her. That was...in 1962, but even now, if a child with measles happens to develop the same deadly reaction from measles as Olivia did, there would still be nothing the doctors could do to help her. On the other hand, there is today something that parents can do to make sure that this sort of tragedy does not happen to a child of theirs. They can insist that their child is immunised against measles. ...I dedicated two of my books to Olivia, the first was ‘James and the Giant Peach’. That was when she was still alive. The second was ‘The BFG’, dedicated to her memory after she had died from measles. You will see her name at the beginning of each of these books. And I know how happy she would be if only she could know that her death had helped to save a good deal of illness and death among other children."

I just checked google books for BFG, and the dedication is there. 

https://www.google.com.au/books/edition/_/quybcXrFhCIC?hl=en&gbpv=1 


Roald Dahl, 1986
Random CSS Property

scroll-margin-right

The scroll-margin-right property defines the right margin of the scroll snap area that is used for snapping this box to the snapport. The scroll snap area is determined by taking the transformed border box, finding its rectangular bounding box (axis-aligned in the scroll container's coordinate space), then adding the specified outsets.
scroll-margin-right css reference