calc-size() CSS Function
The calc-size() function does calculations with intrinsic sizes such as auto and fit-content, and makes it possible to animate to height: auto.
Description
The calc-size() CSS function lets you perform calculations on intrinsic size keywords such as auto, min-content, max-content and fit-content, which calc() cannot use. Its most important benefit is that it makes these keywords animatable: for the first time, you can transition an element's height from 0 to its natural auto height, which is exactly what accordions, disclosure widgets and expanding menus need.
The first argument is the basis, usually a size keyword. The optional second argument is a calculation in which the keyword size stands for the resolved size of that basis. For example, calc-size(auto, size + 20px) means "the auto height plus 20px", and calc-size(max-content, size * 0.5) means "half the max-content width". If the second argument is left out, the result is simply the basis size. It can be used in sizing properties such as width, height, min-height, max-width and flex-basis.
/* Smoothly expand a panel to its natural height */
.panel {
height: 0;
overflow: clip;
transition: height 0.4s ease;
}
.panel.open {
height: calc-size(auto, size);
}
/* Natural width plus some extra room */
.badge {
width: calc-size(fit-content, size + 2rem);
}
An alternative is the interpolate-size: allow-keywords property on :root, which allows transitions to and from size keywords throughout the page. calc-size() is currently supported only in Chromium-based browsers, so treat the animation as an enhancement.
Interactive Demo
Syntax
calc-size() = calc-size( <calc-size-basis>, <calc-sum>? )
<calc-size-basis> = <size-keyword> | <calc-size()> | any | <calc-sum>
Values
- <calc-size-basis>The size to calculate with: an intrinsic keyword such as auto, min-content, max-content, fit-content or stretch, another calc-size(), the keyword any, or a calculation.
- <calc-sum>Optional. A calculation in which the keyword size represents the resolved size of the basis, for example size + 20px or size * 0.5. If omitted, the result is the basis size itself.
Example
Browser Support
The following information will show you the current browser support for the CSS calc-size() function. Hover over a browser icon to see the version that first introduced support for this CSS function.
Desktop





Tablets & Mobile





