CSS flex-wrap Property
Subject: css
CSS flex-wrap Property
The flex-wrap property specifies whether flex items should wrap onto multiple lines or stay in a single line inside a flex container. It ensures better layout behavior when there isn't enough space.
Why Use flex-wrap?
- Prevent items from shrinking too much
- Automatically wrap items to the next line when space runs out
- Improve readability and responsiveness
- Avoid complex media queries in simple layouts
Syntax
nowrap(default): All items stay on one linewrap: Items wrap to the next line (from top to bottom)wrap-reverse: Items wrap in reverse order (from bottom to top)
Example: Wrapping Flex Items
Explanation
- The container uses
flex-wrap: wrap, allowing items to wrap when space runs out - Items with fixed width automatically move to the next line on smaller screens
Example: wrap-reverse
- Items will now wrap from bottom to top instead of top to bottom
Best Practices
- Use
wrapfor multi-line flex layouts - Combine with
gap,justify-content, andalign-contentfor clean alignment - Avoid
nowrapon small screens where it may make items unreadable
Common Use Cases
- Responsive card or image grids
- Navigation menus that overflow onto new rows
- Tag or chip-based UI components
- Toolbars or button groups that need to wrap
Key Takeaways
flex-wrapdefines whether flex items wrap onto new lineswrapenables wrapping from top to bottomwrap-reversewraps items in reverse vertical direction- Improves responsiveness and layout structure
- Often used with other layout utilities for full control
Advertisement Slot 1
Advertisement Slot 2