Reordering divs in one column with CSS Flexbox to transition to a two-column layout upon resizing the screen

Exploring the world of Flexbox, I'm not entirely convinced it's the right solution for my layout challenges. Here's what I have in mind:

In a 940px wide space, I need to create three columns with fixed widths of 300px each. However, on medium to large screens, the first row should consist of two columns - one spanning 600px and the other 300px, which contains two stacked divs. On smaller screens, the 600px div should take full width while the 300px divs reflow horizontally on the following row. Finally, on the smallest screens, all divs should stack vertically.

I'm hesitant to resort to floats due to the complexity they introduce, conflicting with reusable CSS classes preferred for JavaScript functionality by other developers. Therefore, I am exploring whether flexbox can offer a responsive solution using repeatable classes for the divs. See below for what I have built so far:

HTML

        <div class="container">
  <div class="content">
    <div class="row">
      <div class="flex-container">
      <div class="flex-item flex-item--1">1</div>
      <div class="flex-container-inner">
       <div  class="flex-item flex-item--2">2</div>
      <div  class="flex-item flex-item--3">3</div>
      </div>
      </div>
    </div>
      <div class="row">
        <div class="flex-container">
       <div class="flex-item">4</div>
      <div class="flex-item">5</div>
      <div class="flex-item">6</div>
      </div>

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

CSS

.container {
  width: 960px;
  margin: 0 auto;
}
.content {
  width: 940px;
  margin-left: 10px;
  margin-right: 10px;
}
.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  justify-content: space-around;
}
.flex-item {
  background: #abc;
  width: 300px;
  height: 150px;
  margin-top: 20px;
  color: blue;
  font-size: 2em;
}
.flex-item--1 {
  width: 600px;
  margin-left: 0;
}
.flex-item--2 {
  height: 75px;
}
.flex-item--3 {
  height: 55px;
}
  @media screen and (min-width: 320px) and (max-width: 767px) 
  { 
 }
 @media screen and (min-width: 768px) and (max-width: 959px) {
  .flex-item--1 {
    flex: 2 0px;
  }
  .flex-item--2 {
    order: 1;
  }
  .flex-item--1 {
    order: 2;
  }
  .flex-item--3 {
    order: 3;
  }
}
@media screen and (min-width: 959px) and (max-width: 1023px) {}

  @media screen and (min-width: 1024px) {}

This is how I envision the layout between 768px and 959px: https://i.sstatic.net/dimWL.png

CODEPEN Example

UPDATE

Provided are different layouts at various screen sizes. Despite initial beliefs that Flexbox would handle this responsively with ease, adding more divs does not result in seamless reflowing. Perhaps numerous media queries will be necessary to target these divs accurately for optimal responsiveness as illustrated in the image below: https://i.sstatic.net/9vix1.png

Answer №1

Looks like I finally cracked it.

Here's the breakdown:

  1. I switched your workspace setting to box-sizing: border-box. This takes care of margins automatically, eliminating the need for manual adjustments in

    .content { width: [container minus margins] }
    .

  2. I revamped the media queries structure so that the base design caters to the smallest screen size and progresses upward from there. Many experts recommend this approach, and it makes the most sense to me. It's easy to alter if you prefer a different setup. Additionally, I removed all unnecessary and (max-width: [whatever]) queries for clarity.

  3. I omitted prefixes/alternate syntax like -webkit- as I'm not too familiar with them and didn't want to lead you astray. Adding them is a simple task.

  4. All widths are expressed in percentages for better visibility of logic and relationships. This facilitates adjustments for various screen sizes, especially on mobile devices. However, feel free to substitute these values with specifics as needed.

http://codepen.io/anon/pen/zNpgpJ

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 connecting a DOM element when a link is clicked

I am currently browsing http://localhost:8000/index.html My goal is to navigate to localhost:8006/someAddress and interact with an element with the id '157579' as well as click on a button with the id '1787' that will trigger a change ...

Picture in a clear background without any alteration

Is there a way to create a background-color with opacity inside an image without affecting the image itself? The issue lies in the fact that the transparent background-color makes everything underneath it, including text and the image, transparent. Below ...

Sending output from javascript back to html

<head> function displayProductName(name) { } </head> <body> <img src="...images/car.jpg" onclick="displayProductName('car')"> </body> How can I modify this javascript function to display the value received from t ...

What is the best way to manage a session using JavaScript?

