CSS math functions allow a property value - such as the height
, animation-duration
, or font-size
of an element - to be written as a mathematical expression.
Without using any math, the built-in CSS units like rem
, vw
, and %
are often flexible enough to style HTML elements to achieve a particular user experience.
However, there are cases where we might feel limited by expressing an element's style using a single value and unit. Consider the following examples:
font-size
of some text from growing beyond a certain size.In all of these cases, we need to rely on math to achieve the desired outcomes. One solution could be to rely on mathematical functions defined by JavaScript, and dynamically set element styles based on results calculated by our scripts.
In many instances, including in the examples above, we can instead utilize math functions built directly into CSS. This solution is often simpler to implement and faster for the browser to execute than using JavaScript.
In total, developers can use a combination of nearly two dozen CSS math functions in their stylesheets. In this guide, we'll exemplify four of the more commonly-used, and introduce those more advanced.
calc()
: Basic math operations
In the first two of our three examples above, we want to set the style of an element according to the result of an addition or subtraction operation. This is exactly one of the use cases for calc()
.
The calc()
function lets you specify CSS property values using addition, subtraction, multiplication, and division. It is often used to combine two CSS values that have different units, such as %
and px
.
The calc()
math function takes a mathematical expression as a parameter and returns the result of that expression, e.g.:
property: calc(expression);
calc()
Example
Click on the play icon below to see the calc()
example in the code playground and try it for yourself.
<div class="calc1">
<code>width: calc(10px + 100px);</code>
</div>
<div class="calc2">
<code>width: calc(2em * 5);</code>
</div>
<div class="calc3">
<code>width: calc(100% - 32px);</code>
</div>
<div class="calc4">
<code>width: calc(var(--predefined-width) - calc(16px * 2));</code>
</div>
div {
background-color: black;
margin: 4px 0;
width: 100%;
}
div > code {
display: block;
background-color: red;
color: white;
height: 48px;
}
.calc1 > code {
/* Output width: `110px` */
width: calc(10px + 100px);
}
.calc2 > code {
/* Output width: `10em` */
width: calc(2em * 5);
}
.calc3 > code {
/* Output width: Depends on the container's width */
width: calc(100% - 32px);
}
.calc4 > code {
--predefined-width: 100%;
/* Output width: Depends on the container's width */
width: calc(var(--predefined-width) - calc(16px * 2));
}
min()
: Finding the minimum value in a set
There are cases where we don't want the value of a CSS property to exceed a certain number. Say, for example, we want the width of our content container to be the smaller of "the full width of our screen" and "500 pixels." In those cases, we can use the CSS math function min()
.
The min()
math function takes a set of comma-separated values as arguments and returns the smallest of those values, e.g.:
width: min(32px, 50%, 2rem);
This function is often used to compare two CSS values that have different units, such as %
and px
.
min()
Example
Click on the play icon below to see the min()
example in the code playground and try it for yourself.
<div class="min1">
<code>width: min(9999px, 50%);</code>
</div>
<div class="min2">
<code>width: min(9999px, 100%);</code>
</div>
<div class="min3">
<code>width: min(120px, 150px, 90%);</code>
</div>
<div class="min4">
<code>width: min(80px, 90%);</code>
</div>
div {
background-color: black;
margin: 4px 0;
width: 100%;
}
div > code {
display: block;
background-color: darkblue;
color: white;
height: 48px;
}
.min1 > code {
/* Output width: Depends on the container's width; */
/* on this page, likely to be `50%` of the container's width */
width: min(9999px, 50%);
}
.min2 > code {
/* Output width: Depends on the container's width; */
/* on this page, likely to be `100%` of the container's width */
width: min(9999px, 100%);
}
.min3 > code {
/* Output width: Depends on the container's width; */
/* on this page, likely to be `120px` of the container's width */
width: min(120px, 150px, 90%);
}
.min4 > code {
/* Output width: Depends on the container's width; */
/* on this page, likely to be `80px` of the container's width */
width: min(80px, 90%);
}
max()
: Finding the maximum value in a set
Similar to min()
, sometimes we don't want the value of a CSS property to go below a certain number. For example, we might want the width of our content container to be the larger of "the full width of our screen" and "500 pixels." In those cases, we can use the CSS math function max()
.
The max()
math function takes a set of comma-separated values as arguments and returns the largest of those values, e.g.:
width: max(32px, 50%, 2rem);
This function is often used to compare two CSS values that have different units, such as %
and px
.
Notice the similarities and differences between the examples for min()
and max()
.
max()
Example
Click on the play icon below to see the max()
example in the code playground and try it for yourself.
<div class="max1">
<code>width: max(50px, 50%);</code>
</div>
<div class="max2">
<code>width: max(50px, 100%);</code>
</div>
<div class="max3">
<code>width: max(20px, 50px, 90%);</code>
</div>
<div class="max4">
<code>width: max(80px, 80%);</code>
</div>
div {
background-color: black;
margin: 4px 0;
width: 100%;
height: 48px;
}
div > code {
display: block;
background-color: darkmagenta;
color: white;
height: 48px;
}
.max1 > code {
/* Output width: Depends on the container's width; */
/* on this page, likely to be `50%` of the container's width */
width: max(50px, 50%);
}
.max2 > code {
/* Output width: Depends on the container's width; */
/* on this page, likely to be `100%` of the container's width */
width: max(50px, 100%);
}
.max3 > code {
/* Output width: Depends on the container's width; */
/* on this page, likely to be `90%` of the container's width */
width: max(20px, 50px, 90%);
}
.max4 > code {
/* Output width: Depends on the container's width; */
/* on this page, likely to be `80%` of the container's width */
width: max(80px, 80%);
}
clamp()
: Constraining a value between two values
We can combine the functions of min()
and max()
by using clamp()
. The clamp()
math function takes a minimum value, the value to be clamped, and the maximum value as arguments, e.g.:
/* clamped value: 50%, minimum: 100px, maximum: 300px */
width: clamp(100px, 50%, 300px);
This function is often used to compare two CSS values that have different units, such as %
and px
.
clamp()
Example
Click on the play icon below to see the clamp()
example in the code playground and try it for yourself.
<div class="clamp1">
<code>width: clamp(10%, 1px, 90%);</code>
</div>
<div class="clamp2">
<code>width: clamp(10%, 9999px, 90%);</code>
</div>
<div class="clamp3">
<code>width: clamp(125px, 1px, 250px);</code>
</div>
<div class="clamp4">
<code>width: clamp(25px, 9999px, 150px);</code>
</div>
div {
background-color: black;
margin: 4px 0;
width: 100%;
height: 48px;
}
div > code {
display: block;
background-color: darkgreen;
color: white;
height: 48px;
}
.clamp1 > code {
/* Output width: Depends on the container's width; */
/* on this page, likely to be `20%` of the container's width */
width: clamp(20%, 1px, 80%);
}
.clamp2 > code {
/* Output width: Depends on the container's width; */
/* on this page, likely to be `90%` of the container's width */
width: clamp(10%, 9999px, 90%);
}
.clamp3 > code {
/* Output width: `125px` */
width: clamp(125px, 1px, 250px);
}
.clamp4 > code {
/* Output width: `150px` */
width: clamp(25px, 9999px, 150px);
}
Advanced CSS Math Functions
When laying out and styling DOM elements, the four basic math functions calc()
, min()
, max()
, and clamp()
are often sufficient. However, for advanced uses like mathematics learning materials, 3D visualizations, or CSS animations, you may consider using:
sin()
: calculates the trigonometric sine of a numbercos()
: calculates the trigonometric cosine of a numbertan()
: calculates the trigonometric tangent of a numberasin()
: calculates the trigonometric inverse sine of a numberacos()
: calculates the trigonometric inverse cosine of a numberatan()
: calculates the trigonometric inverse tangent of a numberatan2()
: calculates the trigonometric inverse tangent given two numberspow()
: calculates a number raised to the power of another numbersqrt()
: calculates the square root of a numberhypot()
: calculates the square root of the sum of the squares of the given numberslog()
: calculates the logarithm of a number (with e
as the default base)exp()
: calculates e
raised to the power of another numberRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4