Ensure all vertically stacked boxes maintain the same height using Flexbox technology

My design includes a series of links styled as larger boxes, with varying content and height. Using flexbox to ensure equal heights on each row is straightforward. I'm utilizing the Bootstrap grid layout, but when the boxes stack vertically (one per row) on smaller screens, I can't achieve consistent heights using flexbox. I attempted changing flow-direction from row to column, but it did not resolve the issue.

Is there a solution that doesn't involve resorting to JavaScript hacks?

Here are some CSS snippets from the relevant parent container:

display: flex;
flex-direction: row;
flex-flow: row wrap;

And here's how the child link (flex)boxes are styled:

display: flex;

For reference, here's some pseudo code:

<div class="flex-container>
   <a href="/#" class="box col-lg-3 col-md-4 col-sm-6">
     <h2>Test2</h2>
     <div><img src="/ds" alt="Hi" /></div>
     <div>sadsafmf sløfmkls</div>
   </a>
   <a href="/#" class="box col-lg-3 col-md-4 col-sm-6">
     <h2>Test3</h2>
     <div><img src="/ds" alt="Hi" /></div>
     <div>sadsafmf sløfmkls skldfsd ffsf sdflsdf f sl as ad sad</div>
   </a>
</div>

Answer №1

Unfortunately, equal height only applies to elements within the same row. When the elements wrap onto a new line, they will no longer maintain equal heights. However, in a column layout, equal height can work if there is enough vertical space for both elements to match the height of the tallest one (2 times the largest height). If there isn't enough space, the container won't stretch to accommodate both divs at the same height.

.container {
  margin-bottom:10px;
  }

