calc(), min(), max(), and clamp()
CSS math functions combine values and place useful limits on fluid dimensions without requiring many breakpoints.
✓ What you will learn in this topic
- Explain the key idea: Math functions express fluid values with explicit limits.
- Apply the concept: The page gutter grows with the viewport but stays bounded; the title scales between readable limits.
- Avoid this common mistake:
Concept explanation
calc() evaluates an expression with compatible units. min() chooses the smallest supplied value; max() chooses the largest. clamp(min, preferred, max) keeps a preferred value within bounds. These functions are useful for gutters, widths, and type scales.
Readable formulas are easier to tune. Keep a minimum font size and a maximum line width so fluid values do not become uncomfortable.
Syntax
A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.
.demo {
padding-inline: clamp(1rem, 4vw, 2.5rem);
}Example 1: a small page element
Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.
<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>.demo {
padding-inline: clamp(1rem, 4vw, 2.5rem);
}Expected result: The page gutter grows with the viewport but stays bounded; the title scales between readable limits.
Example 2: a practical interface card
The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.
<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>.page-shell {
width: min(100% - 2rem, 76rem);
margin-inline: auto;
}
.page-title {
font-size: clamp(2rem, 1.2rem + 3vw, 4rem);
}Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.
Common mistake
Combining units with calc() without checking the resulting value or providing a sensible bound.
Best practice
Use readable expressions, retain minimum and maximum constraints, and verify support for any newly adopted feature.
Practice exercise
Create a fluid heading size and page gutter that scale smoothly but remain usable at extreme viewport widths. Apply it to the example above and inspect the result at a narrow and a wide viewport.
Section summary
- Math functions express fluid values with explicit limits.
- Use readable expressions, retain minimum and maximum constraints, and verify support for any newly adopted feature.
Knowledge check
What happens when the preferred value in clamp() exceeds the maximum?
Reveal the explanation
The clamp bounds limit the preferred value on both ends.
Finish the module lessons, then take the server-scored module quiz to check your understanding.
Practical example
css.demo {
padding-inline: clamp(1rem, 4vw, 2.5rem);
}
Expected result:
The page gutter grows with the viewport but stays bounded; the title scales between readable limits.
Hands-on practice task
Create a fluid heading size and page gutter that scale smoothly but remain usable at extreme viewport widths.
In this section
Other lessons in Modern CSS Functions
Broaden your knowledge
Related learning topics
HTML
Learn to structure accessible, meaningful web pages with a complete HTML course from first document to professional application patterns.
JavaScript
Build strong JavaScript foundations with browser examples, focused practice, beginner projects, and module quizzes.