looping through Bootstrap boxes with equal heights

Every time I come across this issue, I find myself struggling without a clear solution that simplifies my life. My layout consists of 14 boxes arranged side by side. I am using the col-sm-12 class (with my bootstrap set up for 24 columns) on these boxes. However, the content within the boxes varies, causing their heights to be unequal, and the larger boxes end up "pushing" the ones adjacent to them. One straightforward solution is to insert a row after every two boxes, but here's the challenge - the boxes are dynamically generated in a loop! I could set a min-height, but that might not look good on mobile devices (especially on landscape mode). I am also unsure about the browser support for flexbox.

Are there any other clever hacks that I could implement?

HTML

<div class="row">
  <div class=" col-xs-24 col-sm-12 col-md-12 blocks"><!--the loop starts from here-->
   <div class="row">
      <div class="col-sm-8"> some content</div>
       <div class="col-sm-16">some more content that changes height. you can put some lorem isum in here. </div>
    </div>
    <!--my loop, aka, blocks, end here-->
   </div>
 </div>

Answer №1

Flex is the ultimate solution when it comes to creating columns or boxes of equal height. Utilize flex and let the browser handle all the calculations and heavy lifting for you.

.wrapper {
    display: block;
    max-width: 500px;
    margin: 30px auto;
}

.flex-row {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
}

.flex-row div[class*="col-sm"] {
    background: #ddd;
    padding: 10px;
    box-sizing: border-box;
    flex: 0 1 45%;
}
<head>
  <link rel="stylesheet prefetch" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<div class="wrapper">
  <div class=" col-xs-24 col-sm-12 col-md-12 blocks"><!--the loop starts from here-->
   <div class="row flex-row">
      <div class="col-sm-8"> some content</div>
       <div class="col-sm-16">some more content that changes height. you can put some lorem isum in here. </div>
    </div>
    <!--my loop, aka, blocks, end here-->
   </div>
 </div>

Visit CodePen Example

Browser Support

While flex is generally well supported in modern browsers, Safari may present some challenges according to caniuse.com.

For older browsers like IE 8 or 9, support is lacking. IE 10 adheres to an older syntax from 2011, and IE 11 offers partial support. Older WebKit browsers may require fallback rules to accommodate legacy Flexbox syntax from 2009 or 2011.

Learn more about Advanced Cross-browser Flexbox

If you prefer to avoid using flex, options like tables or inline-block elements may be considered, though equal-height columns may not be achievable with these alternatives.

Answer №2

Feeling hesitant about utilizing flexbox since it doesn't support IE10 and below? Consider using CSS tables instead.

Keep in mind that I have implemented the standard 12 columns for this demonstration.

