Creating a CSS layout with two columns using Flexbox and varying heights

I'm grappling with the following chunk of code:

.wrapper {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}

.a {
  background-color: red;
  width: 65%;
}

.b {
  background-color: green;
  width: 35%;
}

.c {
  background-color: blue;
  width: 65%;
  height: 100px;
}

.d {
  background-color: orange;
  width: 35%;
}

.e {
  background-color: teal;
  width: 65%;
}

.f {
  background-color: purple;
  width: 35%;
}
<div class="wrapper container">
  <div class="a">A</div>
  <div class="b">B</div>
  <div class="c">C</div>
  <div class="d">D</div>
  <div class="e">E</div>
  <div class="f">F</div>
</div>

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

I want to position F right under D, as shown in this diagram:

https://i.sstatic.net/5vGLQ.png

My rationale for using a single column instead of two separate columns is so that I can reorganize them on mobile later using order. Is it possible to achieve this layout in Flexbox, or is there another approach?

Answer №1

To make a flex column wrap, it is important to have a fixed height defined on the container. In the absence of a breakpoint, the column will continue to expand within the container without wrapping.

For more details on this issue, you can refer to:

  • Is it possible for flex items to align tightly to the items above them?

If facing difficulties with flexbox, considering a grid solution may be more beneficial.

.wrapper {
  display: grid;
  grid-template-columns: 65% 35%;
  grid-template-areas: " a b "
                       ...
                       " e . "
                       " f . " ;}

.a { grid-area: a; background-color: red;    }
.b { grid-area: b; background-color: green;  }
.c { grid-area: c; background-color: blue; height: 100px; }
.d { grid-area: d; background-color: orange; }
.e { grid-area: e; background-color: teal;   }
.f { grid-area: f; background-color: purple; }
<div class="wrapper">
  <div class="a">A</div>
  <div class="b">B</div>
  <div class="c">C</div>
  <div class="d">D</div>
  <div class="e">E</div>
  <div class="f">F</div>
</div>

Browser support for CSS Grid is now quite strong.

In both Grid and Flex layouts, the order property can be utilized. However, in Grid layout, using grid properties might be sufficient for rearranging the layout on mobile screens without requiring order.

Answer №2

Found inspiration in a tutorial on creating Easy Masonry Layout With Flexbox

Works perfectly for my needs

<div id="masonry">
    <img src="jennifer.jpg" alt>
    <img src="samantha.jpg" alt>
    <img src="natalie.jpg" alt>
    …
</div>

div#masonry {
  display: -ms-flexbox;
  -ms-flex-direction: column;
  -ms-flex-wrap: wrap;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  height: 100vw;
  font-size: 0; 
}

div#masonry img { 
    width: 33.3%;
    transition: .8s opacity;
}

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

Extract content from an HTML form within a specific cell using Cheerio

A sample HTML table is displayed below: <tr class="row-class" role="row"> <td>Text1</td> <td> <form method='get' action='http://example.php'> <input type='hidden' ...

Transform Array into an unordered list with list items

Currently, I am dealing with a file that contains strings of file paths in the following format: ./CatDV/S1/SFX/steam/6004_90_04 LargeHeadlightSm.wav ./CatDV/S1/SFX/steam/AirHissPressureRe HIT032001.wav ./CatDV/S1/SFX/steam/Impact_Metal_Bullet_Hit(1).wav ...

Utilize JavaScript to eliminate tags surrounding a text node

If my HTML code looks something like this: <div id="text"> This is some text that has an area <span class="highlight">highlighted with different formatting</span> and then some more text. </div> I am trying to figure ...

Create precise shapes by tracing images with an SVG generator

Does anyone know of a SVG generator that allows me to trace and save a specific shape from an image as an SVG file? I've come across generators that generate the entire image, but I only want to save a particular shape. For example, if there is an im ...

How can I make sure that index.php displays the current page when I am navigating through categories on the WordPress

