CHAPTER 43
CSS Math Functions
Learn how calc(), min(), max() and clamp() can create flexible and responsive CSS values.
01. What is CSS Math Functions?
CSS math functions allow calculations and flexible value selection.
calc() can combine different CSS units.
min() chooses the smallest available value.
max() chooses the largest available value.
clamp() creates a value between a minimum and maximum.
02. Syntax
width: calc(100% - 40px);
width: min(100%, 900px);
width: max(300px, 50%);
font-size: clamp(1.5rem, 5vw, 4rem);
03. Example
CSS / HTML Example
AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>
.container {
width: calc(100% - 40px);
max-width: 900px;
margin: auto;
padding: 20px;
background: #dbeafe;
}
h1 {
font-size:
clamp(2rem, 6vw, 4rem);
}
</style>
</head>
<body>
<div class="container">
<h1>AIFA Coding Lab</h1>
</div>
</body>
</html>
04. Important Points
✓
calc() performs mathematical calculations.
✓
min() chooses the smallest value.
✓
max() chooses the largest value.
✓
clamp() is useful for responsive typography and sizing.
✓
Math functions reduce the need for many media queries.
05. Common Mistakes
⚠
Forgetting spaces around + and - operators in calc().
⚠
Creating calculations that result in unexpected sizes.
⚠
Using complex formulas when a simple value would work.
06. Real World Use
CSS math functions are useful for responsive containers, spacing, typography, dashboards and modern fluid layouts.
Ready to Practice? 🚀
Open the AIFA CSS Editor and practice this chapter live.
Try It Yourself →
Practice Exercise
Create Fluid Typography
Create a heading using clamp() so its font size changes smoothly between mobile and desktop sizes.