Discover the ultimate Flexbox solution for creating responsive layouts like never before!

I am working on achieving three different layouts in a responsive manner.

1st layout - default design

https://i.sstatic.net/4M0AI.png

2nd Layout - with "min-width:600px"

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

3rd Layout - with "min-width:700px"

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

I have successfully achieved the above three layouts. However, I have concerns about the fixed height of "200px" for the dark-blue and red colored divs. If the content increases later on, the layout might not adjust properly.

I am looking for a better way to handle this dynamically so that my design remains intact even if the content increases unexpectedly. I have tried to brainstorm ideas but haven't found a suitable solution yet.

Just wanted to ask this question for educational purposes.

I aim to implement this using flexbox, but open to suggestions for other approaches as well.

If anyone could guide me in the right direction, it would be greatly appreciated.

Here is my HTML and CSS code:

/* default color of background */
.red {
  background: red;
}
.dark-blue {
  background: darkblue;
}
.light-blue {
  background: lightblue;
}
.green {
  background: green;
}

/* Container properties */
.container {
  /* mentioned 100% so that element inside takes full width i.e with id = container2 */
  width: 100%;

  display: flex;
  flex-wrap: wrap;
}

.box {
  width: 100%;
  height: 100px;
}

/* Responsive design proprties */
@media screen and (min-width: 600px) {
  .dark-blue {
    width: 50%;
    height: 200px;
  }
  #container2 {
    width: 50%;
    height: 50%;
  }
}

