Can nested divs in an HTML document be styled using Flexbox?

I'm currently attempting to design a layout with two columns, displaying only two items per row. I came across a solution on StackOverflow that aligns child elements of different blocks, but unfortunately, it doesn't apply to my HTML structure.

My HTML structure is as follows:

<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> 

My question pertains to using CSS Grid or Flexbox with this HTML structure in order to ensure each childItem in the LeftColumn aligns perfectly with each childItem in the RightColumn on the same row.

The issue I'm encountering is that due to varying content lengths in some childItems, the alignments are getting disrupted. This is how it looks at the moment: https://i.sstatic.net/72e3s.png

However, my desired outcome is for the childItems from both the Left and Right columns to be aligned on the same row like this: https://i.sstatic.net/1zQFg.png

I understand that without the LeftColumn or RightColumn divs, I could easily resolve the alignment issue using CSS Grid or Flexbox.

If altering the HTML isn't an option, can CSS Grid or Flexbox still be utilized, or is there another technique available to achieve proper alignment between the child item in the left column and the child item in the right column?

Answer №1

To achieve this layout, you can utilize CSS properties such as grid-auto-flow: dense; and display:contents. Here is an example implementation:

.twoLayout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-auto-flow: dense;
  gap: 1em;
}
.childItem {
  align-self: start;
  background-color: #4F71BE;
}
.LeftColumn, .RightColumn {
  display: contents;
}
.LeftColumn .childItem {
  grid-column: 1;
}
.RightColumn .childItem {
  grid-column: 2;
}
body {
  background-color: #262626
}
<div class="twoLayout">
    <div class="LeftColumn">
        <div class="childItem">A</div>
        <div class="childItem">B<br>N</div>
        <div class="childItem">C</div>
        <div class="childItem">D</div>
    </div>
    <div class="RightColumn">
        <div class="childItem">1</div>
        <div class="childItem">2</div>
        <div class="childItem">3<br>100<br>101</div>
        <div class="childItem">4</div>
    </div>
</div> 

Answer №2

It may not be feasible to achieve the desired look using only CSS. However, setting a minimum height can prevent unexpected overflowing of divs. Additionally, implementing a maximum height can aid in proper alignment, though there is a risk of some content being clipped.

.twoLayout {
  display: flex;
  gap: 20px;
}

.LeftColumn, .RightColumn {
  display: flex;
  flex-direction: column;
}
.childItem {
  min-height: 100px; /* Establish a minimum height */
  padding: 10px;
  box-sizing: border-box; 
}
<div class="twoLayout">
    <div class="LeftColumn">
        <div class="childItem">TEXT</div>
        <div class="childItem">TEXTTEXTTEXTTEXTTEX<br>TTEXTTEXTTEXTTEXTTEXTTEXTTEX<br>TTEXTTEXTTEXTTEXT</div>
        <div class="childItem">TEXT</div>
        <div class="childItem">TEXT</div>
    </div>
    <div class="RightColumn">
        <div class="childItem">TEXT</div>
        <div class="childItem">TEXT</div>
        <div class="childItem">TEXT</div>
        <div class="childItem">TEXT</div>
        <div class="childItem">TEXT</div>
    </div>
</div> 

Answer №3

To display both left column child items and right column child items in the same row, you can utilize flex properties. By setting the parent container to display:flex, you can achieve this layout.

   // Set parent <div> display option flex
    .twoLayout{
      display: flex;
    }
   // Set first child <div> LeftColumn flex:1 so it will take all available space in left
    .LeftColumn{
      flex: 1;
    }
    // You can specify any size for flex basis as per your requirement
    .RightColumn{
      flex-basis: 300px;
    }

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

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

Ensure the width of an HTML table by using only the <td witdth> tag

How can I set a fixed width for each column in my HTML table without using the width property in CSS? The current code is not displaying the table properly, with it only rendering at 100% width of the screen. Here's a snippet of the relevant code: ...

Utilizing jQuery to apply a class to an element and modify the CSS parameters with custom values simultaneously

Unsure if this idea is feasible, but I'll attempt to explain my requirements. I am interested in accomplishing something like the following: A CSS class with variables X, Y, and Z left undefined: .r {border-radius:Xpx;-webkit-border-radius:Ypx;-moz ...

Is it possible to create a blurred effect on an image while preserving the clarity of text within

I have been attempting to create an effect where the image blurs and then reveals text. However, I have encountered issues where the image either blurs completely or behaves strangely. My code is behaving oddly, almost like it's giving a signal. (I&a ...

Toggler and Dropdown malfunctioning

