Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

CSS Dictionary

  1. Home
  2. CSS Dictionary
  3. calc()

calc()

Using 'calc()' allows you to perform arithmetic (calculations). In recent versions of Chrome and Safari, the vendor prefix 'webkit' is not required, but older versions may need it. It is also a good idea to specify a fixed value as a fallback for browsers that do not support 'calc()'.

Sample Code

div.hoge{
	width: 200px; /* Fallback for browsers that don't support calc() */
	width: calc(300px - 100xp);
	width: -webkit-calc(300px - 100xp); /* For older Chrome and Safari */
}

Available Values

ValueDescription
 + Performs addition.
 - Performs subtraction.
 * Performs multiplication. Note that one of the arguments must be a 'number' (unitless).
 / Performs division. Note that the right-hand argument must be a 'number' (unitless).

Browser Preview

<div style="width: calc(300px - 100xp); width: -webkit-calc(300px - 100xp); background: #f00;">This is a div element. The width is set to the result of '300px - 100px', which is '200px'.</div>

Browser Support

Chrome Chrome
26+
25
24
23
22
21
20
19
↑ Requires prefix '-webkit-'
18 and earlier ×
Firefox Firefox
16+
15
14
13
12
11
10
9
8
↑ Requires prefix '-moz-'
3 and earlier ×
Safari Safari
7+
6
↑ Requires prefix '-webkit-'
5 and earlier ×
Edge Edge
12+
Supported in all versions
IE IE
11
10
9
8 ×
7 ×
6 ×
Opera Opera
15+
14 and earlier ×
iOS Safari iOS Safari
7+
6
↑ Requires prefix '-webkit-'
5 and earlier ×
Android Android Browser
4.4+
3 and earlier ×
Chrome Android Chrome Android
Latest
Same support as desktop
Firefox Android Firefox Android
Latest
Same support as desktop

※ Version data based on MDN / Can I Use.

※ Android 4.x is supported from 4.4 onwards, so versions 4.3 and earlier cannot use it.

Overview

Using 'calc()' allows you to perform arithmetic (calculations).

The values that 'calc()' can calculate include 'length', 'angle', 'time', 'integer', and 'frequency' (although frequency was removed in CSS 2.1).

<div class="nagasa">This is a div element. The width is set to the result of '300px - 100px', which is '200px'.</div>
<div class="kakudo">This is a div element. The angle is set to the result of '50deg - 40deg', which is '10deg'.</div>
<div class="jikan">This is a div element. The animation duration is set to the result of '5s - 3s', which is '2s'.</div>

/* Sets the width to the result of '300px - 100px' = '200px'. */
div.nagasa{
	width: calc(300px - 100xp);
	width: -webkit-calc(300px - 100xp);
	background: #F00;
}
/* Sets the angle to the result of '50deg - 40deg' = '10deg'. */
div.kakudo{
	width: 200px;
	background: #FF0;
	margin:40px 0;
	transform: rotate(calc(50deg - 40deg));
	-moz-transform: rotate(calc(50deg - 40deg));
	-ms-transform: rotate(calc(50deg - 40deg));
	-webkit-transform: rotate(-webkit-calc(50deg - 40deg));
}
/* Sets the duration to the result of '5s - 3s' = '2s'. */
div.jikan{
	width:200px;
	background: #0F0;
	-webkit-animation:hoge -webkit-calc(5s - 3s) infinite alternate;
	-moz-animation:hoge calc(5s - 3s) infinite alternate;
	animation:hoge calc(5s - 3s) infinite alternate;
}
@-webkitkeyframes hoge { 0%{margin-left:0px;} 100%{margin-left:50px;}}
@-mozkeyframes hoge { 0%{margin-left:0px;} 100%{margin-left:50px;}}
@keyframes hoge { 0%{margin-left:0px;} 100%{margin-left:50px;}}

Calculations that do not divide evenly, such as '100 ÷ 3', can also be computed.

<div style="width:33.33%; width:calc(100% / 3); width:-webkit-calc(100% / 3); background: #f00;">This is a div element. The width is set to the result of '100% ÷ 3'.</div>

Units other than 'px' and '%' such as 'em' and 'cm' can be used, and you can even mix different units in the same calculation. Also, 'calc()' can be nested inside another 'calc()'.

<div style="width: calc(100% - calc(10px + 10px)); width: -webkit-calc(100% - calc(10px + 10px)); background: #f00;">This is a div element. The width is set to the result of 'calc(100% - calc(10px + 10px))'.</div>

If you find any errors or copyright issues, please .