In my WordPress website, I have set up categories in the main navigation. Specifically, I have a category named "Home" which corresponds to category 4. To display the Home category (start page), I have added the following code to my index.php file: <?p ...

Employing CSS for page breaks, while contained within a DIV that is absolutely positioned

I have integrated the ASP.NET AJAX ModalPopupExtender into a webpage. The popup displays details of customer orders that need to be printed, with each order ideally on a separate page. Unfortunately, the CSS page-break-after style doesn't seem to wor ...

Using Javascript to prevent css from changing the display

I want to make some CSS adjustments to a single element using a single script, but I want all the changes to be displayed at once. For example: $("#someelement").css({width:"100%"}); //this change should not be displayed $("#someelement").width($("#someel ...

Filtering data from an array in PHP

I have a code snippet that functions well when the target variable is a single value. The assumption here is that $bank_country represents a single value, such as Brazil, with no issues. <select class="form-control" multiple="1" name="country[]"> & ...

Enhanced jQuery implementation for hiding elements

I encountered a peculiar issue where jQuery's .is(':hidden') function wrongly returned true for an element that visibly displayed content. You can see the problem demonstrated in this fiddle. The :hidden pseudo checks both offsetWidth and o ...

Sending HTML emails with a Django contact form

Can the django-contact-form be utilized to send an HTML email? ...

What is the best way to access ng model values globally throughout my application?

I am currently working with two modules: the View Items Module and the Add Items Module. Within my project, I have files named app.view.component.ts, app.add.component.cs. How can I utilize the [(ng-model)] values globally in this context? This code snip ...

What could be causing the discrepancy in sizes of the columns in my multi-column layout?

https://jsfiddle.net/drqjmssy/ Seeking assistance from the linked jsfiddle, I aim to align "div class='main_content'" vertically with "div class='sidebar'". What would be the best approach to achieve this alignment? In case the fiddle ...

Tips for moving a div away from the mouse cursor

Creating a landing page where I need my logo to move away from the mouse cursor in a specific direction, but it's currently moving randomly. The page is built using HTML and I have the flexibility to utilize any open-source JavaScript library. <! ...

The alignment of the DIV elements is not as I had hoped

I currently have three different divs on my webpage. Two of them are background divs stacked on top of each other, while the third one contains content and is positioned within the lower background div. My issue arises when I try to make the content div co ...

What is the process for inserting HTML content into the body of an iframe?

Is there a way to insert HTML content into the body of an iframe instead of using the src attribute to call a page's URL? I am looking for a code that is compatible with all browsers and works perfectly. ...

What's the best way to insert an image right beneath a title within a webpage?

Check out my fiddle here. I'm trying to position the pencil directly below the word "blah", but it seems like margin-top is not working as expected... Additionally, I'm struggling with making sure that the pencil stays aligned below "blah" rega ...

Certain letters in the alphabet are unable to be underlined

Struggling to add underlines to certain characters in the alphabet such as p, y, g, and j? I can't seem to get them to show up correctly. Can anyone provide guidance on how to fix this issue? Here is the code I am using: <td style="text-decoratio ...

Could the quantity of JavaScript files impact the performance of a project and cause any delays?

In my current HTML and JavaScript project, I am incorporating multiple JavaScript files. I'm curious to learn about the potential impact of having numerous JavaScript files on a web project's efficiency and speed. Can anyone shed some light on th ...

How can I implement an autocomplete feature in Vue.js?

Recently, I started working with Vue and decided to create a basic search app using Vue.js cdn. My goal is to add a suggestion bar that displays user IDs from a fake JSON server. For instance, if I input '1' in the search bar, I want only the use ...

Steps to obtain the precise source code of a webpage

Is there a way to download the exact source code of a webpage? I have tried using the URL method and Jsoup method, but I am not getting the precise data as seen in the actual source code. For example: <input type="image" name="ctl00$dtlAlbums$ct ...