Ways to organize two columns of flexible items on a responsive display

Can someone assist me with CSS flexbox for responsive screens?

Below is the layout of the flex container and items on a desktop screen: https://i.sstatic.net/AXBHw.png

The container has flex and flex-wrap properties applied, with each column having flex: 1 1 auto;.

For mobile screens, I want it to look like this: https://i.sstatic.net/SAVlJ.png

I used flex: 0 1 50% to show 2 columns in one row, but I have two specific requirements:

**1. (required) Keep the orange items next to each other regardless of screen size.** 2. (optional) Allow the possibility of displaying 2 columns in 1 cell (like the green and purple items in a single cell).

If anything is unclear or if you need more information, please feel free to ask me to clarify.

Answer №1

To maintain the desired layout, combining the green and purple elements into one div may be the best solution. From there, adjusting the flex-basis and using order to reposition as necessary will achieve the desired result.

.flex {
    display: flex;
}
.flex > div {
    flex: 1 1 calc(100% / 7);
}
.flex > .d2-wrap {
    flex-basis: calc(100% / 7 * 2);
    display: flex;
}
.flex > .d2-wrap > div {
    flex: 1 1 auto;
}

@media screen and (max-width: 600px) {
    .flex, .flex > .d2-wrap {
        flex-wrap: wrap;
    }
    .flex > div, .flex > .d2-wrap {
        flex: 1 1 50%;
    }
    .flex > .d2-wrap > div {
        flex-basis: 100%;
    }
    .d4 {order: 1;}
    .d5 {order: 2;}
    .d1 {order: 3;}
    .d2-wrap {order: 4;}
    .d6 {order: 5;}
    .d7 {order: 6;}
}

.d1 {background: pink; height: 150px;}
.d2 {background: red; height: 100px;}
.d3 {background: green; height: 50px;}
.d4 {background: purple; height: 150px;}
.d5 {background: lightblue; height: 150px;}
.d6 {background: orange; height: 150px;}
.d7 {background: darkorange; height: 150px;}
<div class="flex">
    <div class="d1"></div>
    <div class="d2-wrap">
        <div class="d2"></div>
        <div class="d3"></div>
    </div>
    <div class="d4"></div>
    <div class="d5"></div>
    <div class="d6"></div>
    <div class="d7"></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 could be causing my text input style to not update after re-rendering?

When I focus on my text input, I wanted to apply special styles but my initial approach using the focused state didn't work as expected. import React from "react"; import { useState } from "react"; import { Text, View, TextInput } ...

Tips on personalizing the DateTimePicker component from Material-UI

I am currently working on customizing the DateTimePicker component provided by Material-UI. To access its documentation, please visit: One challenge I have encountered is the lack of styling information in the documentation. My goal is to change the main ...

Mastering the correct application of template language in CSS files

I am trying to serve a CSS file through my url.py file in order to utilize template tags in my CSS. Below is the code I am using: Base.html: <head> ... <link rel="stylesheet" type="text/css" href="/site_media/css/wideTitle.css" /> urls.py: ...

This error is thrown when trying to access the property 'message' of an undefined value

I'm currently working on adding an AJAX contact form to my website. However, I've run into a problem where when I try to submit the form, I encounter the following error in Google Chrome: "Uncaught TypeError: Cannot read property 'message&a ...

Sending data to another page by selecting a list item

Being new to web programming and JavaScript, I am facing a scenario where I have a list of requests displayed on one page and I need to pass the ID of the selected request to another page to display all the details of that particular request. I am strugg ...

The perplexing tale of Storybook, Ionic React, and styled-components CSS sequencing puzzles

I am facing an issue with my custom components in Ionic, where the styles overridden by Ionic's standard components. The component looks fine on the app page but gets messed up when used in Storybook. After some investigation, I realized that the orde ...

Displaying a sneak peek of the information along with a "See More" button beneath the title in React

My goal is to create a section header followed by preview content that defaults to showing only the first few lines. The text would then fade out, with an option to expand on click labeled "Show More." This functionality is similar to how Reddit displays p ...

Swapping out a <DIV> element following a specified duration

Looking to swap out DIV-A with DIV-B on a landing page after 10 seconds. After some research, it seems like JQUERY is the best option, but I'm unsure about how to proceed. Most solutions involve cycling through DIVs or replacing them on a button clic ...

The webpack command in the terminal failed to locate the file /config/webpack/development.js

Whenever I type $ webpack in my terminal, I encounter the following error: webpack config /Users/kristenmkulha/Desktop/react-help-queue/config/webpack/development.js not found, please run 'bundle exec rails webpacker:install' to install ...

Tips for displaying a pop-up when pressing a link:

I've spent a considerable amount of time on this project without making much progress. However, I came across a tutorial with a beautiful menu design that I decided to replicate. Here is the code: HTML: <!DOCTYPE html> <html> <head> ...

Retrieving the value of a TextField from Material-UI upon submission

Is it possible to capture the value from a TextField input and display a message conditionally using onSubmit instead of onChange? I have tried implementing this functionality dynamically with onChange, but now I would like to achieve the same result wit ...

CSS :hover problem, text appearing unexpectedly

I'm currently working on designing a logo that displays hidden text when hovered over, providing instructions to users. However, I've encountered an issue where the hidden text reveals itself even before the logo is hovered over. This is frustrat ...

What is the most effective method for retrieving data from an API and integrating it into a Redux container using Reselect within a ReactJS application

Although this question may seem familiar, I have yet to come across a satisfactory solution, especially when combined with reselect. Take a look at the following Redux container component: export default connect((state, props) => { return { ...p ...

What is the best way to create a layout for Flexbox project boxes with three boxes per row?

My project cards are displaying in a single horizontal line and expanding infinitely to the right, rather than appearing in rows of three as intended. This causes them to extend outside the visible area in a long horizontal strip. See the issue in action ...

Unable to switch between navigation items and icons

Currently, I am in the process of learning vanilla JavaScript and I'm trying to incorporate a navigation feature similar to when the hamburger icon is clicked. However, I have encountered an issue where I cannot toggle the hamburger icon to change it ...

How to align a child element in the center when the parent is not centered or taking up the full width

I am currently working on building a layout with Bootstrap 4.6, and I am facing an issue with centering the header, columns/cards, and buttons on the page width while the parent container is left-aligned and not full width. Additionally, I need help with ...

Updating field values in Graphcool mutations that are not intended to be changed

I encountered an issue with mutation while using Graphcool and Apollo Client in my application. The problem arises when implementing a soft delete functionality, where the user triggers a delete action and the mutation changes only one field in the databas ...

Discovering text on a webpage and extracting its XPath or CSS using Selenium and Java

Imagine a scenario where there is a page displaying various items on a table that changes in both order and quantity randomly. The task at hand is to locate a specific item labeled "Itemx" by its text, extract its XPath or CSS, and then use it in another ...

Encountering an ENOENT error in the CodeSandbox CI environment, whereas this issue does not arise in GitHub

I am currently in the process of creating a GitHub pull request for the react-querybuilder library. However, I am encountering an issue with my CodeSandbox CI job, which is failing and displaying the following error message: { [Error: ENOENT: no such file ...

Error parsing module: Encountered unexpected token at line 7855, character 112

Currently, I am in the process of constructing a frontend application using npm run build via Jenkins. Unfortunately, I have encountered an error message as shown below. Can you provide guidance on how to resolve this issue? Creating an optimized productio ...