.blocks > .row {
  display: table
}
.blocks .row > [class*="col"] {
  display: table-cell;
  background: red;
  float:none /* override the float:left from bootstrap.css */
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class=" col-xs-12 col-sm-12 col-md-12 blocks">
      <!--the loop starts from here-->
      <div class="row">
        <div class="col-xs-4 col-sm-4 col-md-4">some content</div>
        <div class="col-xs-8 col-sm-8 col-md-8">some more content that changes height. you can put some lorem isum in here.</div>
      </div>
      <!--my loop, aka, blocks, end here-->
    </div>
  </div>
</div>

UPDATED based on OP's request in the comments section below

Here is another version using CSS tables as well. However, due to being within the loop, OP cannot add HTML markup.

A more straightforward approach given the markup is clearing left after every 4 items

.row{
  margin:15px auto;
}
.block{
  background:black;
  color:white;
  border:1px solid white;
  padding:15px 5px;
}
.block h4{
  color: pink; 
  text-decoration:underline;
}

.block:nth-of-type(3n+1) {
  clear:left
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row">
    <div class="col-sm-4 block">
      <!--lets assume this coloumn is on the loop-->
      <h4>this is a small paragraph</h4>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sit amet tortor pretium quam dapibus maximus nec quis dolor. Mauris non auctor eros. In hac habitasse platea dictumst. </p>
    </div>
    <div class="col-sm-4 block">
      <!--lets assume this coloumn is on the loop-->
      <h4>this is another small paragraph</h4>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sit amet tortor pretium quam dapibus maximus nec quis dolor. Mauris non auctor eros. In hac habitasse platea dictumst. </p>
    </div>
    <div class="col-sm-4 block">
      <!--lets assume this coloumn is on the loop-->
      <h4>wow! small paragraph</h4>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sit amet tortor pretium quam dapibus maximus nec quis dolor. Mauris non auctor eros. In hac habitasse platea dictumst. </p>
    </div>
    <div class="col-sm-4 block">
      <!--lets assume this coloumn is on the loop-->
      <h4>again small paragraph!</h4>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sit amet tortor pretium quam dapibus maximus nec quis dolor. Mauris non auctor eros. In hac habitasse platea dictumst. </p>
    </div>
    
    <!-- Additional columns in the loop with similar structure -->
    
  </div>
</div>

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

Aligning an image vertically within a division

I am currently working on aligning images vertically within an image rotator (SlideDeck). At the moment, all the images are aligned at the top but I need them to be centered. The heights of the images vary. I have tried several methods, including absolute ...

Tips on transmitting form information from client-side JavaScript to server-side JavaScript with Node.js

Having created an HTML page with a form, my goal is to capture user input and store it in a JSON file. However, I've run into some confusion... In the process, I utilized the express module to set up a server. My mind is juggling concepts such as AJA ...

Reduce the value in the database when a button is clicked using PHP

I am currently working on a carpooling website and I could really use some assistance. I have implemented a button that, once clicked, should decrease the number of available free spaces in the car by one (meaning the person who clicks it will reserve a ...

Adjusting the width of a <div> element based on window size changes

So I have this Vuejs application that has three divs using CSS grid: a sidebar, a config panel, and a preview. I want to dynamically set the width of the preview div in pixels based on the current screen size and when the screen is resized. To give a bett ...

Animations experiencing delays on mobile Chrome

I am currently exploring the world of website animations. I have a version of the jsfiddle code linked below that is similar to what I am working on. The animations function perfectly when viewed on desktop, but when accessed on mobile - specifically on my ...

Using Selenium to extract and manipulate text content enclosed within specific HTML tags

Imagine you have this snippet of HTML code <tag1> "hello" <tag2></tag2> "world" </tag1> If we use driver.find_element(By.CSS_SELECTOR,"tag1").text, it will display the string "helloworld". ...

Having trouble with your CSS on your mobile website?

After developing a website and implementing media queries for responsiveness, everything seemed to be in perfect working order. The website displayed flawlessly on emulators like () and Chrome's inspect element, mirroring my desired mobile design. How ...

Struggles with conflicting versions of Jquery within asp.net

Here is a snippet of my jQuery function: $(window).scroll(function () { if ($(window).scrollTop() > offset.top) { $("#sidebar").css("marginTop", $(window).scrollTop.length - offset.top); $("#sidebar").css({ o ...

Why is there an excessive amount of white space on my Reactjs page?

This webpage (page URL -) displays well without any white space between two images when viewed on a laptop. However, when accessed on a mobile device, it appears to be taking up too much space as shown in the image below. Image of webpage on laptop- lapto ...

Anticipated the presence of a corresponding <tr> in the <table> within the server HTML, but encountered hydration failure due to discrepancies between the initial UI and the server-rendered content

Next.js Version 13.2.3 In Next.js, it is advised not to use the table element. "use client"; export default function index() { return ( <table> <tr> <th>Company</th> ...

Creating interactive labels in a histogram that update based on the user's input

Currently, I have operational code in place that takes input data and generates a histogram based on set thresholds. When the code is executed, the histogram changes dynamically as the threshold slider is adjusted. However, there seems to be an issue wit ...

Determine if a certain value is present in an array within an HTML document

In the context of AngularJS, I have an array that looks like this: var test=["abc/1/1","def/2/2","efg/3/3"]; I am trying to determine if the array contains a value with "abc" in it within my HTML code. When I used the following: test.indexOf("abc")!=-1 ...

Spacious width that doesn't require a horizontal scrollbar for navigation

My dilemma is with the width of my body and background image. I set the body width to 1920px to match the background-image for the header. However, since I have a narrower wrapper inside the body, the header background's width gets cut off by the wrap ...

Is there a way I can toggle between nav-pills and nav-stacked based on varying screen sizes?

Can someone help me with a solution to switch between nav-stacked nav-pills for mobile and non-nav-stacked nav-pills for other devices using Bootstrap? For instance, on a desktop: <nav> <ul class="nav nav-pills"> <li role="presentat ...

Create an element that can be dragged without the need to specify its position as absolute

I am having an issue where elements I want to drag on a canvas are not appearing next to each other as expected. Instead, they are overlapping: To make these elements draggable, I am utilizing the dragElement(element) function from https://www.w3schools.c ...

Issue with hiding the overflow of the document's body

Despite finding a solution to a previous issue on Stack Overflow, I am now facing a new challenge. Adding margin:0em auto; to my page is causing it to have a 'jumpy' effect. .align-center-public { width:1000px; margin:0em auto; overf ...

Enhance design based on scrolling function in React

I've been trying to update the color of my header when a user scrolls the page, but for some reason, my onScroll method isn't working. Can anyone help me figure out why and how to solve this issue? The onScroll method is triggered by the bottom T ...

The tab content in VerticalTab Material React UI is spilling out and overflowing

Having some trouble with this particular component. Whenever I input content larger than my Grid size, it overflows the VerticalTab section. You can see an example of this in this codesandbox. Here's what I've attempted: return ( <div cla ...

Looking to integrate the date, month, and year selection tags into the inline format

The Edit User Profile page includes a Birthday field, which is currently displayed vertically. I am looking to change the layout so that the Birthday field appears horizontally. Here is the code I am using: Within visitors/edit.html.erb, <%= simple_f ...

What is the best way to stack several items vertically beside an image?

As a beginner in the world of web development, I recently embarked on my first project. However, I am facing an issue where I cannot align multiple elements under each other next to an image. One element floats at the top beside the image, while the other ...