Reorganizing flexbox design for optimal mobile display

I am currently working on a layout with 4 divs using flexbox for alignment. On desktop size, the first and last divs take up the entire height of the container, while the second and third divs stack vertically in the center with each taking up 50% of the height.

For mobile size, I want to rearrange the layout so that the last div stretches across the entire width at the top, the first div aligns left underneath it taking up 50% of the width and remaining height, and the second and third divs stack vertically under the top one taking up the remaining 50% of the width and splitting the height equally between them.

Despite trying to adjust the media query settings and other properties like flex-direction, I have not been successful in achieving this layout as desired.

Below is a snippet of my code:

* {
  margin: 0;
  padding: 0;
}

.box-wrapper {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  height: 70vh;
  align-items
}

.boxa {
  background: red;
  flex: 0 0 100%;
  width: 33%;
}

.boxb {
  background: orange;
}

.boxc {
  background: lightgreen;
}

.boxb,
.boxc {
  flex: 0 0 50%;
  width: 33%;
}

.boxd {
  background: grey;
  flex: 0 0 100%;
  width: 33%;
}

@media (max-width: 700px) {
  .boxa {
    order: 2;
    width: 50%;
    flex: 0 0 75%;
  }
  .boxb {
    order: 3;
    width: 50%;
    flex: 0 0 37.5%;
  }
  .boxc {
    order: 4;
    width: 50%;
    flex: 0 0 37.5%;
  }
  .boxd {
    order: 1;
    flex: 0 0 25%;
    width: 100%;
  }
}
<div class="box-wrapper">
  <div class="boxa"></div>
  <div class="boxb"></div>
  <div class="boxc"></div>
  <div class="boxd"></div>
</div>

Answer №1

If you are struggling to achieve the desired layout with flexbox, it may be because flexbox is not designed for 2-dimensional grids. While it works well for arranging items in rows or columns, it falls short when it comes to crossing both row and column lines.

In this case, CSS Grid would be a more suitable option as it allows for easy creation of complex layouts involving intersections of row and column lines.

For a demonstration, check out this jsFiddle demo.

.box-wrapper {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;  /* 3 equal width columns */
  grid-template-rows: 1fr 1fr;         /* 2 equal height rows */
  height: 70vh;
  grid-column-gap: 5px;
  grid-row-gap: 5px;
  padding: 5px;
  grid-template-areas: " first second last " 
                       " first  third last ";
}

.boxa { grid-area: first; background: red; }
.boxb { grid-area: second; background: orange; }
.boxc { grid-area: third; background: lightgreen; }
.boxd { grid-area: last; background: grey; }

@media ( max-width: 700px) {
  .box-wrapper {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-template-areas: "  last   last  "
                         " first  second " 
                         " first   third ";
  }
}

* { margin: 0; padding: 0; }

/* for placing and styling numbers only */
.box-wrapper > div {
  font-size: 1.5em; display: flex; align-items: center; justify-content: center; }
<div class="box-wrapper">
  <div class="boxa">1</div>
  <div class="boxb">2</div>
  <div class="boxc">3</div>
  <div class="boxd">4</div>
</div>

For further reading, you can visit:

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

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

Align pictures in the middle of two divisions within a segment

This is the current code: HTML: <section class="sponsorSection"> <div class="sponsorImageRow"> <div class="sponsorImageColumn"> <img src="img/kvadrat_logo.png" class="sponsorpicture1"/> </div& ...

Troublesome sizing and text wrap problem found in <ul> elements

Update: Successfully resolved the initial issue, now focusing on fixing the second part. Take a look at this demo app I am developing. I am working on adding a feature that allows users to create subtasks for each todo item, which will appear when the tas ...

Jquery functionality experiencing issues with Flash on Internet Explorer 6 and Google Chrome browsers

HTML Structure <div class="dinz-slider-film trigger-vid"> <object width="480" height="340" class="vid-flash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="FlashID" style="visibility: visible;"> <param value="sites/all/theme ...

Concealing and revealing template URLs with AngularJS

