CSS Math Functions
What are CSS Math Functions?
CSS Math Functions allow you to perform mathematical calculations directly in CSS. They make layouts more flexible by calculating values based on screen size, percentages, or fixed units. The most commonly used CSS Math Functions are calc(), min(), max(), and clamp().
Why Use CSS Math Functions?
Math functions help create responsive webpages without using JavaScript. They automatically calculate widths, heights, margins, padding, and font sizes, making websites adapt to different screen sizes more efficiently.
Common CSS Math Functions
Step 1: Use calc()
The calc() function performs mathematical calculations using different CSS units.
<!DOCTYPE html>
<html>
<head>
<style>
.box{
width:calc(100% - 100px);
padding:20px;
background:#007BFF;
color:white;
}
</style>
</head>
<body>
<div class="box">
Width calculated using calc()
</div>
</body>
</html>
Step 2: Use min()
The min() function chooses the smallest value from the values you provide.
<!DOCTYPE html>
<html>
<head>
<style>
.box{
width:min(500px,90%);
padding:20px;
background:#28a745;
color:white;
}
</style>
</head>
<body>
<div class="box">
Using min()
</div>
</body>
</html>
Step 3: Use max()
The max() function selects the largest value from the values provided.
<!DOCTYPE html>
<html>
<head>
<style>
.box{
width:max(300px,50%);
padding:20px;
background:#ff9800;
color:white;
}
</style>
</head>
<body>
<div class="box">
Using max()
</div>
</body>
</html>
Step 4: Use clamp()
The clamp() function sets a minimum value, an ideal value, and a maximum value. It is commonly used for responsive font sizes.
<!DOCTYPE html>
<html>
<head>
<style>
h1{
font-size:clamp(20px,5vw,50px);
color:#007BFF;
}
</style>
</head>
<body>
<h1>
Responsive Heading
</h1>
</body>
</html>
Complete CSS Math Functions Example
<!DOCTYPE html>
<html>
<head>
<style>
body{
font-family:Arial;
text-align:center;
}
.card{
width:min(500px,90%);
padding:calc(20px + 1vw);
margin:30px auto;
background:#007BFF;
color:white;
border-radius:10px;
}
h2{
font-size:clamp(22px,4vw,40px);
}
</style>
</head>
<body>
<div class="card">
<h2>
CSS Math Functions
</h2>
<p>
This card uses min(), calc(), and clamp().
</p>
</div>
</body>
</html>
Advantages of CSS Math Functions
Best Practices
- Use calc() when combining different CSS units.
- Use min() to prevent elements from becoming too large.
- Use max() to maintain a minimum usable size.
- Use clamp() for responsive typography and spacing.
- Always include spaces around +, -, * and / in calc().
- Test your layouts on different screen sizes.
- Use CSS Math Functions instead of JavaScript whenever possible.
- Keep your calculations simple and easy to understand.
Web Resources on CSS Math Functions
1. MDN Web Docs : Using CSS math functions
2. Web.dev : CSS stepped value math functions
3. MDN Web Docs : CSS value functions
4. MDN Web Docs : Trigonometric functions in CSS
🧪 Test Your CSS Code
Edit the CSS code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.