Adapt a dynamic nested flexbox grid into a stacked layout optimized for mobile devices

Utilizing the power of the CSS3 Flexible Box layout has been crucial in developing a responsive site that caters to various devices.

On desktop screens, I have successfully implemented a flexbox layout that looks like this:

body {
  text-align: center;
}

.wrapper {
  background-color: white;
  display: flex;
}

.nested-wrapper1 {
  background-color: cyan;
  width: 50%
}

.nested-wrapper2 {
  background-color: pink;
  width: 50%
}
<html>

<body>
  <div class="wrapper">
    <div class="nested-wrapper1">
      <div class="1">1</div>
    </div>
    <div class="nested-wrapper2">
      <div class="2">2</div>
      <div class="3">3</div>
    </div>
  </div>
</body>

</html>

I am now faced with the challenge of adapting this layout for mobile devices, where elements 1 and 2 should be displayed side by side, while element 3 should stack below 1 and 2. Refer to the visual representation below:

Answer №1

Perhaps utilizing a flexbox could offer a solution - specifically for mobile views under 650px, by absolutely positioning the third div relative to the wrapper.

One potential concern could be the third div overflowing the wrapper. For a demonstration, refer to the sample below:

body {
  text-align: center;
}

.wrapper {
  background-color: #ddd;
  display: flex;
  position: relative;
}

.nested-wrapper1 {
  display: flex;
  width: 50%;
}

.nested-wrapper2 {
  width: 50%;
}

.nested-wrapper1>[class='1'] {
  background-color: cyan;
  padding: 1em;
  flex: 1;
}

.nested-wrapper2>[class='2'] {
  background-color: pink;
  padding: 1em;
}

.nested-wrapper2>[class='3'] {
  background: lightblue;
  padding: 1em;
}

@media screen and (max-width: 650px) {
  .nested-wrapper2>[class='2'] {
    height: 100%;
  }
  .nested-wrapper2>[class='3'] {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    transform: translateY(100%);
  }
}
<div class="wrapper">
  <div class="nested-wrapper1">
    <div class="1">
      1. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
      survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    </div>
  </div>
  <div class="nested-wrapper2">
    <div class="2">
      2. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
       2. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
    </div>
    <div class="3">
      3. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
      survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
  </div>
</div>

Answer №2

To achieve this effect, you can utilize the display:contents property along with a media query.

For a demonstration, check out this Codepen Demo

For more information on Display:contents, visit the MDN page

These elements do not generate a specific box on their own. Instead, they are replaced by their pseudo-box and their child boxes.

The display:contents property turns off the display of an element, causing it to have no impact on the layout. This means the element and its descendant elements will not be rendered.

Browser Support is limited. Currently, supported by Chrome 58 and FF37, but not by IE/Edge/Opera/Safari.

body {
  text-align: center;
}

.wrapper {
  background-color: white;
  display: flex;
}

.nested-wrapper1 {
  background-color: cyan;
  width: 50%
}

.nested-wrapper2 {
  background-color: pink;
  width: 50%
}

.r-2 {
  background: red;
}

.r-3 {
  background: yellow;
}

@media (max-width:600px) {
  .wrapper {
    flex-wrap: wrap;
  }
  .nested-wrapper2 {
    display: contents;
  }
  .l-1,
  .r-2 {
    flex: 0 0 50%;
  }
  .r-3 {
    width: 100%;
  }
}
<div class="wrapper">
  <div class="nested-wrapper1">
    <div class="l-1">1</div>
  </div>
  <div class="nested-wrapper2">
    <div class="r-2">2</div>
    <div class="r-3">3</div>
  </div>
</div>

Answer №3

Although the original poster requested a flexbox solution, it seems challenging to achieve the desired layout using flexbox. An alternative approach could involve using floats.

The success of this approach will depend on the content and the required proportions of the elements.

body {
  margin: 0;
}
.flexo {
  height: 200px;
}
.flexo > div {
  float: left;
  width: 50%;
  height: 66.66666%;
}
.flexo > div:nth-child(1) {
  background-color: dodgerblue;
}
.flexo > div:nth-child(2) {
  background-color: deeppink;
}
.flexo > div:nth-child(3) {
  background-color: limegreen;
}
.flexo > div:nth-child(3) {
  height: 33.33333%;
  width: 100%;
}

@media ( min-width: 480px ) {

  .flexo > div:nth-child(1) {
    height: 100%;
  }
  .flexo > div:nth-child(2),
  .flexo > div:nth-child(3) {
    height: 50%;
    width: 50%;
  }  

}

.cf:before,
.cf:after {
  content: " ";
  display: table;
}
.cf:after {
  clear: both;
}
<div class="flexo cf">
  <div>1</div>
  <div>2</div>
  <div>3</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

What is the proper way to incorporate parentheses (either "(" or ")") on the JavaScript calculator's output screen?

