CSS
Flexbox: Equal Height Columns with Item Wrapping
Implement a responsive layout where multiple columns maintain equal height even when wrapping to new rows, a common challenge in CSS layouts.
.container {
display: flex;
flex-wrap: wrap;
align-items: stretch; /* Ensures all items in the same row have equal height */
gap: 20px; /* Space between items */
padding: 20px;
background-color: #f9f9f9;
}
.item {
flex: 1 1 calc(33% - 20px); /* Approx 3 items per row with gap */
background-color: #ffffff;
border: 1px solid #ddd;
padding: 15px;
box-sizing: border-box;
display: flex; /* Can use flex inside for internal content alignment */
flex-direction: column;
}
How it works: This Flexbox snippet demonstrates how to create a responsive layout where items maintain equal height, even when they wrap onto multiple rows. `display: flex` combined with `flex-wrap: wrap` allows items to move to a new line when space is insufficient. The key here is `align-items: stretch` on the container, which stretches all flex items along the cross-axis to fill the container, effectively making them all the same height within their respective rows. `gap` provides consistent spacing between items.