I'm currently learning how to utilize Bootstrap, but I'm having trouble getting the Toggler and Dropdown features to expand. I can't figure out if I'm making a mistake somewhere. Below is the snippet of code for the navbar (It's a ...

What is the best way to eliminate the gap beneath v-app-bar in Vue.js?

Is there a way to eliminate the space below v-app-bar? the header segment User_Header.vue <template> <v-app id="user_header"> <v-app-bar app color="#8a0303" dark> <h1>this is app bar</h1> & ...

The CSS Grid is too small to accommodate the Grid Item, even though there is ample space available

I'm currently working on a grid system where the first part (one to five) should be the opposite of the second part (six to ten). I have successfully set up the first part, but I'm struggling with fitting the tenth piece into the grid. I attempte ...

Comparison of Fabric JS Filters and CSS Filters

We are currently in the process of transitioning a project from HTML and CSS to Fabric JS. This project allows users to manipulate images by moving them around and applying filters. In our current HTML/CSS implementation, we handle image positioning with C ...

Clicking the responsive menu does not reveal the hidden overlay div as expected

Struggling with finding the correct function to open the hidden div of my responsive menu on my new website. Check out my code: https://codepen.io/anon/pen/XRJRGE <a class="page-head__menu" id="js-menu" href="#" onClick="open()">Menu< ...

jQuery UI Sortable: Efficiently Drag Items Across Multiple Interconnected Lists

One feature of using jQuery UI Sortable is the ability to make the sortable item container scroll when an item is dragged. This can be achieved by specifying the scroll option. In my case, I have several sortable lists that are connected within separate c ...

Move an object in relation to the right boundary of the viewport

I have a div that starts off the screen due to this CSS code: transform: translateX(100%); Now, I am looking to bring this div back into view by a specific distance from the right side. Although I have tried using transform: translateX(-450px), it seems ...

Expanding scrollbar when hovered over

My goal for a landing page design is to showcase a centered logo at the top, followed by three images with borders arranged side by side, each with a bit of padding. When a user hovers over any of these images, I want the text on that image to slide to the ...

Thumbnails on the grid are vertically aligned to the top

How can I vertically align grid items at the top without manually setting grid span properties for li elements? I need the layout to be flexible and work for any number of list elements. ul { position: absolute; left: 0; top: 0; width: 400px; ...

What adjustments can be made to the Bootstrap code to prevent it from causing layout issues with the Signin Form and Footer?

Recently, I've encountered some challenges with the footer formatting on my website's home page and the sign-in form on a separate page. It appears that the Bootstrap framework at the top of the page is affecting these elements exclusively. How c ...

Can you explain the significance of using `!important` in CSS without specifying a value?

Scenario: Currently, I am facing an issue with using the bootstrap 4 stylesheet in conjunction with NextJS. The bootstrap 4 stylesheet, compiled from SASS, contains code like: .checkbox.checkbox-accent > span { border-width: !important; } This speci ...

Utilize various styles from within a specified font family in CSS

My website is currently using the font-family Roboto with two different font types (400 and 500i). <link href="https://fonts.googleapis.com/css?family=Roboto:400,500i" rel="stylesheet"> Even though I have both fonts available, I am struggling to im ...

The CSS code for the Kendo grid popup is malfunctioning when viewed in Chrome

I included these in my razor view: "kendo.common.min.css" "kendo.default.min.css" "kendo.rtl.min.css" Also added a script for culture, it works fine on Chrome. However, on Firefox everything appears to be working properly. Any suggestions on how to re ...

Centered dropdown positioned under parent element with hidden overflow

In my navigation bar, I have a link that opens a dropdown when clicked. The parent of this link has the CSS property overflow: hidden in place to truncate the text if it becomes too long. However, I am facing an issue where the dropdown is not positioned c ...

jQuery not able to replace background-image on mobile devices

Looking to update a background image using jQuery upon loading the website on mobile devices or screens smaller than 767px. The specific site in question is: I am attempting to replace the initial image on the homepage slider (the blue sky with 'vor ...

Pseudo-element fails to display in React when applied to a paragraph tag, even with display block property set

I am experiencing an issue where the pseudo element ::after is not appearing in my browser. I am currently working with React.js and Material UI's makeStyles. Here is the code snippet causing the problem: modalTitle: { borderBottom: '2px sol ...

Implement CSS to place an image below the text

I have the following HTML & want to enhance it by adding an image BENEATH the h1 element using CSS. <div class="wf-wrap"> <div class="page-title-head hgroup"> <h1>Kookshop</h1> </div> <div class="pa ...