3

I have a bootstrap layout like this:

<div class="container">
    <div class="row">
        <div class="col-sm-6">
            <div>
                This block should be the same height as the right column.
                The content in this block is in a scrollable container.
                The scrollable container currently has a fixed height set to allow scrolling.
            </div>
        </div>
        <div class="col-sm-6"></div>
            <div>Block 1 content</div>
            <div>Block 2 content</div>
    </div>
</div>

Currently on desktop the layout looks like this:

Current desktop layout

When I switch to smaller screen size breakpoints the layout changes to this:

enter image description here

I would like the scrollable area to take on the same height as the two content blocks on the right. I have tried using display: flex on the row but it displays the entire content of the scrollable area if I remove the specified height from it, which is not what I want.

I basically want the right column dictate the height for the left column.

At the moment I'm using JavaScript to do the resizing but ideally I would like to do this in CSS if possible.

JsFiddle: https://jsfiddle.net/b12ybxpm/

b3n
  • 3,805
  • 5
  • 31
  • 46

1 Answers1

1

I basically want the right column to dictate the height for the left column.

Try adding this:

.row {
     display: flex;
}

.left-column,
.right-column {
     display: flex;
     flex-direction: column;
}

.left-column > *,
.right-column > * {
     flex: 1;
}

By adding display: flex to .row, the children (flex items) of .row – left column and right column – will automatically have equal heights.

However, the children of the flex items – in this case, the scrollable content and content blocks – are not flex items, and have no reason to stretch to fill their container.

Hence, apply display: flex to the parents, which then converts the children into flex items, and by default they stretch to fill their container.

Since it appears you're working in a column direction, give each child a flex: 1, so they know to stretch in that direction.


UPDATE (revision based on new code posted in question)

The code posted in your fiddle is somewhat complex, considering all the nested divs.

With all these nested divs, there's a good chance my code above will not work, as it's based on the images you posted (not the actual code posted later).

In order to achieve equal heights in the demo using flexbox (no JS required), you'll need to make each nested div a flex container.

Here's your demo, revised: https://jsfiddle.net/b12ybxpm/4/

Community
  • 1
  • 1
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701