This is the customized button I made for the parentheses. <div class="col"> <button class="parentheses rounded-pill btn-outline-primary btn btn-lg" id="keyButton" data-key="operator"&qt;()</but ...

Using JavaScript, generate ten clickable circles on the screen. Each circle should display the number of times it has been clicked in the center when interacted with

I am currently working on my JavaScript skills and would like to implement a fun project involving 10 colorful circles. While I can easily create these circles using HTML and CSS, I require assistance with the interactive functionality using JavaScript. ...

Modifying the color of the active column

In an attempt to remove the color from the column border following a click on the image, I am struggling to locate the specific code segment that needs adjustment. I have scrolled through the entire code and experimented with color changes without succes ...

What is the best way to incorporate this PHP script into an HTML document?

Recently, I made the decision to create a booking calendar and integrate it into my website. However, I encountered an issue when trying to include it in the html file. I attempted using the <?php ?> tag, but unfortunately, it did not display as expe ...

The Challenge of a Chess Board that Adjusts to Different

Looking at the demonstration below, it's clear that the chessboard adjusts properly to a width of no more than 512px while adapting to different devices. The pieces also resize accordingly with the board. However, I'm encountering an issue when a ...

Enhance your browsing experience with Fire-fox add-on that automatically triggers JavaScript after YouTube comments have

I am currently working on creating an add-on for the Firefox browser. I have implemented a page-mod that triggers a script when specific pages (such as Youtube) are loaded, allowing it to communicate data back to the main script. However, I am encountering ...

Perform an AJAX call from the main file after inserting data using AJAX beforehand

I am in need of assistance with a question I have. On a page, an AJAX function is executed after the page finishes loading, creating a table in PHP that contains two buttons: one for changing passwords and the other for deleting. This table is then injecte ...

What is the most efficient way to send various props to multiple child components within a React parent component?

Currently, I am working on developing a color palette generator using React. However, I have encountered an issue where all child divs in the color palette receive the same color instead of different colors. Below is the code snippet: class ColorPalet ...

How to make background color transparent in CSS for Safari browser

Does anyone know how to make the background color transparent in Safari? Right now, my PNG file is showing with a white background instead of being transparent. I have attempted: background-color: transparent; background-color: rgba(255,255,255,0); appe ...

The text beside the image is not aligning correctly to the top of the page

I am having an issue on my website where the text is not aligning with the image as desired. The parent container is aligned to the top of the page, but the text seems to be out of line. I would like the text to be in line with the image, similar to a navi ...

Guide to showcasing user input using a Javascript function

I need assistance to show the user input via the submit button. Users will enter tool types and the first five inputs will be displayed in list items (li). Once the limit of 5 tools is reached, a message 'Thanks for your suggestions' should appea ...

Python allows for the sending of HTML content in the body of an email

Is there a way to show the content of an HTML file in an email body using Python, without having to manually copy and paste the HTML code into the script? ...

Having trouble retrieving the text of an element using Selenium

Looking at the DOM element provided: <span class="styles__Content-rlm06o-1 ixoRjG">20.00000000</span> I am attempting to extract the value 20.00000000 using the following Python code: text = driver.find_elements_by_xpath("//*[@c ...

Drop down menus fail to appear after the screen has been resized

Creating responsive menus involves using ordered and unordered lists, along with CSS for styling. I have added a script to dynamically generate dropdown menus, but encountered an issue where nothing appears on the screen upon resizing - only the backgrou ...

Error encountered when attempting to utilize a variable within an EJS template document

I seem to be encountering some difficulties while attempting to get an EJS template file to recognize a variable that holds the rows from an SQLite3 table query in a related .js file. Whenever I try to access that route by launching the server, I receive a ...

Tips for preventing a page from automatically scrolling to the top after submitting a form

Recently, I set up a form where users can input values. The form is set to submit either on change or when the user hits enter, depending on which value they want to update. However, after the values are submitted, the page automatically jumps back to the ...

Use JQuery to gradually decrease the opacity of divs individually

I am currently working on a function that fades out all divs except the one that has been clicked on simultaneously. However, I want them to fade out one by one instead. Additionally, I would like the divs to fade out in a random order. If anyone knows ho ...

When the position of my navbar is set to fixed, it doesn't stay attached to the top of the webpage

Currently, I am working on programming a website and I am trying to create a navigation bar that stays fixed at the top while scrolling. I have attempted to achieve this using the following CSS: h1 { Position: fixed; } However, the above code is not ...

Tips for displaying HTML elements beyond the boundaries of an iframe

Similar Inquiry: How can content from an IFRAME overflow onto the parent frame? I am in search of a potential solution for the following issue: There is a specific element with the style "position: absolute" within a page loaded into an iframe. Based ...

javascript - Alternate text colors several times within specified color ranges

Seeking assistance with changing the text color multiple times from #627CA9 to #FFFFFF and vice versa. Here's what I've tried: function changeColor(id) { var x = document.getElementById(id); if (document.getElementById(id).style.color = " ...