@media screen and (min-width: 700px) {
  .dark-blue,
  .red {
    height: 200px;
  }

  .dark-blue {
    width: 25%;
    order: 2;
  }
  #container2 {
    width: 50%;
    flex-wrap: wrap;
  }
  .red {
    width: 25%;
    order: -1;
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Layout shifter</title>
    <link rel="stylesheet" href="./style.css">
</head>

<body>
    <div class="container">
        <div class="box dark-blue"></div>
        <div class="container" id="container2">
            <div class="box light-blue"></div>
            <div class="box green"></div>
        </div>
        <div class="box red"></div>
    </div>
</body>

</html>

Answer №1

Flexbox comes with its own set of advantages and disadvantages. One drawback is that it can only effectively control either height or width, but not both at the same time.

If you require simultaneous control over both height and width, CSS-Grid is the recommended solution.

Utilize grid-template-areas to arrange the div elements. This property serves as the counterpart to flex order.

body {
  margin: 0;
  width: 100vw;
  min-height: 100vh;
  box-sizing: border-box;
  display: grid;
}

.dark-blue {
  grid-area: dark-blue;
  background-color: darkblue;
}

.light-blue {
  grid-area: light-blue;
  background-color: lightblue;
}

.green {
  grid-area: green;
  background-color: green;
}

.red {
  grid-area: red;
  background-color: red;
}

@media only screen
  and (max-width: 600px) {
    body {
      grid-template-areas:
        "dark-blue"
        "light-blue"
        "green"
        "red"
    }
}

@media only screen
  and (max-width: 600px) {
    body {
      grid-template-areas:
        "dark-blue"
        "light-blue"
        "green"
        "red";
    }
}

@media only screen
  and (min-width: 601px)
  and (max-width: 700px) {
    body {
      grid-template-areas:
        "dark-blue light-blue"
        "dark-blue green"
        "red red";
    }
}

@media only screen
  and (min-width: 701px) {
    body {
      grid-template-areas:
        "red light-blue light-blue dark-blue"
        "red green green dark-blue"
    }
}
<div class="dark-blue"></div>
<div class="light-blue"></div>
<div class="green"></div>
<div class="red"></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

A guide on dynamically altering text upon hovering over an element using Vue.js

On my website, I have the following HTML code: <div class="greetings"> <img @mouseover="hover='A'" @mouseleave="hover=''" src="a.png" alt="A" /> <img @mouseover="hover='B'" @mouseleave="hover=''" ...

Creating a blank div in bootstrap without any content inside and filling it with a background color

Is it possible to display 3 lines inline side by side in bootstrap? I've encountered a challenge with the height and width! While using pure css works fine, integrating bootstrap seems to be causing issues. Below is an example of the code using CSS f ...

PhoneGap switches up the error type with each consecutive run

Why does PhoneGap change errors after every time it is compiled? Sometimes it runs without any issues, but then the same code throws strange errors like parse error or function not found, even though no changes were made to the code. Here is the code that ...

Two child DIVs within a single parent DIV

I am struggling to keep two divs side-by-side within a parent div. Initially, I was able to position the image in the right div successfully, but when resizing the elements, everything became disorganized and I can't seem to fix it. As a beginner, I w ...

Interactive HTML5 Web Application File Selector

After conducting thorough research, it is important to note that I am well-versed in the security implications of this client-side application which utilizes JavaScript, Vue, and pure HTML5. To initiate the file dialog, I employ a hidden input type file. ...

Customizing the background color in TurnJSIncorporating

Recently, I encountered a problem that has left me puzzled. Despite searching online for help, I have not been able to find a solution. The issue lies with the basic code provided on the official page of the TurnJS script. <div id="flipbook"> <di ...

Tips for ensuring images are mobile-friendly in terms of screen size adaptation

I have implemented a carousel feature for my app by referring to the example provided at https://www.w3schools.com/bootstrap/bootstrap_carousel.asp. To ensure that the carousel displays the example images perfectly on a mobile screen without requiring user ...

Loading a div class dynamically with JQuery

It's clear to me there is an issue with my code, and I'm reaching out for assistance. Here is the problematic snippet that needs fixing. Currently, I'm encountering this error message: Uncaught TypeError: Cannot read property 'find&apos ...

What is the best way to align this horizontal list in the middle of a fixed div?

I have a UL element that contains 4 icons within a div using absolute positioning to align it to the bottom of the screen. This div is located within a fixed sidebar. To create a horizontal list, I applied display: inline to the list items. You can see t ...

Detecting collisions with spheres using Three.js Raycaster

I came across an example from a previously answered question on this site: Three Js Object3D Button Group Detect Single Object Click While Mouse Movement Causes Object3D Button Group Zoomi As I tried to customize it to fit my needs, I encountered a few is ...

The TD border is slicing through the div and is placed on the table in IE standards, causing it

Below is the HTML code where the div is being cut by the table border when the page is in standards mode. Interestingly, if I remove the <!DOCTYPE html>, then it works fine as expected. The issue of the outside div not behaving properly on the table ...

Simply drag your files onto the form for quick and easy uploading upon submission

Would like to know how to enable drag-and-drop file selection for uploading files synchronously upon form submission. Understanding the basics of file uploading using HTML and PHP, I am seeking a way to process these files in PHP along with those in the $_ ...

What is the best way to position divs both vertically and horizontally at the center of a container

Here is the HTML markup I'm working with: <div class="wrapper"> <div class="block new-block"></div> <div class="block used-block"></div> <div class="block service-block"></div> <div class="block ce ...

Rearranging the @use directive in Sass

Here are some style snippets: tokens.scss .text-center { text-align:center; } .bold { font-weight: bold; } @mixin text-style-1 { font-size: 1.2rem; font-weight: bold; } component/card.scss @use "../token.scss" as tokens; .card { @i ...

Creating a striking Materialize Jumbotron: A step-by-step guide

Recently, I came across a sign up page that caught my eye. You can view it here. Upon inspecting the source code, I discovered that it was built using angular material design. This led me to wonder how I could achieve a similar look using Materialize.css ...

Exploring the reasons for utilizing class and ID selectors within a <div> element to enclose a map in OpenLayers

The 'quickstart' guide provides a comprehensive overview of using id and class css selectors within the html code. In order to link the map object to a specific div element, a target is required as an argument for the map object. This target val ...

Verify if a parent element contains a specific class, and if it does, then add a custom style to that

I am currently working on a challenge where I need to hover over an element, check if its class contains a specific prefix, and then apply some styles to that particular element. The issue I'm facing is that when the element has a parent div with a pr ...

Struggling to make jQuery apply .css("display", "block") upon clicking a link

I am having trouble with getting jQuery to display CSS properties when a link is clicked on. After testing the CSS properties, I found that setting the display to "block" in the style sheet works as expected. However, if I set the initial CSS display to " ...

Adjusting the page view when a collapse is opened

I am working on a website that features 3 images, each of which opens a collapse below with an embedded iframe website inside when clicked. I am trying to implement a function where the site automatically scrolls down to the opened iframe when an image is ...

Encase children within a view component in React Native

I've been working on the following code: renderActionButtons(actionButtons){ let actionButtonsContent = actionButtons.map((button, i) => { return <TouchableHighlight key={i} onPress={() => {this.updateConversation(button);}} style= ...