.col-sm-6 {
  border:1px solid grey;
  flex:1;
}
.fixed {
  height: 250px;
}
.flex-container {
  display: flex;
  flex-direction: column;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row fixed flex-container">
        <a href="/#" class="box col-lg-3 col-md-4 col-sm-6">
       <h2>Same Height</h2>
       <div>sadsafmf sløfmkls</div>
     </a>
        <a href="/#" class="box col-lg-3 col-md-4 col-sm-6">
       <h2>Same Height</h2>
       <div>sadsafmf sløfmkls skldfsd ffsf sdflsdf f sl as ad sad</div>
     </a>
  </div>
</div>

<div class="container">
  <div class="row flex-container">
        <a href="/#" class="box col-lg-3 col-md-4 col-sm-6">
        <h2>Shorter</h2>
       <div>sadsafmf sløfmkls</div>
     </a>
        <a href="/#" class="box col-lg-3 col-md-4 col-sm-6">
       <h2>Taller</h2>
       <div>sadsafmf sløfmkls skldfsd ffsf sdflsdf f sl as ad sad</div>
     </a>
  </div>
</div>

Answer №2

To make the box element stretch, just include align-self:stretch. You could also try adding height: 100% on the child as an alternative solution.

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

How can I prevent scrolling while a loader is displayed during page loading?

How can I disable scrollability on my personal website when the page is loading with the loader wrapper? I have tried using overflow: hidden; in the CSS but it didn't work. The loader is set to display for 2.5 seconds. Any suggestions on how to achiev ...

Selecting Radio Buttons using Selenium with Java

Having trouble locating the XPath for checking radio button selection in Selenium with Java. I am attempting to determine if the "one way" checkbox is selected, and if so, select the two way checkbox. However, each time I run the code, it only recognizes t ...

What is the process for connecting a personalized bootstrap framework with an index.html document?

I have recently dived into the world of bootstrap and I just customized my bootstrap file on http://getbootstrap.com/docs/3.3/customize/ After compiling and downloading, I found it challenging to link it with my index.html file. The source of the bootstra ...

What is the best way to integrate a vh property instead of px for a scrolling navigation using jQuery?

I want to achieve a navigation bar that fades in after scrolling 100% of the viewport height, but all examples I find use a pixel value instead of viewport height. Is there a way to convert window height into jQuery to make this work? Thank you in advance ...

jQuery does not support CSS pseudo-code

I'm having trouble with my CSS pseudo-code not being recognized by jQuery. Here is the code I'm using: CSS: h1 { background: red; width: 100px; display: inline-block; } h1:after { content:" | "; background:blue; display ...

The text displayed on the image is experiencing fluctuations

I am experiencing an issue on my website where the post text fluctuates rapidly when the mouse hovers over the image. I have a site where I display the post text on mouseover of the image. Can someone please help me solve this problem? You can test it by ...

What is the best way to remove the box-model from an element?

Within my VueJS project, I am faced with nested elements that are generated dynamically like this: <container> <outerElement class="outer" v-for="obj in objects"> <innerElement class="inner" v-for="el ...

I noticed that my jquery code is injecting extra white space into my HTML5 video

Ensuring my HTML5 background video stays centred, full-screen, and aligned properly has been made possible with this jQuery snippet. $(document).ready(function() { var $win = $(window), $video = $('#full-video'), $videoWrapper = $video. ...

Creating a Modern Tooltip Menu with HTML, CSS, and Bootstrap

My goal is to design a menu that opens to the right, similar to a tooltip. I have experimented with different bootstrap techniques, but I am encountering difficulties in including HTML li elements within the tooltip. ...

Hiding a div becomes impossible once it has been set to display:block

When I click on an element, I want a box to open and then when I click on a "CLOSE" button, I want it to disappear. However, I am encountering an issue where the box appears using "display:block" but does not disappear with "display:none" as intended (see ...

Searching on the search bar using Django's object.filter method results in returning Object (1)

Recently, I have been working on a Django website Project that features a database filled with various books. Within the site, there is a search bar that allows users to type in the name of a book and receive results from the database. However, I have enco ...

Allow the button to be clicked only when both options from the 1/3 + 1/3 radio buttons are selected

How can I ensure that the next <button> at the bottom of the HTML is only clickable when at least one of each <input type="radio"> is checked? Also, how do I integrate this with my current function? The button itself triggers a jQuery function ...

Prevent fixed element from scrolling beyond a specific point

I'm working on a fixed sidebar that needs to scroll along with the main content and stop at a specific point when scrolling down, and vice versa when scrolling up. I've created a script that calculates the window height, current scroll position ...

Click to dynamically toggle classes with jQuery

I am trying to apply a class called 'select' when I click on a paragraph tag. However, the code I have written doesn't seem to be working. Can someone please provide some suggestions? Here is the code: <style type="text/css"> #el ...

How to conceal sections of a webpage until a child component is successfully loaded with vue

I am currently working on a Single Page Application using Vue. The default layout consists of some HTML in the header, followed by an abstract child component that is injected into the page. Each child component has a loader to display while the data is be ...

increased space between tables in HTML email design for optimal display in Gmail inbox

Here is the link to my code: http://jsfiddle.net/user1212/G86KE/4/ I am experiencing an issue in Gmail where there is extra white space between 2 tables inside the same cell. I have attempted using display:block; margin:0; padding:0; line-height:0; How ...

Limit jQuery script to operate exclusively on a single page

Despite the abundance of answers to similar questions, my lack of JS skills hinders me from implementing them in my specific case. On my WordPress site, I have a script that changes the navigation bar's color when scrolling down to the #startchange CS ...

Is this the correct method for constructing a class in CSS?

What is the correct way to style a class with CSS and write HTML code? .first{ color:red; } Here is an example of HTML code: <class id=first> <p> some text </p> <p> some other text </p> ...

Tips for creating boxes with clearly defined edges on shared sides using three.js

When I draw boxes with common sides, I don't see the common edges, but rather perceive them as a single object even though there are 25 boxes: https://i.sstatic.net/gE8FW.png function box(scene, x, y, z, size) { const points = []; ...

Error encountered in jQueryUI Autocomplete: the function 'this.source' is not defined

I have been working on incorporating a live search feature that scans through keys in a JSON obtained from a public API. To achieve this, I am utilizing Jquery UI. However, I encountered the following error and I am uncertain about how to resolve it. Un ...