I am currently working with a dynamic Array in my Controller that contains templates (html files) structured similarly to the example below: $scope.templates = [ { name: 'template1.html', url: 'template1.html'}, { name: ...

What is the best way to evenly space out divs filled with images in a horizontal layout?

On my website, the header consists of text and a logo. Since the font used is not standard, the text is displayed as an image. I am looking to create a design in which the elements of the site adjust according to the size of the browser window. Is this wh ...

The responsive website functions flawlessly in remote testing on Chrome, but encounters issues when live

My website gallery is responsive to all sizes when viewed on chrome at 127.0.0.1, but once I upload it to the live domain and host, the gallery appears oversized on all mobile phones. Below is the code snippet along with the CSS that I am currently using. ...

Is there a way to manipulate the direction of bouncing divs using a button?

I want to design a single button that can control the bouncing motion of my div elements. The initial configuration will have the space divs arranged in a static, straight line. Once the button is clicked, the divs should start bouncing within their conta ...

When I press the button, the text box fails to refresh

Here is an example I want to share with you: code demonstration. Below is the HTML code snippet: <input id="uploadFile" placeholder="Choose File" disabled="disabled" /> <div class="file-upload btn btn-primary"> <span>Upload</span ...

Scaling background images in HTML and CSS to perfectly fit any screen size

As I am in the process of creating my portfolio website, I have encountered an issue with the background image not adjusting to the screen size. Currently, I am using HTML and CSS for the design. Can you please provide guidance on how to code the backgro ...

Utilizing CSS to set a map as the background or projecting an equirectangular map in the backdrop

How can I set an equirectangular projection like the one in this example http://bl.ocks.org/mbostock/3757119 as a background for only the chart above the X-axis? Alternatively, is it possible to use an image of a map as a CSS background instead? .grid . ...

What is the method to have the text cursor within a text field start a few pixels in?

I need a text field with the cursor starting a few pixels (let's say 4) from the left-hand side. I am aware that this can be achieved by adjusting the size of the text field using padding, but I am curious if there is a way to resize the text box with ...

PHP: Converting messy CSV data into beautifully formatted HTML tables

My client regularly sends CSV text files to my PHP application, where the elements in each row follow a consistent format but the commas that separate them vary. This inconsistency poses a challenge when trying to extract data and present it in an HTML tab ...

The functionality of Ajax loading content will fail when the link is already loaded

I am currently using an ajax script that dynamically replaces the content of #main with the contents of a loaded page based on the clicked link. For example, clicking on rddd would replace #main with the content of about.php. However, I encountered an is ...

The Verdana font in Microsoft Word appears with a distinct rendering when converted to an HTML-based

I'm currently working on generating a PDF from an HTML template. Our customer provided the template they previously used in Word, and they require the font to be Verdana. The challenge I'm facing is that Verdana looks smaller and has a different ...

The importance of having separate CSS styles for various browsers, particularly when dealing with different versions of Internet Explorer like IE6, IE7, IE8, and IE9

I have always been intrigued by the fact that default CSS does not always work across all browsers, often resulting in breaks specifically for IE browsers (6, 7, 8, 9). As a result, we typically find ourselves creating multiple CSS files tailored for dif ...

Issue with column-break-before toggle function only operational in Safari

In my website layout, I have arranged li elements in a 2 column design. I am looking to implement a specific functionality where if a div element (inside an li element) is clicked, it should add a class with the following CSS properties: -webkit-column-br ...

Struggling to make an AJAX form function properly

I'm running into an issue while setting up a basic AJAX form. My goal is to have a message display on the same page upon successful login using a PHP form through AJAX, but currently, I get redirected to the PHP file after form submission. Can anyone ...

Mobile navigation bar with Bootstrap transparency

I recently encountered a challenge with my Bootstrap navbar where I needed to remove the background. To achieve this, I applied the class "navbar-fixed-top" to the main wrapper. <nav class="navbar navbar-fixed-top"> After making this change, the na ...

Using HTML and CSS to capture user input data and store it in an array

I've created a form that allows users to add and remove multiple fields, ranging from one to three. My goal is to retrieve the form data and store it in an array. index.html <!DOCTYPE html> <html lang="en"> <head> ...

I would like to know how to create a dropdown menu that shows the country name, flag, and code based on the

I have successfully generated the telephone input field. One requirement is to display the Country flag along with the country name as soon as the dropdown is selected. Another requirement is that once the country is selected, it should display the countr ...