Course contentsShow
← Web Development
Lesson 288 of 2,9825. FlexboxFree lesson

min-width and max-width with Flexbox

Control flex item sizing limits to prevent excessive growth or shrinkage.

min-width and max-width with Flexbox

What you'll learn: How to use min-width and max-width to set boundaries on how much flex items can grow or shrink, preventing them from becoming too large or too small.

The Problem Flexbox Solves (and Sometimes Creates)

You've learned how flex-grow and flex-shrink let flex items expand or contract to fill available space. But sometimes items grow too much or shrink too much, breaking your layout. That's where sizing limits come in.

Setting Boundaries with min-width and max-width

The min-width and max-width properties act like guardrails for flex items:

  • min-width: The smallest the item can ever shrink, even if flex-shrink wants it smaller
  • max-width: The largest the item can ever grow, even if flex-grow wants it bigger

These properties override flex calculations when items would exceed these limits.

How They Work

.flex-item {
  flex: 1 1 200px; /* Grow, shrink, 200px basis */
  min-width: 150px; /* Never smaller than 150px */
  max-width: 400px; /* Never larger than 400px */
}

Even though flex-grow: 1 says "take extra space," this item stops growing at 400px. Similarly, despite flex-shrink: 1, it won't go below 150px.

Think of It Like This

Imagine a balloon (flex item) that can inflate or deflate. flex-grow and flex-shrink control the air pump, but min-width and max-width are physical limits—the balloon's minimum deflated size and maximum stretch before popping.

For Column Layouts

When flex-direction: column, use min-height and max-height instead, since items grow/shrink vertically.

Key Takeaway: Use min-width and max-width to prevent flex items from growing or shrinking beyond acceptable limits, giving you precise control over responsive layouts.