Creating a Bootstrap layout with columns of varying heights

My current setup looks like this:

<div class="row">
    <div class="col-md-4">Content</div>
    <div class="col-md-4">Content</div>
    <div class="col-md-4">Content</div>
    <div class="col-md-4">Content</div>
    <div class="col-md-4">Content</div>
    <div class="col-md-4">Content</div>
    <div class="col-md-4">Content</div>
    <div class="col-md-4">Content</div>
    <div class="col-md-4">Content</div>
</div>

If each content box had a different height but the same width, how could I maintain the grid layout while causing them to stack directly below the preceding element rather than lining up in rows?

Normally, Bootstrap will align the next row of col-md-4 under the tallest item in the previous row, resulting in neat and even rows. However, I am looking for a "Masonry" layout where each item falls directly below the last ("Masonry" layout).

Answer №1

With the ever-evolving Bootstrap framework, it's important to stay updated on best practices and solutions for common layout issues. Here is an in-depth look at how to handle column height discrepancies across different versions of Bootstrap.

Bootstrap 5 Update (2021)

For Bootstrap 5, the traditional card-columns approach for achieving a Masonry-like layout has been deprecated. Instead, the recommended method is to utilize the Masonry JS plugin. Check out this Bootstrap 5 Masonry Example to see it in action.

Bootstrap 4 Update (2018)

In Bootstrap 4, equal-height columns are achieved effortlessly due to its default use of flexbox. This eliminates any "height issue" concerns. You can explore multi-column solutions in Bootstrap 4 with examples like this Bootstrap 4 Masonry cards Demo.

Bootstrap 3 Original Solution

Back in the Bootstrap 3 days, uneven column heights were a common challenge mainly because of the float:left property used for columns. The solution involved stacking columns to the closest side. Learn more about this issue here.

Note: When dealing with scenarios where there are more than 12 column units within a single .row, consider these options and explanations before making design decisions.


Different Approaches to Addressing Column Height Discrepancies:

1 - Utilizing the 'clearfix' Method:

The 'clearfix' method involves adding specific markup after every X number of columns to ensure proper wrapping. Take a look at this Clearfix Demo for a better understanding.

View additional clearfix demos for responsive tiers here.


**2 - Equalizing Column Heights with Flexbox:**

To maintain consistent column heights using flexbox, you can implement the following CSS rules within your project structure. Check out the Flexbox equal height Demo for practical implementation.


**3 - Switching from Float to Inline-block Display:**

An alternate method to tackle column height discrepancies involves setting columns to display:inline-block and float:none. Just ensure minimal HTML whitespace between columns to avoid premature wrapping. See the fix in action here.


4 - Trying the CSS3 Columns Approach:

If you prefer a CSS-only solution resembling Masonry or Pinterest layouts, you can explore CSS multi-columns. While not directly native to Bootstrap 3, Bootstrap 4 offers similar functionality. Get inspired by this Bootstrap 3 multi-columns Demo.

5 - Implementing Masonry JavaScript/jQuery Plugin:

Consider utilizing plugins like Isotope/Masonry for dynamic layout adjustments. Witness the impact of this approach in action through examples like Bootstrap Masonry Demo.


Explore further insights into handling varying column heights in Bootstrap here.


Answer №2

This solution surprisingly worked for me even without using the .display-flex class

.row {
    display: flex;
    flex-wrap: wrap;
}
.row > [class*='col-'] {
    display: flex;
    flex-direction: column;
}

Answer №3

I developed a new add-on (that is also reactive) for the Bootstrap row. Here is an example of how you can implement it:

.row.flexRow   { display: flex;  flex-wrap: wrap;}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method for implementing border-radius on select2 elements?

I previously had a default bootstrap user select box with a border-radius style applied: <div class="container"> <select class="form-control" style="border-radius: 1rem;"> <option value="1">Us ...

Troubles arise when the left column shifts to the top with widths larger than 1440

Having a WordPress site with the Divi theme, I encounter an issue where the menu on the left-hand side is being displayed above the page content when the screen width exceeds around 1440 pixels. This problem persists on every page that contains this colum ...

Ways to implement a setTimeout function to return to the initial div element?

I have a unique setup on my webpage with three divs. The first div features an html5 video, the second div contains buttons for interaction, and the third div acts as a thank you page that loops back to the beginning like a photo slide. I have shared my co ...

