CSS math functions allow you to perform calculations directly in CSS, enabling responsive layouts, dynamic sizing, flexible positioning, and more advanced design logic.
They include:
calc()min()max()clamp()sin(), cos(), tan() (CSS trigonometric functions)asin(), acos(), atan(), atan2()pow(), sqrt(), hypot(), log(), exp()round(), mod(), rem(), sign()This guide focuses first on the fully supported ones, then introduces advanced ones.
Math functions help you:
calc(): The Workhorse of CSS Mathcalc() performs arithmetic in CSS using + − × ÷.
width: calc(100% - 40px); .card {
width: calc(50% - 2rem);
padding: 1rem;
background: #222;
color: white;
}
.box {
position: absolute;
top: calc(50% - 100px / 2);
left: calc(50% - 100px / 2);
width: 100px;
height: 100px;
background: purple;
}
.hero {
height: calc(100vh - 60px); /* responsive viewport minus header */} min(): Choose the Smallest Valuemin() evaluates multiple values and picks the smallest.
img {
width: min(90%, 400px);
} .title {
font-size: min(5vw, 32px);
} This allows fluid scaling without media queries.
max(): Choose the Largest Valuemax() picks the largest among multiple values.
.box {
width: max(300px, 40vw);
} .section {
padding: max(1rem, 4vw);
} clamp(): Min + Preferred + Maxclamp(min, ideal, max) restricts a value between a lower and upper bound.
h1 {
font-size: clamp(1.8rem, 5vw, 3.5rem);
} Meaning:
button {
padding: clamp(0.5rem, 2vw, 1.2rem);
} .sidebar {
width: clamp(200px, 25vw, 350px);
} Modern CSS now supports real mathematical functions, including trigonometry, powers, logs, rounding, etc.
Support varies across browsers.
sin(), cos(), tan()These are great for animations, rotations, and circular layouts.
@keyframes orbit {
from {
transform: translate(calc(100px * cos(0deg)), calc(100px * sin(0deg)));
}
to {
transform: translate(calc(100px * cos(360deg)), calc(100px * sin(360deg)));
}
}
.planet {
position: absolute;
animation: orbit 4s infinite linear;
}
pow(), sqrt().box {
scale: calc(sqrt(16)); /* = 4 */} div {
width: calc(pow(2, 5) * 1px); /* 2^5 = 32px */} log(), exp()Useful for natural-feeling motion curves.
.element {
opacity: calc(1 / log(10));
} round(), mod(), rem(), sign().item:nth-child(odd) {
left: calc(mod(100px, 60px));
} .card {
width: clamp(250px, 50vw, 600px);
padding: min(2rem, 5vw);
margin: calc(2rem + 1vh);
border-radius: max(10px, 2vw);
background: linear-gradient(
135deg,
hsl(calc(200 + 50 * sin(30deg)), 80%, 60%),
hsl(calc(200 + 50 * cos(30deg)), 80%, 50%)
);
} This card:
clamp() for responsive widthmin() for flexible paddingmax() for rounded cornerscalc() for mixed-unit marginshsl() for dynamic color effectsCSS math functions help you:
| Function | Purpose |
|---|---|
calc() | Combine values with arithmetic |
min() | Choose the smallest |
max() | Choose the largest |
clamp() | Set min, ideal, max |
sin()/cos()/tan() | Trigonometry-based animations |
pow()/sqrt() | Power and roots |
log()/exp() | Scientific & natural curves |
round()/mod()/rem() | Rounding and remainder logic |
They enable more dynamic, elegant, responsive, and scalable CSS.
Latest tech news and coding tips.
Download the Codeflare iOS app and learn on the Go 1. What UI and UX…
1. Running Everything as Root One of the biggest beginner errors. Many new users log…
A keylogger is a type of surveillance software or hardware that records every keystroke made…
In JavaScript, it’s commonly used for: Recursive functions (like Fibonacci) Heavy calculations Repeated API/data processing…
For years, responsive design has depended almost entirely on media queries. We ask questions like: “If…
1. What is Task Scheduling? Task scheduling is the process of automatically running commands, scripts,…