I am trying to create a two column layout that only displays two items per row. Something like what’s mentioned on here Align child elements of different blocks but my HTML structure is different and that solution doesn’t work in my case.
Here is the structue of my HTML:
<div class="twoLayout">
<div class="LeftColumn">
<div class="childItem"></div>
<div class="childItem"></div>
<div class="childItem"></div>
<div class="childItem"></div>
</div>
<div class="RightColumn">
<div class="childItem"></div>
<div class="childItem"></div>
<div class="childItem"></div>
<div class="childItem"></div>
</div>
</div>
The question I have is, given this structure of HTML, how would I use CSS Gird or Flexbox to make sure that each childItem in the LeftColumn is aligned exactly with each childItem in the RightColumn on the same row?
Right now the issue I am running into is that sometimes depending on how long the content is on some of these childItems the alignments are getting messed up. For example this how it current looks:
But I am hoping it would look something like this where each childItem from the Left and Right column are alinged on the same row:
I know that if I didn’t have the LeftColumn
or the RightColumn
div, I could easily use CSS-Grid or Flex Box to fix the alignment.
But suppose I can’t modify this HTML, could I still use CSS-Grid or Flex box or any other technique to make the connection between the child item in left column and child item in the right column to fix the alignments?
2
Answers
I don’t know if it’s possible to achieve the appearance you’re asking for with pure CSS alone. However, you can use a minimum height to ensure that the divs don’t overflow unexpectedly. Additionally, employing a maximum height can help ensure that the elements are aligned correctly, although this might result in some content being cut off.
In my understanding you need to show left column child items and right column child items in the same row .
if so it is possible with flex, hope it will help you.