Convert a two-column layout on the web into a single-column layout for mobile devices, featuring dynamic

Is there a way to style this diagram with CSS that will work on IE11 and all major browsers?

It seems like Flexbox doesn't support dynamic height.

Do I need separate left and right columns for larger viewports and no columns for smaller viewports?

Check out the code on CodePen

<div class="container">
   <div id="box1" class="box">box1</div>
   <div id="box2" class="box">box2</div>
   <div id="box3" class="box">box3</div>
   <div id="box4" class="box">box4</div>
   <div id="box5" class="box">box5</div>
</div>

View image here

Answer №1

One method to handle different screen sizes is by using media queries to render different layouts based on the current screen size.

A Simple Example:

<html>

<head>
    <style>
        .container {
            width: 100%;
        }

        .row {
            display: flex;
            flex-direction: row;
            justify-content: flex-start;
            width: 100%;
        }

        .column {
            display: flex;
            flex-direction: column;
            justify-content: flex-start;
        }

        .item {
            width: 100%;
            border: solid 1px #dadada;
            border-radius: 16px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        @media screen and (max-width: 600px) {
            .desktop-only{
                display: none;
            }
            .column{
                width: 100%;
            }
        }

        @media screen and (min-width: 601px) {
            .mobile-only{
                display: none;
            }
            .column{
                width: 50%;
            }
        }

        .item-1 {
            height: 200px;
        }

        .item-2 {
            height: 400px;
        }

        .item-3 {
            height: 250px;
        }

        .item-4 {
            height: 300px;
        }

        .item-5 {
            height: 350px;
        }
    </style>
</head>

<body>
    <div class="desktop-only">
        <div class="container">
            <div class="row">
                <div class="item item-1">1</div>
            </div>
            <div class="row">
                <div class="column">
                    <div class="item item-3">3</div>
                    <div class="item item-5">5</div>
                </div>
                <div class="column">
                    <div class="item item-2">2</div>
                    <div class="item item-4">4</div>
                </div>
            </div>
        </div>
    </div>
    <div class="mobile-only">
        <div class="container">
            <div class="column">
                <div class="item item-1">1</div>
                <div class="item item-2">2</div>
                <div class="item item-3">3</div>
                <div class="item item-4">4</div>
                <div class="item item-5">5</div>
            </div>
        </div>
    </div>
</body>

</html>

By adjusting the breakpoint at 600px, you can observe the layout "changing" as you resize the window above and below that value.

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

CSS rule: The behavior of my specified property is not being implemented

I have inserted an image directly into my HTML code, which serves as a small profile picture. I am attempting to position this image in the top right corner of my website, but no matter what CSS property I apply, the image refuses to budge. body { fon ...

Upon inputting the text value, the concealed button will automatically appear without the need to press any buttons

I would like to create something similar. The hidden button should appear after entering the text value without the need to press any buttons. For example, if a user enters Hazrat Shahjalal International Airport, Dhaka (DAC), the button will become visibl ...

CSS switch status toggle

Here's my code for a toggle switch from . I'm trying to change the label before the switch/checkbox to display "checked" or "not checked" based on the toggle state. When I click on the label, it changes the switch but not the text. JavaScript: ...

Discovering nested documents in MongoDB using JavaScript

How to find nested data in MongoDB with the following collection: { "_id": { "$oid": "585b998297f53460d5f760e6" }, "newspaper": { "playerID": "57bffe76b6a70d6e2a3855b7", "playerUsername": "dennis", "player_newspaper": "{\"ID\":\" ...

What steps should I take to resolve this unexpected issue with svelte?

Whenever I attempt to execute the application, an error is consistently displayed to me. Here is a screenshot of the error: https://i.sstatic.net/jfo3X.png This issue always arises on the initial import type line, regardless of the content or arrangement ...

Jquery validation is ineffective when it fails to validate

I have implemented real-time jQuery validation for names. It functions correctly in real-time, however, when I attempt to submit the form, it still submits the value even after displaying an error message. Below is the code snippet for the validation: $ ...

Utilize JavaScript to compute and implement a deeper shade of background color

To dynamically apply darker shades of background using JavaScript, I have devised the following code. .event-list .bg{ background:#eee; padding:5px; } .grid .event-list:first-child .bg{ background: #2aac97 } .grid .event-list:nth-child(2) .bg{ backgrou ...

Discovering child elements within an iframe using Angular and customizing their appearance

Is there a simple and effective way to locate child nodes within an iframe using Angular in order to access the HTML element? Currently, I have implemented the following method: I designated a reference variable within my iframe (#privacyPolicy) <ifra ...

Loading a Component in React (Next.JS) Once the Page is Fully Loaded

After implementing the react-owl-carousel package, I encountered an error upon refreshing the page stating that "window is not defined." This issue arises because the module attempts to access the window object before the page has fully loaded. I attempted ...

The useState setFunction does not alter on OnClick events

My useState element is causing issues because the function doesn't get called when I click on it. I've tried multiple solutions and debugging methods, but it seems like the Click event isn't being triggered no matter what I do. const [moda ...

Unable to bring in the Firebase Firestore Colletion

When working on my next app, I encountered an error while trying to call a Firestore Collection. The specific error message that appeared when I ran the app was: FirebaseError: Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicat ...

Employing CSS selectors to target the subsequent element that is accessible

Here is the structure of my HTML: <input type = "checkbox" style = "display:none" id = "select"> <label for = "select" id = 'click'> click </label> <div class = 'next'> </div> I am trying to style t ...

What is Microsoft's equivalent to an embedded Java web applet?

Is there a Microsoft alternative to embedded Java web applets? Can you provide an example from the internet? Furthermore, why is ajax prevalent in dynamic web development compared to embedded applications? ...

Unforeseen issues arise when working with CSS selectors

Exploring CSS with a series of buttons and encountering some unusual behavior. Here's the code: https://codepen.io/buoyantair/pen/JNgqXG?editors=1100 Let me share the snippets for Pug Script and Sass file: Pug Script header.row .col ...

What is the best way to adjust the height of a dropdown box in an angular application?

Is there a way to change the height of the scroll view? It seems too long for my liking, any advice? I attempted to adjust it using css but unfortunately, it didn't work out. The scroll view appears excessively lengthy as shown in the image below. ...

Dynamic HTML5 elements in constant motion

I'm interested in creating an HTML5 canvas that features bouncing circle and square elements that interact with each other, potentially spinning upon collision. The best example I've come across so far is located at this link. var canvas = docu ...

Node.js used to create animated gif with unique background image

As I navigate my way through the world of node.js and tools like express and canvas, I acknowledge that there might be errors in my code and it may take me some time to grasp certain concepts or solutions. My current project involves customizing a variati ...

Retrieving the custom attribute value from the chosen option and storing it in a JavaScript variable

I am currently working on a project that involves a country select input, with custom attributes included in the options. My goal is to retrieve the value of the "name" attribute using JavaScript and then append it to a link for further processing. Howev ...

Including a Pinterest image hover widget following the completion of an Ajax load

I have implemented the Pinterest image hover widget on my website to allow users to easily pin images to their Pinterest accounts. You can find the widget here. (Make sure to click the image hover radio button under button type to see the one I am using.) ...

Use the same CSS style for various attribute selectors

Is there a way to simultaneously apply the following style to a type="submit" without having to repeat the entire block of code? input[type="button"] { width: 120px; margin-left: 35px; display: block; } ...