Min Max and Clamp Functions
Use min(), max(), and clamp() for adaptive sizing that responds to container or viewport size.
Min Max and Clamp Functions is a free CSS Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CSS Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why CSS Comparison Functions?
CSS comparison functions let you express conditional sizing without JavaScript: "use this size but never smaller/larger than these bounds." They are the core of modern fluid, responsive design.
min() Function
min() returns the smallest of a comma-separated list of values. The browser picks whichever value is smallest at the current viewport size.
.container {
width: min(800px, 90%);
/* 800px on large screens, 90% on small — never exceeds 800px */
}max() Function
max() returns the largest value. Useful for setting minimum sizes that also grow.
p {
font-size: max(1rem, 2vw);
/* at least 1rem, grows with viewport */
}clamp() Function
clamp(min, preferred, max) constrains a value between a minimum and maximum. The preferred value is usually fluid (like vw).
h1 {
font-size: clamp(1.5rem, 4vw, 3.5rem);
/* minimum 1.5rem, preferred 4vw, max 3.5rem */
}clamp() for Fluid Typography
Fluid typography with clamp() eliminates the need for multiple media query font-size overrides:
h1 { font-size: clamp(1.75rem, 5vw, 3.5rem); }
h2 { font-size: clamp(1.4rem, 3.5vw, 2.5rem); }
p { font-size: clamp(1rem, 2vw, 1.25rem); }clamp() for Fluid Spacing
Apply the same pattern to spacing for naturally responsive margins and padding:
.section {
padding: clamp(2rem, 8vw, 6rem);
}min() for Containers
A safe container pattern that never exceeds a max-width and never adds horizontal scroll:
.wrapper {
width: min(1200px, calc(100% - 2rem));
margin-inline: auto;
}Nesting Functions
You can nest comparison functions for complex expressions:
.hero-text {
font-size: clamp(1rem, max(2vw, 1.5rem), 4rem);
}calc() Combined with clamp()
Use calc() inside clamp() for precise fluid interpolation between two sizes:
h1 {
/* 16px at 320px viewport, 48px at 1200px viewport */
font-size: clamp(1rem, calc(0.5rem + 2.5vw), 3rem);
}min-content max-content fit-content
CSS also has keyword size values:
min-content: smallest possible size without overflowmax-content: takes as much space as content needsfit-content(): like max-content but capped at a provided limit
.tag {
width: fit-content(200px);
padding: 4px 8px;
}Browser Support
min(), max(), and clamp() are supported in all modern browsers. No prefixes needed. They can be used anywhere a length, frequency, time, or number value is expected.
Quick Check
A heading uses font-size: clamp(1rem, 4vw, 3rem) on a 600px-wide viewport. What size is the font?
Recap
min() picks the smallest value, max() picks the largest. clamp(min, preferred, max) constrains a fluid value within bounds. These functions power modern fluid typography and spacing, often eliminating the need for multiple media-query overrides.
Frequently asked questions
Is the “Min Max and Clamp Functions” lesson free?
Yes — the full text of “Min Max and Clamp Functions” is free to read here on the web, and the CSS Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CSS Academy course, upgrade to CoddyKit PRO.
What will I learn in “Min Max and Clamp Functions”?
Use min(), max(), and clamp() for adaptive sizing that responds to container or viewport size. You practise CSS Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start CSS Academy?
No prior experience is required. CSS Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Min Max and Clamp Functions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this CSS Academy lesson?
Yes. Every CSS Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Absolute Units: px pt cm
- Relative Units: em and rem
- Viewport Units: vw vh dvh
- Min Max and Clamp Functions