Having a flexbox container with two dynamic content columns, it becomes challenging to adjust the height of the right column without affecting the alignment. Setting a top margin does not yield the desired result as the bottom remains aligned with the left column due to default flex item specifications. Check out this Fiddle for reference.
.container {
display: flex;
border: 1px solid red;
padding: 10px;
overflow: hidden;
}
.container .component {
width: 50%;
box-sizing: border-box;
/*height: 250px;*/
border: 1px solid black;
padding: 0px 10px;
}
.container .right {
margin-top: 20px;
}
<div class="container">
<div class="component left">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
</div>
<div class="component right">
<p>
Morbi sit amet pharetra turpis, id placerat sem. Fusce tempus nisl vel arcu euismod, nec convallis risus ullamcorper. Duis fringilla molestie lacus nec rhoncus. Nunc ex massa, placerat id risus sed, luctus varius elit. Nam vestibulum orci a massa sollicitudin
porttitor. Donec sagittis odio metus, eu aliquam tellus blandit eget. Aenean viverra dapibus risus at euismod. Ut hendrerit scelerisque libero, at accumsan ligula vestibulum in. Vivamus tristique erat faucibus ante dapibus vestibulum. Maecenas id
auctor elit. Nullam at interdum arcu, in gravida nisl. Nunc lacinia, quam a hendrerit auctor, sapien est pulvinar mauris, non laoreet justo elit id risus. Nullam vel nulla ligula. Vestibulum at tincidunt orci, non rhoncus sapien. Vivamus nunc eros,
tempus a ex non, dictum posuere enim.
</p>
</div>
</div>
To achieve the desired layout without explicitly setting fixed heights, you can modify the CSS properties accordingly. Uncomment the height property in the code snippet above:
.container {
display: flex;
border: 1px solid red;
padding: 10px;
overflow: hidden;
}
.container .component {
width: 50%;
box-sizing: border-box;
height: 250px;
border: 1px solid black;
padding: 0px 10px;
}
.container .right {
margin-top: 20px;
}
<div class="container">
<div class="component left">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
</div>
<div class="component right">
<p>
Morbi sit amet pharetra turpis, id placerat sem. Fusce tempus nisl vel arcu euismod, nec convallis risus ullamcorper. Duis fringilla molestie lacus nec rhoncus. Nunc ex massa, placerat id risus sed, luctus varius elit. Nam vestibulum orci a massa sollicitudin
porttitor. Donec sagittis odio metus, eu aliquam tellus blandit eget. Aenean viverra dapibus risus at euismod. Ut hendrerit scelerisque libero, at accumsan ligula vestibulum in. Vivamus tristique erat faucibus ante dapibus vestibulum. Maecenas id
auctor elit. Nullam at interdum arcu, in gravida nisl. Nunc lacinia, quam a hendrerit auctor, sapien est pulvinar mauris, non laoreet justo elit id risus. Nullam vel nulla ligula. Vestibulum at tincidunt orci, non rhoncus sapien. Vivamus nunc eros,
tempus a ex non, dictum posuere enim.
</p>
</div>
</div>
The example above demonstrates that setting a margin-top
of 20px
affects both the top and bottom alignments of the right column. Is there an alternative method to achieve this layout without specifying a fixed height?