Is there a way to connect two tables using CSS pseudo-selectors?

Is there a way to make two separate tables interact with each other using CSS pseudo-selectors? I have a data table and an auto-numbered table, and I want the rows to highlight in both tables when hovering over a cell in one of them. I've tried using ...

Encountered a glitch during the npm installation process for mui core

I recently set up a new create-react-app and am still encountering the same error message. After installing MUI, I'm getting an 'unable to resolve dependency tree' issue. npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve depende ...

Angular does not display results when using InnerHtml

I'm currently in the process of creating a weather application with Angular, where I need to display different icons based on the weather data received. To achieve this functionality, I have developed a function that determines the appropriate icon to ...

Creating a dynamic visual experience with Angular 2: How to implement multiple font colors

I have a text area which is connected to one string, with the default text color set to white. <textarea style="background-color: black;color:#fff;" [(ngModel)]="outputText"></textarea> The connected string contains multiple variables. retur ...

Create a Bootstrap multi-bar component for visually displaying the daytime information

I am looking to create a horizontal bar with multiple intervals. Here is what I have tried so far: <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%;"& ...

The issue of AngularJS failing to bind object properties to the template or HTML element

Just dipping my toes into angularJS, following Todd Motto's tutorials, and I'm having trouble displaying object properties on the page. function AddCurrentJobs($scope){ $scope.jobinfo = [{ title: 'Building Shed', description: ...

Issue with jQuery Cycle causing images not to load in IE all at once, leading to blinking

When using the jQuery Cycle plugin in IE8 (as well as other versions of Internet Explorer), I am encountering an issue. My slideshow consists of 3 slides, each containing a Title, Description, and Image. The problem arises when viewing the slideshow in I ...

Troubleshooting padding problem in mobile gallery view with Bootstrap

Having a problem with the gallery layout in Bootstrap. It looks great on desktop view, but on mobile, there's no space between images on the same row. On mobile, the images stack vertically without any spacing between them within a row. However, ther ...

Is it possible to alter the font size in Vuetify depending on the viewport size?

Looking to customize font sizes based on viewports? <v-card-text style="font-size:5em"> Some Heading Here </v-card-text> If you want to change the font size to 3em on XS view, without duplicating code, consider using CSS only. Avoid dupli ...

lines stay unbroken in angular

I am encountering an issue when I execute the following code: DetailDisplayer(row) : String { let resultAsString = ""; console.log(row.metadata.questions.length); (row.metadata.questions.length != 0 )?resultAsString += "Questions ...

Is there a way to adjust the contents of an iframe to match the dimensions of the iframe itself?

I am trying to adjust the width of an iframe to 60%, regardless of its height. Is there a way to "zoom in" on the contents of the iframe to fit the width, so that I can then set the height based on this zoom level? I haven't been able to find any solu ...

Why isn't my textarea in jQUERY updating as I type?

On my website, I have a comment script that is not functioning correctly in some parts of the jQuery/JavaScript code. Instead of posting an edited comment to PHP, I created a notification window to test if the value passed through is actually changing. W ...

I'm having trouble centering the links in my navigation bar using flexbox, and I'm not sure how to fix it

Struggling with creating a navigation bar for my portfolio-building project. The "Who needs a quote" element is correctly displaying on the left, but I can't figure out how to center align the links "Sports, business, politics". Tried using flexbox op ...

What is the correct way to cite an inner class?

Exploring the world of Material UI styling for the first and probably last time, I find myself in a bit of a bind. In my current code setup, I have a question regarding the following structure: .map(section => ( <Section className={classNames(clas ...

Tips for hiding the calendar icon once a form has been submitted

Below is the HTML code snippet for the date field. <asp:TextBox ID="txtExpiryDate" runat="server" Width="80px" MaxLength="10" CssClass="fromDate" /> And here is the HTML code snippet for the Submit button. <asp:Button ID="cmdSubmit" runat=" ...

PHP is functioning properly, but no content is being displayed

I recently delved into coding and had to tackle a form that sends data through email using PHP. While the functionality of my form is working perfectly and I'm able to receive emails, I encountered an issue where the PHP page appears blank. Despite tr ...

Adaptable Semantic UI form design

Welcome, internet friends! If anyone out there has a moment to spare and is familiar with Semantic UI, I could really use some assistance... Currently, I am working on a form that looks great on larger screens like this: https://i.stack.imgur.com/cafc5.j ...