flex-wrap

Term side of the Card.
Topic: Flexboxes
Unit title: flex-wrap
Categories: Property
Explanation side of the Card

Explanation: The property that controls whether flex items are forced onto a single line or can wrap onto multiple lines within a flex container. It helps manage how items flow when there's not enough space, allowing for more flexible and responsive designs.

Browser compatibility:
  • The chrome browser supports this feature starting from version 29.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 28.
  • The safari browser supports this feature starting from version 9.
Availability: 

Example

Wrongcss
.cards {
  display: flex;
}
.card {
  width: 300px;
}
Rightcss
.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}
.card {
  flex: 1 1 300px;
}

Without flex-wrap, items shrink into one line; with it, they move to new lines once they hit 300px.

Common mistakes

  • Items never wrap because they all have flex-shrink: 1 and no minimum size.
  • The last row's items stretch wider than the others with flex: 1.

Key points

  • Responsive rows of cards use flex-wrap: wrap with a flex-basis or min-width, so items wrap instead of overflowing.
  • Spacing between wrapped lines uses gap, not negative margins.

Interview questions

Why are my flex items not wrapping?

The default is 'nowrap', and items shrink first. Add flex-wrap: wrap and give items a basis or minimum width.

How do I keep last-row items the same width?

Flexbox can't align items across rows. Use grid with repeat(auto-fill, minmax(300px, 1fr)) for equal columns.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy