Course contentsShow
← Web Development
Lesson 480 of 2,9828. Advanced CSS Selectors and FeaturesFree lesson

min(), max(), and clamp() for Responsive Sizing

Set boundary constraints on values to create fluid typography and layouts that adapt without media queries.

min(), max(), and clamp() for Responsive Sizing

You'll learn how to set flexible boundaries on CSS values so elements scale smoothly between constraints without writing media queries.

The Core Idea

While calc() lets you combine values, min(), max(), and clamp() give you intelligent choice between multiple values. Think of them as CSS functions that automatically pick the best value based on current conditions:

  • min() chooses the smallest value from a list
  • max() chooses the largest value from a list
  • clamp() ensures a value stays between a minimum and maximum

Why This Matters for Responsiveness

Instead of writing media queries, you can set boundaries directly in your values. The browser does the math and picks the appropriate size in real-time as the viewport changes.

min() example — "Never exceed this width":

.container {
  width: min(90%, 1200px);
  /* Uses 90% on small screens, but caps at 1200px */
}

max() example — "Never go smaller than this":

.button {
  font-size: max(16px, 1vw);
  /* Scales with viewport, but stays readable at minimum 16px */
}

clamp() example — "Stay between these bounds":

h1 {
  font-size: clamp(1.5rem, 5vw, 3rem);
  /* MIN: 1.5rem, PREFERRED: 5vw, MAX: 3rem */
}

The clamp() function is especially powerful because it combines minimum, preferred, and maximum in one declaration. It's like saying "scale naturally, but don't get too small or too large."

Combining with Custom Properties

:root {
  --min-size: 1rem;
  --max-size: 2.5rem;
  --fluid-scale: 4vw;
}

.heading {
  font-size: clamp(var(--min-size), var(--fluid-scale), var(--max-size));
}

Key Takeaway: min(), max(), and clamp() let you create self-adjusting values with built-in constraints, making responsive design more fluid and reducing reliance on breakpoints.