What is the best way to arrange divs in varying order across different breakpoints?

I am currently utilizing Bootstrap and aiming to achieve a specific layout as shown below:

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

The numbers are indicating the desired order of the divs in a single column on mobile devices. The HTML code I attempted is as follows:

<div class="col-xs-12">
<div class="flex-row">
    <div class="col-md-6">
        <div class="order-1">
            <!-- TITLE SUBTITLE -->
        </div>
        <div class="order-md-3">
            <!-- FILM META AND CONTENT -->
        </div>
    </div>

    <div class="col-md-6">
        <div class="order-md-2">
            <!-- FILM POSTER -->
        </div>
        <div class="order-md-4">
            <!-- UNDER POSTER. -->
        </div>
    </div>
</div>

Could someone please point out any mistakes or issues with my current setup?

Answer №1

Here are three different approaches you can take:

  1. Replicate your HTML elements and toggle their visibility accordingly
  2. Arrange items as flex columns and set a fixed height to wrap them
  3. Utilize column-count

Approach 1: Replication of Elements

The simplest solution involves duplicating items #2 and #4 in the left column for mobile display. Hide the right column by adding the class d-none. On larger screens, hide the replicas with d-md-none but show the right column with d-md-block.

HTML

<div class="container">
    <div class="row">
        ...
    </div>
</div>

Demo

View Demo

Approach 2: Flex Column Setup for Wrapping

If items are not in the same row or column, using order won't work. Instead, set the container to display as d-md-flex and

flex-md-column</code on larger screens, ensuring wrapping with <code>flex-md-wrap
.

HTML

<div class="container d-md-flex flex-md-column flex-md-wrap">
    ...
</div>

CSS

@media (min-width: 768px) {
    .container {
        height: 40rem;
    }
    
    ...
}

DEMO

View Demo

Approach 3: Utilizing Column-Count

Employ CSS3 column-count for a clean layout solution where item order cannot be changed.

HTML

<div class="container">
    ...
</div>

CSS

.card {
    text-align: center;
    margin-bottom: 1rem;
}

.container {
    display: flex;
    flex-direction: column;
}

...

@media (min-width: 768px) {
    ...
}

DEMO

View Demo

Answer №2

The easiest solution (without any additional CSS or repeated markup) is to utilize "floats" within Bootstrap 4. By utilizing the d-md-block class to remove flexbox on larger screens, and then incorporating float-* for column positioning, you can achieve the desired layout. This approach allows the columns to maintain their natural order on mobile devices...

<div class="row d-md-block d-flex">
    <div class="col-md-6 float-left py-2">
        <div class="border">
            1
        </div>
    </div>
    <div class="col-md-6 float-right py-2">
        <div class="border taller">
            2 FILM POSTER
        </div>
    </div>
    <div class="col-md-6 float-left py-2">
        <div class="border taller">
            3 FILM META AND CONTENT
        </div>
    </div>
    <div class="col-md-6 float-left py-2">
        <div class="border">
            4
        </div>
    </div>
</div>

Answer №3

To maintain the existing HTML structure, you have the option to utilize flex as the display property for the parent element and arrange the children accordingly by setting their order. By incorporating media queries to define specific breakpoints, adjusting the order can easily achieve the desired layout.

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

JQuery unable to recognize dynamically inserted Anchor Tag through AJAX request

I am currently working on a page that is sourcing a large amount of data from a database through various PHP files. To achieve this, I am using JQuery to identify specific events and trigger AJAX requests to the necessary PHP file to display the required c ...

The SVG image exceeds the boundaries of the parent column div in bootstrap

I am struggling to make a SVG fit inside a column in a bootstrap div with col-lg-4. I tried using img-fluid, but it did not work as expected. The SVG should automatically adjust its size to fit the parent div. However, that is not happening. .star-ratin ...

How to set a default checked value in a custom DropDownList in AngularJS?

I have created a bespoke DropDownList in my HTML code. I'm looking for guidance on how to set one of the options as default within the following scenario: <div class="dropdownlistheader" ng-click="toggle('subdiv','item')"> ...

The bootstrap 4 dropdown button fails to activate the dropdown feature

