Tips for creating flexbox children that adjust their height to match the first child, even when it has varying heights

Imagine a flexbox with 3 elements:

The first child contains 4-7 lines. The second child contains 40-50 lines. The third child contains 2-5 lines.

My goal is to make the height of the second child match that of the first child.

I am aware that I could set the height: 300px or some fixed value, but the content length varies.

Here's a visual example (I want the second child [green box] to have the same height as the first child [purple box]:

https://i.sstatic.net/wNL0v.png

I've attempted various methods unsuccessfully (such as using flex-shrink). Here's a snippet of the code:

I have thought about using JavaScript to determine the height of the first child and applying it to the parent, but is there a CSS-only solution?

.parent {
  background-color: red;
  display: flex;
  flex-direction: row;
  gap: 1rem;
}

.child {
  width: 33.3333333%;
  font-size: 2rem;
  // height: 300px; // this works but then the height would not match the first child's
}

.child1 {
  display: flex;
  background: purple;
}

.child2 {
  background: green;
  overflow: scroll;
}

.child3 {
  background: grey;
}
<div class="parent">
  <div class="child1 child">
    This div has dynamic amount of lines... <br>line 1 <br> line 2 <br/>line 3 <br> line 4 <br> line 5<br/>
  </div>
  <div class="child2 child">
    This div has a lot of lines... <br>line 1 <br> line 2 <br/>line 3 <br> line 4 <br> line 5<br/> line 6 <br> line 7 <br/>line 8 <br> line 9 <br> line ...<br/>
  </div>
  <div class="child3 child">
    some other div <br> 1 <br>2 <br/> 3 <br> 4
  </div>
</div>

Answer №1

If you're open to making some adjustments to your HTML structure, here's a solution that requires minimal changes:

  1. Enclose the content of child2 and child3 within another div, let's name it wrapper.
<div class="child2 child">
  <div class="wrapper">
    This particular div contains many lines... <br>line 1 <br> line 2 <br/>line 3 <br> line 4 <br> line 5<br/> line 6 <br> line 7 <br/>line 8 <br> line 9 <br> line ...<br/>
  </div>
</div>
<div class="child3 child">
  <div class="wrapper">
    some other piece of content <br> 1 <br>2 <br/> 3 <br> 4
  </div>
</div>
  1. Set wrapper to have position: absolute, so the heights are solely based on children without a wrapper, like child1. Then apply scroll: auto to them.
.child {
  width: 33.333333%;
  font-size: 2rem;
  position: relative;
}

.wrapper {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  max-height: 100%;
  overflow: auto;
}

Below is the complete solution:

.parent {
  background-color: red;
  display: flex;
  flex-direction: row;
  gap: 1rem;
}

.child {
  width: 33.333333%;
  font-size: 2rem;
  position: relative;
}

.wrapper {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  max-height: 100%;
  overflow: auto;
}

.child1 {
  display: flex;
  background: purple;
}

.child2 {
  background: green;
  min-height: 0;
  position: relative;
}

.child3 {
  background: grey;
}
<div class="parent">
  <div class="child1 child">
    This div has variable number of lines... <br>line 1 <br> line 2 <br/>line 3 <br> line 4 <br> line 5<br/>
  </div>
  <div class="child2 child">
    <div class="wrapper">
    This particular div contains many lines... <br>line 1 <br> line 2 <br/>line 3 <br> line 4 <br> line 5<br/> line 6 <br> line 7 <br/>line 8 <br> line 9 <br> line ...<br/>
    </div>
  </div>
  <div class="child3 child">
    <div class="wrapper">
      some other piece of content <br> 1 <br>2 <br/> 3 <br> 4
    </div>
  </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

Why does the pound symbol in z-index always show up in Angular?

Having an issue with my code where I set a z-index in the CSS like this: .mat-mini-fab { position: absolute; right: 5px; top: 4px; z-index: 999; box-shadow: none !important; } However, whenever I visit my site, the z-index is not being appl ...

Struggling to alter Material-UI theme styles

I have been working on a project using Material-UI and I am trying to change the default theme style of textTransform:"uppercase" to textTransform:"capitalize." After checking out the documentation on custom styling, I learned that I should use inline sty ...

Tips for aligning two cards in the center with Bootstrap

[site][1] [1]: https://i.sstatic.net/CmQRP.png Hey there, I'm trying to figure out how to center my two cards on the page. Can anyone help me with this? <div> <div class="card" style="width: 18rem;"> <img ...

