Utilizing Flexbox for Nested Columns

I am attempting to nest flexbox columns inside a flexbox layout. This is the HTML code I have:

<div class="container">
  <div class="row flex height">
    <div class="col-md-6 red">
    </div>
    <div class="col-md-6 orange">

      <div class="flex flex-columns">
        <div class="row black flex">
          <div class="col-md-3 yellow">
          </div>
          <div class="col-md-9 green">
          </div>
        </div>

        <div class="row white flex">
          <div class="col-md-6 blue">
          </div>
          <div class="col-md-6 indigo">
          </div>
        </div>
      </div>

    </div>
  </div>
</div>

and this is my CSS:

.container {
  border: 1 px solid pink;
}

.height {
  min-height: 500px;
}

.flex {
  box-sizing: border-box;
  display: flex;
}

.flex-columns {  
  display: flex;  
  flex-direction: column;
}

.row {
  flex: 1;
}

.red {
  background-color: red;
}

.orange {
  background-color: orange;
}

.yellow {
  background-color: yellow;
}

.green {
  background-color: green;
}

.blue {
  background-color: blue;
}

.indigo {
  background-color: indigo;
}

.violet {
  background-color: violet;
}

.black {
  background-color: black;
}

.white {
  background-color: pink;
}

Below is an illustration of what I'm aiming for:

You can view my codepen at: http://codepen.io/r3plica/pen/PqWqKx?editors=110

If you understand what I'm trying to accomplish and can provide assistance, it would be greatly appreciated!

Answer №1

If my understanding is correct, your desired layout can be achieved using the following CSS:

.flex-columns {  
  display: flex;
  flex-direction: row;
}
.row {
  flex: 1;
}

@import '//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css';
.flex {
  min-height: 500px;
  box-sizing: border-box;
  display: flex;
}
.flex-columns {  
  box-sizing: border-box;
  display: flex;
  align-content: stretch;
}
.row {
  flex: 1;
  margin: 0; /* Removing unnecessary bootstrap margins */
}
.red    { background-color: red;    }
.orange { background-color: orange; }
.yellow { background-color: yellow; }
.green  { background-color: blue;   }
.blue   { background-color: orange; }
.indigo { background-color: indigo; }
.violet { background-color: violet; }
.black  { background-color: black;  }
.white  { background-color: white;  }
<div class="container">
  <div class="row flex">
    <div class="col-md-6 red">
    </div>
    <div class="col-md-6 orange">
      <div class="flex flex-columns">
        <div class="row black"></div>
        <div class="row violet"></div>
      </div>
    </div>
  </div>
</div>

Answer №2

I made adjustments to your HTML and CSS code to ensure that the layout matches the image, disregarding color variations.

  1. Introduced padding to each div element
  2. Utilized flex-grow property for certain flex-items in order to occupy their parent containers fully (by applying the row class)
  3. Eliminated div.flex.flex-columns and transferred its classes to its parent, resulting in div.col-md-6.orange.flex.flex-columns. This step simplifies the structure and avoids layout issues.
  4. Adjusted the flex-grows of the purple divs shown in the image to alter their proportions

To visualize the changes, execute the provided snippet below. Although additional padding may be required for inner divs to precisely replicate the image, this may not be the primary concern of your inquiry.

.container {
  border: 1px solid pink;
}
div {
  padding: 10px;
}
.height {
  min-height: 500px;
}
.flex {
  box-sizing: border-box;
  display: flex;
}
.flex-columns {
  display: flex;
  flex-direction: column;
}
.row {
  flex: 1;
}
.red {
  background-color: red;
}
orange {
  background-color: orange;
}
.yellow {
  background-color: yellow;
  flex-grow: 2;
}
.green {
  background-color: green;
  flex-grow: 3;
}
.blue {
  background-color: blue;
  flex-grow: 3;
}
.indigo {
  background-color: indigo;
  flex-grow: 2;
}
.violet {
  background-color: violet;
}
.black {
  background-color: black;
}
white {
  background-color: pink;
}
<div class="container">
  <div class="row flex height">
    <div class="col-md-6 red row"></div>
     <div class="col-md-6 orange row flex flex-columns">
      <div class="row black flex">
        <div class="col-md-3 yellow row"></div>
         <div class="col-md-9 green row"></div>
       </div>
       <div class="row white flex">
         <div class="col-md-6 blue row"></div>
         <div class="col-md-6 indigo row"></div>
       </div>
     </div>
   </div>
</div>

Answer №3

It is recommended that you do not set the flex-direction property for your .flex-column:

.flex-columns {  
  box-sizing: border-box;
  display: flex;
  /* flex-direction: column; */
  align-content: stretch;
}

To make necessary adjustments, include the following CSS:

.flex-columns > .row{
  flex: 1 0 auto;
  margin: 0; /* this will reset any padding/margins applied to columns by bootstrap */
}

Forked pen - http://codepen.io/braican/pen/PqWqvr

@import '//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css';
.flex {
  min-height: 500px;
  box-sizing: border-box;
  display: flex;
}

.flex-columns {  
  box-sizing: border-box;
  display: flex;
  /* flex-direction: column; */
  align-content: stretch;
}
.flex-columns > .row{
  flex: 1 0 auto;
  margin: 0;
}


.red {
  background-color: red;
}

.orange {
  background-color: orange;
}

yellow {
  background-color: yellow;
}

.green {
  background-color: blue;
}

.blue {
  background-color: orange;
}

.indigo {
  background-color: indigo;
}

.violet {
  background-color: violet;
}

.black {
  background-color: black;
}

