Bootstrap
Web Development
CSS
Responsive Design
Front-End Development

How can I make Bootstrap columns all the same height?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Bootstrap is a powerful, mobile-first front-end framework that is widely used in web development to create responsive and dynamic websites. One of the common questions that arises when using Bootstrap’s grid system is how to make columns all the same height regardless of their content.

Understanding the Bootstrap Grid System

Bootstrap’s grid system is built with flexbox in Bootstrap 4 and later, which facilitates more flexible and advanced layout capabilities compared to earlier Bootstrap versions that used float-based layouts. The system consists of containers, rows, and columns that help structure the content in a flexible and responsive manner.

Using Flexbox to Achieve Equal Height Columns

Flexbox makes it fairly straightforward to set equal height columns within a row. Since Bootstrap’s .row class is already display:flex, columns within a row automatically become flex items.

Here’s a simple example illustrating equal height columns:

html
1<!-- Bootstrap CSS -->
2<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
3
4<div class="container">
5  <div class="row">
6    <div class="col-md-4 bg-primary text-white p-3">This is a column</div>
7    <div class="col-md-4 bg-secondary text-white p-3">This column has more content than the previous one. It still matches the height of all other columns automatically thanks to flexbox.</div>
8    <div class="col-md-4 bg-success text-white p-3">This is a column</div>
9  </div>
10</div>

Aligning Items Within Columns

Sometimes, aligning content within these equal-height columns is necessary. Bootstrap 4 provides utility classes for aligning content along the cross axis (perpendicular to the main axis). Here are a few classes:

  • .align-items-start: Aligns items to the start of the container.
  • .align-items-end: Aligns items to the end of the container.
  • .align-items-center: Aligns items in the center of the container.
  • .align-items-stretch: (default) Stretches items to fill the container.

For example:

html
1<div class="row align-items-center">
2  <div class="col-md text-center bg-light p-4">Centered Content</div>
3  <div class="col-md text-center bg-light p-4">Centered Content</div>
4</div>

Stretching Columns Vertically

If you explicitly need to force columns to be the same height (useful when not all columns are direct children of the same .row or when nesting grids):

html
1<div class="row h-100">
2  <div class="col-md-4 d-flex bg-light">
3    <div class="m-auto">Centered content using d-flex and m-auto</div>
4  </div>
5  ...
6</div>

Handling Different Column Content Sizes

When columns have different amounts of content, the default behavior is to fit the content. Using utility classes like min-vh-100 or custom CSS can force them to take up a minimum height, which can help in creating visually appealing layouts.

Summary Table on Bootstrap Utility Classes for Column Heights

Utility ClassPurpose
.align-items-startAligns items to the start of the flex container
.align-items-endAligns items to the end of the flex container
.align-items-centerCenters items in the flex container
.align-items-stretchStretches items to fill the flex container
h-100Makes an element take the full height of its parent
d-flexApplies display:flex to an element
m-autoMargins auto to center content within a flex container

Conclusion

Using Bootstrap’s flex-based grid system makes it relatively straightforward to create equal height columns. The provided utility classes can accommodate various content alignment needs within these columns. Understanding and applying these techniques can greatly improve the aesthetic and functional quality of a web layout.


Course illustration
Course illustration

All Rights Reserved.