How to Make Equal Height Columns in CSS: Simple Methods
display: flex; on the container and set flex: 1; on the columns. Alternatively, CSS Grid with grid-template-columns also creates equal height columns automatically.Syntax
Use display: flex; on a container to create a flexible layout. Then, apply flex: 1; to child columns to make them grow equally and have the same height.
For CSS Grid, use display: grid; and define columns with grid-template-columns. Grid automatically aligns rows to equal height.
/* Flexbox syntax */ .container { display: flex; } .column { flex: 1; padding: 1rem; border: 1px solid #ccc; } /* CSS Grid syntax */ .container-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; } .column-grid { padding: 1rem; border: 1px solid #ccc; }
Example
This example shows three columns side by side with equal height using Flexbox. Each column's height matches the tallest content automatically.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Equal Height Columns Example</title> <style> .container { display: flex; gap: 1rem; } .column { flex: 1; padding: 1rem; border: 2px solid #007BFF; background-color: #E9F5FF; } </style> </head> <body> <div class="container"> <div class="column"> <h2>Column 1</h2> <p>This column has some short content.</p> </div> <div class="column"> <h2>Column 2</h2> <p>This column has a bit more content to show how the height adjusts to the tallest column automatically.</p> <p>Extra paragraph here.</p> </div> <div class="column"> <h2>Column 3</h2> <p>Short content again.</p> </div> </div> </body> </html>
Common Pitfalls
One common mistake is using float for columns, which does not make their heights equal automatically. Another is forgetting to set flex: 1; on the columns inside a flex container, causing uneven widths and heights.
Also, using height: 100% on columns without a fixed height on the parent won't work because the parent height is not defined.
/* Wrong approach with float */ .container-float { overflow: hidden; } .column-float { float: left; width: 33%; padding: 1rem; border: 1px solid #ccc; } /* Right approach with flexbox */ .container-flex { display: flex; } .column-flex { flex: 1; padding: 1rem; border: 1px solid #ccc; }
Quick Reference
- Flexbox: Use
display: flex;on container andflex: 1;on columns. - CSS Grid: Use
display: grid;withgrid-template-columnsfor equal columns. - Avoid: Using floats or fixed heights for equal height columns.
- Remember: Flexbox aligns heights automatically based on tallest column.