.white {
  background-color: white;
}
<div class="container">
  <div class="row flex">
    <div class="col-md-6 red">
    </div>
    <div class="col-md-6 orange">

      <div class="flex flex-columns">
        <div class="row black">
        </div>

        <div class="row white">
        </div>
      </div>

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

Tips for centering text on a webpage using the div element in HTML

I need help aligning the text "Monitoring Engineering Dashboard" to the right side of the image in the top-left corner. Currently, it is positioned below the image (Refer to the image below) <div style="width:1200px; margin:0 auto ...

Include a legal disclaimer on a website comprised of individual pages

The website at was uniquely crafted by hand, without the use of platforms like Wordpress or any PHP-type databases. Upon its creation over 15 years ago, a legal notice was not initially included on the website. Now, there is a desire to add a link to a l ...

The sidebar scrolling feature seems to be malfunctioning, as only the page scrolling functionality is currently

I'm currently working on a webpage that features a sidebar, along with other elements as shown below: In this design, the Your Friends section represents the sidebar. However, I have noticed that when I hover over the sidebar and try to scroll, it&ap ...

Forced Landscape Viewing on Mobile Site with No Auto-Rotation

My website is equipped with a mobile stylesheet: <link rel="stylesheet" href="css/mobile.css" media="handheld"> In addition to that, I am utilizing jQuery to customize functionality for mobile devices. I am wondering if there is a method to enforc ...

How to conceal hyperlink underline with css

This is some HTML Code I'm working with <a href="test.html"> <div class=" menubox mcolor1"> <h3>go to test page</h3> </div> </a> Here's the corresponding CSS: .menubox { height: 150px; width: 100%; ...

Embed an HTML element within a DIV container

How can I add an HTML tag such as <p> to wrap around the text "Search"? <button id="ihf-quicksearch-submit2" class="btn btn-lg btn-block btn-primary btn-form-submit ihf-main-search-form-submit" type="submit"> Search </button> I am looki ...

Having trouble with the click button flip function? It seems to be working in one section but not in

Currently, I am facing an issue with a card section that contains two buttons and a description. The first button adds an image which is working perfectly fine, as well as the description section. On the other hand, the second button adds a video and when ...

Issue with spacing dropdown choices within primary navigation bar

Struggling with my final project for a class as the semester comes to a close. I've received minimal help from my Professor and hit a roadblock. The main navigation on the side is working correctly, but the dropdown options are stacking on top of each ...

The hover animation is not functioning properly in Icomoon

I have implemented a menu with a CSS3 hover animation called toTopFromBottom. Now, I want to replace the Font Awesome icons with Icomoon icons and apply the same animation effect: HTML: <div id="top-bar" class="top-bar"> <div class="container" ...

Design: Seeking a layout feature where one cell in a row can be larger than the other cells

To better illustrate my goal, refer to this image: Desired Output <\b> Currently, I'm achieving this: current output Imagine 7 rows of data with two columns each. The issue arises in row 1, column 2 where the control needs to span 5 row ...

Tips for displaying CSS background color on top of an inline style background image

I am facing a dilemma while using Laravel - if I put a background image into my CSS style sheet, I lose my variable. Currently, this is how I have it set up: <div class="gradient"><div class="topback" style="background-image: url('/storage/c ...

Ensure child elements do not surpass parent size in HTML/CSS/Javascript without altering the children directly

I am intrigued by how iframes neatly encapsulate all site data within a frame and adjust it to fit the size. Is there any method to achieve this functionality in an HTML wrapper? Can the wrapper be configured so that regardless of the content, it is dis ...

Error copying cell width attribute when using border collapse

Currently, I am attempting to duplicate a table header by copying the tr HTML and then replicating the th widths. Unfortunately, this method is unsuccessful as the widths are displayed as (width minus W) pixels when border: 'Wpx' and border-colla ...

CSS designs that adapt flawlessly to high-resolution mobile devices with perfect responsiveness

Currently, I am implementing max-width: 768px to modify the appearance of my website. However, with the increasing number of high-resolution devices available in the market such as 4K mobile phones, I am wondering how I can detect them. Should I consider ...

The positioning of the text appears to be in the center, but it is actually

Currently, my text "lorum ipsum" is centered in CSS, but I want to align it to the left and add padding on the right later. Can someone explain what I've created here? #ubba { font-family: "Open Sans", sans-serif; font-size: 20px; ...

What is the best way to align this form perfectly in Bootstrap 4?

HTML <div class="container-fluid background"> <div class="row"> <!-- background effect --> <div id="particles-js"> <div class="login col-12"> <form class="login-form" method="post" action=""> ...

Setting the z-index for a JavaScript plugin

I need help with embedding the LinkedIn plugin into a website that has stacked images using z-index for layout. I have tried assigning the plugin to a CSS class with a z-index and position properties, but it hasn't worked as expected. Can anyone sugge ...

Instant webpage updates upon editing CSS/HTML live

I recently watched a tutorial where a designer was updating CSS with live browser refreshes. What stood out to me was that the browser instantly showed any changes made to the CSS or HTML without needing to manually refresh the page. I am using a Mac with ...

The overflow scroll feature is activated, causing the background to appear greyed out, yet the

I've been struggling for hours to achieve this layout: Check out the code example here html, body { height: 100%; overflow-y: hidden; overflow-x: hidden; margin: 0px; padding: 0px; } .MyNavbar { height: 30px; background-color: red; } ...

"TailwindCSS opts for the inclusion of prefexied utilities instead of relying

Trying to set the height of the div to h-2, with a class that includes height animation on medium devices. The issue is that even though h-2 should be used on mobile, tailwindcss still uses h-20 instead. Any thoughts on why this may be happening? Here i ...