Currently developing a website that initially hides all elements except for the password box upon loading. Once the user enters the correct password, all webpage elements are revealed. Seeking a solution to keep the elements visible on reload by utilizing ...

Can the orientation of the card reveal be customized in Materializecss?

Exploring the card-reveal feature in the materializecss framework on this page: https://codepen.io/JP_juniordeveloperaki/pen/YXRyvZ with official documentation located at: In my project, I've rearranged the <div class="card-content"> to display ...

Learn how to showcase real-time stock information from Yahoo Finance on an Android device. See the JSON format provided below for guidance

finance_charts_json_callback( { "meta" : { "uri" :"/instrument/1.0/PTC/chartdata;type=quote;range=1d/json/", "ticker" : "ptc", "Company-Name" : "PTC Inc.", "Exchange-Name" : "NMS", "unit" : "MIN", "timezone" : "EDT", "curr ...

CSS3 Background-size Inquiry

Is there a way to stop the resizing of my background image using CSS? Currently, I have the background-size property set to 100%, but I would like it to remain at that size once the div reaches a minimum height of 800px. Any suggestions on how to achieve t ...

The dynamic addition of divs to the HTML body is causing them to overlap

Check out this functional plunker that is nearly complete. The current issue I am facing is with the chat divs being dynamically added to the body using $compile, but they all have the same CSS class and end up overlapping each other, like so: .chat-wind ...

Tips for getting free jqgrid to display frozen column borders in the search toolbar

If the actions column has a caption of "Activity" or custom settings and is frozen, the free jqgrid will not display column borders for this column in the search toolbar. Vertical lines do not appear in the search toolbar: Is there a way to resolve this ...

Using jQuery to temporarily disable a div element

I need to implement a timer in a div that will disable it for a specific duration, such as 3 seconds. Here is the HTML code snippet: <div class="answ-container" align="center"> <div class="c_answer" id="1316" name="1" data-rcat-no="1"> ...

Can we use Selenium to extract text from a webpage that is not present in the HTML but is instead fetched from an API response?

There is text in the text field, but the HTML isn't displaying it. Check out the Text field with HTML. Can Selenium extract the text "test" if it is retrieved from an API? ...

Using jQuery UI to create sortable lists that are connected and have hidden overflow features

How can I hide overflow from two fixed-height divs containing sortable lists connected to each other without affecting their connection? For example, if overflow is set to hidden, the list item doesn't display when dragged outside of the div. I' ...

Apply CSS styling (or class) to each element of a React array of objects within a Component

One issue I'm facing involves adding specific properties to every object in an array based on another value within that same object. One such property is the background color. To illustrate, consider an array of objects: let myObj = { name: "myO ...

Storing personalized HTML content in a database for individual users: A step-by-step guide

I have a JavaScript code snippet similar to this fiddle: http://jsfiddle.net/nj4N4/7/ On my webpage, it displays like this: image. When you click on the "add a year" button, a table resembling year2 appears above the previous year. Now, I'm looking ...

Shrink the size of the fixed navigation menu

I am experiencing a significant issue with a fixed menu when the browser is resized, as the list items are overlapping and extending beyond their defined section. It's quite frustrating :(. I have been considering two possible solutions: 1: Set a fixe ...

Implementing the linking of a URL to an HTML hidden button that is dynamically displayed through a Javascript program

I'm currently developing a basic webpage that has a feature allowing users to download the final result as an image file in the last step. To achieve this, I've added a hidden HTML button for downloading the result image and used CSS to set its ...

Issues arise when dealing with a CSS and HTML accordion

I decided to create a CSS and HTML collapsing menu system, also known as an accordion. I found a tutorial on how to do it here. However, I had to make some modifications because I wanted to include images in the 'sections' later on. Instead of us ...

Modify the text color of all input elements with Tailwind

I have been using this code in multiple sections of the app: <label className="input text-neutral-content"> Name <input type="text" className="grow text-base-content"/> </label> While I understand that ...

Tiny containers are positioned outside the boundaries of the larger container

click here to view image description Having trouble with my dashboard layout When adding small boxes, they exceed the page boundaries when moving to a new row The boxes in the first row display correctly, but issues arise with subsequent rows Seeking advi ...

Interactive webpages with dynamic HTML content, similar to the design of popular platforms such

Explore the source code of Dropbox's homepage or any Soundcloud page. They utilize various scripts and minimal pure HTML content (article, main, p, div). This method of generating pages is referred to as dynamic content/HTML. The main function seems ...