CSS
Creating Equal Height Columns with Flexbox
Achieve perfectly aligned, equal-height columns in your layouts regardless of their content length, leveraging the power of CSS Flexbox for consistency.
.flex-container {
display: flex;
gap: 20px; /* Space between columns */
align-items: stretch; /* Default, but explicit for clarity */
padding: 20px;
background-color: #eee;
font-family: sans-serif;
}
.flex-item {
flex: 1; /* Each item takes equal available space */
padding: 15px;
background-color: white;
border: 1px solid #ddd;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}
.flex-item:nth-child(even) {
/* Example of different content height for demonstration */
min-height: 150px;
}
How it works: This snippet demonstrates how Flexbox effortlessly creates equal-height columns. By applying `display: flex` to the parent container, all its direct children become flex items. The `align-items: stretch` property (which is the default behavior for flex containers) ensures that all flex items stretch vertically to match the height of the tallest item, creating a visually consistent layout regardless of individual content length. `flex: 1` on the items allows them to grow and shrink equally to fill available horizontal space.