Tips for making CSS Hover Transform functional when using relative positioning

JSFiddle I am trying to create a heading text with an arrow that translates when hovered over. Currently, it is achieved using position: absolute, but I need to make it work with position: relative. Any suggestions on how this can be accomplished? #co ...

The datepicker within the dialog box is obstructed

There seems to be an issue that I'm encountering: https://i.sstatic.net/7aFqA.png Whenever I click on the datepicker input, the calendar appears overlapped. This is evident because when I hide the dialog, the calendar displays correctly: https://i. ...

Where should I put my script in order to fix the issue with my JSFiddle?

Every time I attempt to run it on my local machine, the numbers vanish and then reappear when I select the checkboxes, just as I intended. It seems like there might have been an issue with linking the external resource script. Can someone advise me on wher ...

Is there a way to make jQuery Validate display errors within the form elements rather than outside of them?

Given the jQuery Validate basic example provided in the link below, what method can be used to display error messages within form elements whenever feasible (excluding checkboxes)? http://docs.jquery.com/Plugins/validation#source ...

Can an image in CSS trigger the display of a <div> upon clicking it?

Here is an example of my HTML code: <html> <head> <style> img.settings { hight: 100px; width: 20px; } div.settings { position: fixed; left: 0px; top: 0px; right: 100%; bottom: 0px; background-color: #0000 ...

The curly brackets in Vue.js do not function properly within a v-for loop when there is an object nested inside an array

I am a novice in vuejs and I am attempting to utilize mustache {{}} to render elements of an array. It works fine for simple arrays, but when I try it with an array containing objects, it doesn't seem to work. Is there an issue with my code, or is it ...

Is there a way to adjust the text color based on whether a value is negative?

I am currently developing a web application that performs addition operations on integers. Within this application, I have implemented two functions named num1() and num2() to retrieve the integers provided by the user for calculation. My objective is to m ...

What is the best way to prevent a modal from being triggered specifically on mobile devices such as phones and

$(function() { $('.pop').on('click', function() { $('.imagepreview').attr('src', $(this).find('img').attr('src')); $('#imagemodal').modal('show'); }); }); .modal-ba ...

Ways to confine the tabindex within a specific div container

I am currently working on identifying examples of bad accessibility practices. Specifically, I am focusing on issues related to Keyboard Focus. The first example I have encountered is the lack of visibility when trying to navigate through a set of buttons. ...

The input[type="submit"][disabled] is malfunctioning on IE8

My input submit button has 3 different states: Active : Orange button Mouse down : Dark gray button Disabled : Light gray button <input type="submit" id="savebtn" class="save_button"/> CSS: .save_button { background: url('/Images/save_but ...

Do CSS have a conflict resolution rule? While my fadein keyframes function perfectly, I encounter issues with my slidedown animation

Using react and css, I have created two animations in my css file: @keyframes fadein { 0% { visibility: hidden; opacity: 0; } 50% { opacity: 0.5; } 100% { visibility: visible; opacity: 1; } } ...

Check for compatibility of overflow:scroll with mobile browsers

Is there an easy JavaScript method that works across different devices and libraries? I am looking to assign a class to the html element in order to enable scrollable containers on mobile devices when needed. I want to follow a similar approach to Modern ...

Creating effective href anchors within an iframe using the `srcdoc` attribute

While loading some HTML content in an iframe using the srcdoc attribute, I encountered an issue where following anchor tag links inside the iframe caused the entire page to load within the iframe instead of scrolling to the linked id. You can see a demons ...

Redesigning the reset Zoom feature of HighCharts using a button inspired by Material UI styling

Currently, I am developing a project that incorporates HighCharts and Material UI for the user interface design components. I am wondering if there is a method to substitute the default HighChart reset Zoom button with the Material UI button component? ...

What is the best positioning option to choose: "Relative? Static, or something else?"

I decided not to use float for positioning my divs in CSS; instead, I used the position property with relative value. However, when viewing the site on different screen resolutions, all the divs end up getting jumbled. Can someone please explain what I m ...

Creating a unique button design that incorporates a select tag and utilizes a dynamic percentage width

How can I keep a custom button positioned 20 pixels away from the right side of a select dropdown menu with a fixed width percentage? Using percentages in the background-position property makes the button move too far when the window is large. Setting the ...

Proportional fluid image grid with responsive design

After implementing various media queries, I've managed to create an image grid using Bootstrap 4+ that looks great on specific devices and layouts. Here's the reference code: .cmd-three-img-container { position: relative; margi ...