I am currently working on a Flask application with Bootstrap integration. I am facing an issue while trying to incorporate a dropdown menu, which needs jQuery + Popper elements in order to work properly. Even though the button is displayed, the dropdown fu ...

Transfer information to a different division using Ajax

I am attempting to transfer data to another div after an Ajax refresh. Each time it refreshes, new data is fetched from the database. My goal is to retain the old data displayed in the first div, store it, and then move it to the second div when Ajax refre ...

The @media print rule for Angular 16 in the style.css file is not functioning properly

I'm currently working on an Angular component called ViewTask, which has a template that is rendered inside the app component. The app component also consists of a side nav bar. My goal is to only display the content inside the 'print-section&apo ...

Modify the appearance of the element that is currently chosen

I have a navigation menu and I would like to change the style of the element that is selected from the list in the navigation menu. I am using React with the Grid element from material-ui. NavigationMenu.tsx: import React, { useState, useEffect } from "r ...

The text does not automatically move to the next line

How can I make the content in this form section wrap to the next line in a list-item and overlap the width of the div content? The second line, particularly where it mentions Security Question two https://i.sstatic.net/5CVi2.png This is my HTML & CS ...

Struggling with how to customize the input component from react-native-elements? Let

I am currently exploring how to customize the appearance of an input using react-native-elements, similar to the design depicted in the documentation... My goal is to achieve the look of the top inputs with a white background, but I am uncertain about th ...

Showcasing a div on top of a frame in html

I have a specific requirement where I need to show a div on top of a frame or frameset. Is it achievable to display a div over a frame in an HTML page? ...

Tips for fashionable dressing &

Can you provide some insights on how to format the &amp; symbol in this HTML snippet? <a>Ian</a> &amp; <a>Jim</a> ...

Tips on controlling the color of an input field in a form using React

Within my React app, there is a text input field situated inside a form that displays in its default gray color before any interaction: https://i.stack.imgur.com/FdNos.png Once the input field is clicked on, it changes to white as shown here: https://i.s ...

Populate HTML Table with Array Elements

I need assistance with inserting array values into an HTML table. The table should have 5 columns and enough rows to accommodate all the information. I envision it looking like this: 1 | 2 | 3 | 4 | 5 11 | 22 | 33 | 44 | 55 111 | 222 | 333 | 444 | 555 ...

Master the art of positioning in three.js framework

I am a beginner in javascript and three.js, and I am currently exploring how to determine the 3d positions on a webpage. For instance, I want to place a point light at the coordinates (50,20,10) in x, y, z values. How can I determine where these x, y, z va ...

The list in my "Hamburger" menu isn't loading properly

I have been attempting to add a "Hamburger" style menu to my webpage specifically for the "md" size. Although the hamburger menu displays, clicking on it does not trigger any action. I experimented with removing some classes from certain tags, but most of ...

Sending jQuery variables to PHP

Is it possible to transfer a jQuery variable to a php file? Let's take a look at the code in my_js.js: function updates() { $.getJSON("php/fetch.php", function(data) { $.each(data.result, function(){ var s_id = this[& ...

Using Javascript to dynamically retrieve accordion content from a dynamically generated ID

I am currently working on developing a more intricate accordion feature. The accordion will consist of 4 buttons (section 0-3) with unique content (lorem ipsum 0-3). Clicking on "section 0" should reveal "lorem ipsum 0", while clicking on "section 1" shoul ...

Animation that responds to scrolling movements

Is there a way to animate text based on scrolling? <p class="class">TEXT</p> transform:translateX(-500px);opacity:0; transform:translateX(0px);opacity:1; ...

The form embedded within the <tr> element seems to be malfunctioning

Hey there, I'm facing a little issue with my form that is nested inside a table content. It seems to not be working properly. Let me share the code snippet below: <table> <form name='editpo' method=POST action='js_buat_po.php? ...

Tips for getting the sidebar to move down on mobile devices

When viewing my website on a mobile device, the sidebar appears above my products. I would like it to be positioned below my products. The sidebar is currently on the left side with content on the right. Is there a more effective way to create a sidebar ...