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

MTG Life counter. Display fluctuations in count

I am currently working on a fun project creating an MTG (Magic The Gathering) life tracker, even though the code is quite messy. Despite its flaws, it still gets the job done. Click here to view the MTG life tracker https://i.stack.imgur.com/Su17J.png ...

Safari on IOS transition transform feature malfunctions

Every time I try to apply some code to make a div move, for example using the latest iOS Safari browser, it fails to transition between the two rules set. I have experimented with different values other than percentage but still haven't been able to m ...

Instructions on using $.post in jquery to sude uploaded file to a php file for processing

Is there a way to upload photos asynchronously using $.post without relying on .serializeArray()? I want to send a form, containing an upload file, to a PHP processing file. Any suggestions? HTML: <form action="upload1.php" method="post" id="usrform" ...

Tips for creating a smooth transition using cubic bezier curves

In my current project, I have implemented a CSS transition using the cubic bezier timing function with values (0.16, 1, 0.29, 0.99). However, I have encountered an issue where at the end of the animation, the element's velocity is too slow and the in ...

Having trouble displaying SVG elements in Material UI

Currently, I am faced with an issue while trying to display a .svg file in the header of a drawer using Material-UI and create-react-app. Instead of the expected .svg image, all I see is a broken image rendering. Any assistance on resolving this problem ...

Maintaining a consistent style input until it is modified

I'm currently dealing with this code (continuing from a previous question): input[type=submit]:focus { background-color: yellow; outline: none; } The issue I'm facing is that when I click anywhere else on the screen, the background color go ...

Styling does not affect an Angular component that is injected into an iframe

I am facing an issue with styling in an iframe when trying to append my own angular component. Despite various attempts, I have been unsuccessful in getting the styling to apply to the component within the iframe. Upon loading the iframe, I use JavaScript ...

Automatically transfer images to a designated folder using PHP

I am just starting out in web development. I have a vision of creating a webpage with a photo gallery. Below is the initial code snippet: <body> <div class="container"> <header class="clearfix"> </header> ...

Creating a Rectangular Trapezoid Shape with CSS - Eliminating Unnecessary Spacing

I'm trying to create a trapezoid button using CSS. Here is the intended look: However, my current implementation looks like this: The button appears fine but there seems to be some excess space below it. It's almost like an unwanted margin, ev ...

Struggling to create a line break within an SVG using the <tspan> element?

I have a pair of text lines that are wrapped in <tspan> tags. <tspan dy="-11.7890625">welcome</tspan> <tspan dy="16.8" x="285.75">text</tspan> I am trying to add a line break between them, but the <br> tag is not worki ...

Having trouble getting the `nth-of-type` and `nth-child` selectors in CSS to function

I am attempting to apply a green color for the first nth child flag element <div id="axis"> <div class="super"></div> <div class="flag">flag 1</div><!-- I want this text to be green--> <div class="supe ...

Having trouble loading Yeoman Webapp SASS

Every time I try to build a web application using 'yo webapp', I encounter a 404 Error. GET http://localhost:9000/styles/main.css 404 (Not Found) I removed compass from the project, but that shouldn't be causing the issue, correct? ...

Guide to creating a custom wrapper for a Material UI component with React JS

I am utilizing the Material UI next library for my project and currently working with the List component. Due to the beta version of the library, some parameter names are subject to change. To prevent any future issues, I have decided to create a wrapper a ...

Utilizing R to Extract Data from HTML Webpages

I am currently extracting flight schedules from JFK's website. You can find the link to access the flight schedules right here; My initial approach involves inspecting a specific field of each flight and recording its xpath. The purpose is to first e ...

Is there a way to output several lines from a JSON file in print form?

I'm working with HTML code that displays multiple lines from a JSON file, but it only shows one line at a time. How can I modify the code to display all users' information? <!DOCTYPE html> <html> <head> <script> function ...

Personalized sorting to match the unique rendering of cells in Material UI Data Grid

I have integrated the Material UI V4 Data Grid component into my React Js application. The Data Grid component requires two props: rows (list type) and columns (list type). Below is a sample row item: const rows = [ { id: 1, customerName: " ...

Storing information in a database

Need help troubleshooting why my form isn't saving data to the database. <div class="container"> <div class="row" style="margin-top:200px;"> <form ENCTYPE="multipart/form-data" ACTION="upload.php" METHO ...

Tips for getting flex-end to function properly in IE11

My attempt to implement justify-content: flex-end; for a DIV with hidden overflow content in IE11 was unsuccessful. Despite trying various approaches, I managed to create a basic code snippet that functions properly in Chrome but fails in IE11: .token- ...

CSS - What's the source of this mysterious whitespace?

I'm currently making adjustments to a Wordpress website that is designed to be responsive. The site uses media queries to adapt its display based on the user's screen size, and everything looks great except for when the screen width is at its sma ...

Utilize HTML5, CSS, and Responsive Web Design to place four boxes into a div container

Is there a way to arrange four boxes inside a div so that they are positioned at the corners of the top-left, top-right, bottom-left, and bottom-right? And when you click on the box in the middle, a window opens with text. I'm looking for something s ...