Favorites

calc() CSS Function

calc()
CSS FunctionThis function is supported by all modern browsers.
Updated 21st November 2026

The calc() function does math in CSS property values, adding, subtracting, multiplying and dividing values that can mix units, such as 100% - 20px.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

Description

The calc() CSS function allows you to perform mathematical calculations directly within CSS property values. It enables dynamic value adjustments by combining different units, such as percentages, pixels, ems, rems, or viewport units, in a single expression. This is particularly useful when you need to create responsive layouts or when a value cannot be expressed with a single static unit. Unlike hard-coded values, calc() allows you to mix relative and absolute units - for example, combining width in percentages with px offsets to maintain a flexible design.

The function supports the four basic mathematical operations: addition (+), subtraction (−), multiplication (*), and division (/). Multiplication and division are restricted to numeric values, while addition and subtraction can be applied to compatible units. You can also use parentheses to group calculations and control the order of operations, similar to regular mathematics.

A common use case is adjusting an element’s dimensions while factoring in padding or margins. For instance, to create a div that fills the remaining width of its container minus a fixed sidebar width, you could use:

.main-content {
  width: calc(100% - 250px);
}

Another practical example is responsive font sizing, where you combine viewport units with a base font size:

h1 {
  font-size: calc(1rem + 2vw);
}

Here, the font scales dynamically with the viewport width while maintaining a minimum size defined in rem.

calc() can also work with margin, padding, top, left, and other properties that accept numeric values. This makes it a versatile tool for creating fluid, adaptable designs without relying entirely on JavaScript.

Interactive Demo

050px100px150px200px250px
Demo Code
HTML Code
<div class="demo-track">
	<div class="demo-element"></div>
</div>
<div class="demo-scale"><span>0</span><span>50px</span><span>100px</span><span>150px</span><span>200px</span><span>250px</span></div>
CSS Code
.demo-track {
	width: 300px;
	height: 36px;
	background: #f4f4f4 repeating-linear-gradient(90deg, #b0b0b0 0 1px, transparent 1px 50px);
	outline: 1px solid #c5c5c5;
}
.demo-scale {
	display: grid;
	grid-template-columns: repeat(6, 50px);
	width: 300px;
	font-size: 11px;
	color: #666;
}
.demo-element {
	height: 100%;
	background: linear-gradient(90deg, #f72585, #b5179e);
	width: calc(100% - 100px);
}

Syntax

calc() = calc( <calc-sum> )
<calc-sum> = <calc-product> [ [ '+' | '-' ] <calc-product> ] *
<calc-product> = <calc-value> [ '*' <calc-value> | '/' <calc-number-value> ] *
<calc-value> = <number> | <dimension> | <percentage> | ( <calc-sum> )
<calc-number-sum> = <calc-number-product> [ [ '+' | '-' ] <calc-number-product> ] *
<calc-number-product> = <calc-number-value> [ '*' <calc-number-value> | '/' <calc-number-value> ] *
<calc-number-value> = <number> | ( <calc-number-sum> )

Values

  • +Addition, for example width: calc(20px + 2rem);. The + operator must have a space on each side.
  • -Subtraction, for example padding: calc(10% - 10px);. The - operator must have a space on each side.
  • *Multiplication, for example height: calc(20% * 2);. At least one side must be a plain <number>.
  • /Division, for example width: calc(100% / 3);. The right-hand side must be a <number>. Dividing by zero makes the expression invalid.

Example

<div class="container">
<div class="sidebar">Sidebar</div>
<div class="content">Main Content</div>
</div>
/* General styling for visibility */
.container {
display: flex;
height: 200px;
width: 100%;
border: 2px solid #333;
}

.sidebar {
width: 150px; /* Fixed width */
background-color: #f4f4f4;
padding: 10px;
}

.content {
/* Using calc() to mix units: 100% minus 150px */
width: calc(100% - 150px);

background-color: #007bff;
color: white;
padding: 20px;
}
Open in editor

Browser Support

The following information will show you the current browser support for the CSS calc() function. Hover over a browser icon to see the version that first introduced support for this CSS function.

This function is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee