Get your columns in order with Bootstrap4

I need to rearrange my 3 columns on desktop and mobile devices in different ways.

Currently, my grid structure is set up like this:

<div class="row">
  <div class="col-xs-3 col-md-6">
    1
  </div>
  <div class="col-xs-3 col-md-6">
    2
  </div>
  <div class="col-xs-6 col-md-12">
    3
  </div>
</div>

For the mobile view, I want them displayed in the following order:

1-3-2

However, I'm having trouble figuring out how to achieve this using the Bootstrap 4 .col-md-push-* and .col-md-pull-* classes.

Answer №1

Revamping in Bootstrap 5 for 2021

The new responsive ordering classes have been introduced as order-first, order-last, and from order-0 to order-5

Check out the Demo here

Back in 2018 with Bootstrap 4

The responsive ordering classes were order-first, order-last and ranged from order-0 to order-12

The **push** and **pull** classes in Bootstrap 4 are now labelled as `push-{viewport}-{units}` and `pull-{viewport}-{units}`, without the `xs-` prefix. To achieve a 1-3-2 layout on mobile/xs, utilize this [demo for Bootstrap 4 push and pull](http://www.codeply.com/go/OmrcmepbUp) (Note: this functionality is pre-4.0 beta only)

Bootstrap 4.1+

With Bootstrap 4 employing flexbox, altering the column order is straightforward. Columns can be ordered from order-1 to order-12, responsively as demonstrated in order-md-12 order-2 (appearing last on md, and second on xs) in relation to the parent .row.

<div class="container">
    <div class="row">
        <div class="col-3 col-md-6">
            <div class="card card-body">1</div>
        </div>
        <div class="col-6 col-md-12 order-2 order-md-12">
            <div class="card card-body">3</div>
        </div>
        <div class="col-3 col-md-6 order-3">
            <div class="card card-body">2</div>
        </div>
    </div>
</div>

See the live Demo: Implementing order-* classes to reorder

Desktop view (larger screens): https://i.sstatic.net/M53x9.png

Mobile view (smaller screens): https://i.sstatic.net/q0A5m.png

You can also adjust column order using the flexbox direction utilities...

<div class="container">
    <div class="row flex-column-reverse flex-md-row">
        <div class="col-md-8">
            2
        </div>
        <div class="col-md-4">
            1st on mobile
        </div>
    </div>
</div>

View the Demo: Flexbox Direction for Ordering in Bootstrap 4.1


Demos of older versions
demo - alpha 6
demo - beta (3)

Explore more Bootstrap 4.1+ ordering demos


Related
Column ordering in Bootstrap 4 with push/pull and col-md-12
Bootstrap 4 change order of columns

A-C-B A-B-C

Answer №2

One way to achieve this effect is by using the CSS "Order" property along with a media query.

Here's an example of how you can do it:

@media only screen and (max-width: 768px) {
    #first {
        order: 2;
    }
    #second {
        order: 4;
    }
    #third {
        order: 1;
    }
    #fourth {
        order: 3;
    }
}

You can view this code in action on CodePen: https://codepen.io/preston206/pen/EwrXqm

Answer №3

This method can also be effective:

<div class="wrapper">
            <div class="block">
                <div class="item-1">
                    A
                </div>
                <div class="item-2">
                    B
                </div>
                <div class="item-3">
                    C
                </div>
            </div>
          </div>

Answer №4

When working with Bootstrap 3, you may be wondering if there is a simpler way in Bootstrap 4 to achieve the same result. Nevertheless, the following CSS code should be effective for your needs:

.pull-right-xs {
    float: right;
}

@media (min-width: 768px) {
   .pull-right-xs {
      float: left;
   }
}

Additionally, remember to apply the class to the second column as shown below:

<div class="row">
    <div class="col-xs-3 col-md-6">
        1
    </div>
    <div class="col-xs-3 col-md-6 pull-right-xs">
        2
    </div>
    <div class="col-xs-6 col-md-12">
        3
    </div>
</div>

UPDATE:

Upon further inspection, it appears that what was previously described is essentially the same as the pull-xs-right class in Bootstrap 4. Simply apply this class to the second column for optimal results.

Answer №5

According to the revisited answer above, column-ordering does not function correctly in Bootstrap 4 beta. The solution is to follow the codeply Flexbox order demo - alpha/beta links provided in the answer. Below is an example:

<div class="container">
<div class="row">
    <div class="col-3 col-md-6">
        <div class="card card-block">1</div>
    </div>
    <div class="col-6 col-md-12  flex-md-last">
        <div class="card card-block">3</div>
    </div>
    <div class="col-3 col-md-6 ">
        <div class="card card-block">2</div>
    </div>
</div>

It should be noted that using the "Flexbox order demo - beta" may lead to compatibility issues with the alpha codebase. Copying and pasting the code directly outside of codeply should resolve any display issues in a single column layout.

Answer №6

An effective strategy is to create two separate containers - one designed for mobile orders and hidden on desktop screens, and the other tailored for desktop orders and hidden on mobile screens.

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

Issues with IE9's CSS hover functionality are causing problems

This particular css style functions well on most browsers, but seems to have compatibility issues with Explorer 9 specifically when it comes to the :hover effect. There are instances where it works perfectly fine and other times when it doesn't work a ...

Adding a CSS style to specific sections of a string that is quoted in a Razor ASP.NET file

Is it possible to format a specific part of a string? I'm trying to only stylize the word within quotation marks. This is my cshtml code: { <div class="h-44 overflow-auto text-left mx-4 mb-4"> <p ...

How can I center two divs within a wrapper div without also centering all the text inside?

Is there a method other than using display: inline-block and text-align:center that allows us to center two divs inside a wrapper div? I prefer not to have my text centered. ...

Tips for avoiding anchor tag text from being split across two lines when resizing

One issue I am facing is with an anchor tag that displays the name of the logged-in person. The problem arises when resizing the browser window, causing the name to split across two lines. Is there a way to prevent this from happening? ...

Manipulate inherited CSS styles from an external source

Currently, I am working on creating a pagination using the NG-Bootstrap pagination component. However, I encountered an issue where I need to modify or specifically remove some CSS properties that are applied by default in the NG-Bootstrap library. Is ther ...

Styling with Chemistry: CSS for Chemical Formulas

Is there a way to write multiple formulas in CSS, or is there an alternative method I should consider? I want to integrate these formulas on my quiz website for students to use. I came across some intriguing examples that could be useful: However, I am s ...

Is it possible to create an index.html page with all the necessary head tags to prevent duplicate code across multiple html pages?

Imagine I am using app.js or a Bootstrap CDN link for all of my HTML pages, but I don't want to include it in every single page individually. Is there a way to create an index.html file that includes this so that all other HTML pages load the head fro ...

Can a custom CSS be linked directly to the angular-cli.json file?

I'm trying to incorporate custom CSS into my angular-cli.json file, but I haven't been able to find a successful method. I attempted to use the following code: "styles": [ "styles.css", "http://site.test/mycss.css" ], However, it does ...

Achieving Vertical Content Alignment in Bootstrap 4

I am struggling to achieve a responsive design where two columns stack vertically on top of each other when viewed on smaller mobile screens. Despite numerous attempts, I have been unable to figure it out. Ideally, I want the layout to look like this imag ...

Issue with Refreshing Header Row Filter Control in Bootstrap Table

Currently in the process of developing an application that utilizes Bootstrap Table and its extension, Filter Control. One feature I've incorporated is individual search fields for each column to enhance user experience. The challenge I'm facing ...

What is the best way to center a Checkbox in HTML5 and CSS?

I've been struggling to center my Checkbox with label on my current website project. Despite searching online for solutions, I'm still unable to find the right method! If you're curious, you can check out the source code of my webpage at ( ...

The bottom of the page displays varying outcomes between Code Snippet (CodePen) and Angular

I had the idea of having my footer cover the entire length of the page and stick to the bottom, adjusting itself if more content is added. However, I encountered an issue where on Codepen everything works perfectly, but on Angular (14), the footer does not ...

The display of data attributes is not being rendered correctly

Check out the fiddle I'm currently working on: http://jsfiddle.net/7Z8wY/9/ The HTML section looks like this: <div class="container"> <div class="right"> <div id="cityList" class="inner-table"></div> </div> ...

Delete the placeholder image from a div once live content has been inserted into it

If I have a container div that displays an image when empty, but I want to remove the image when content is dynamically added to the container, what would be the most effective way to do this with jQuery? The usual method of checking if the container' ...

My draggable item seems stuck in place. Any ideas on how to get it moving again?

I have been trying to implement some code I found on this link. Despite adding all the necessary file links, the code is not functioning as expected. It should be able to move within the bounds of a container, but it's not working properly. var max ...

The form-group nested within a form-horizontal is exceeding the boundaries of the preceding form-group

I am currently utilizing Bootstrap 3.3.6 and have a basic form enclosed in a panel: https://i.stack.imgur.com/UOFI6.png Although it is quite simple, the alignment of the "Post" button is not displaying properly. Here is the HTML code for the form: < ...

Guide on displaying gallery images when clicking on a specific class

I am in the process of developing a gallery tab for a website, initially displaying three thumbnails. The goal is to reveal the remaining thumbnails when a class named show-all is clicked. I have successfully animated the gallery's height to show the ...

Encountered an error while using NPM Bootstrap Carousel: Uncaught TypeError - Super expression must be either null or a function

Currently using the NPM build of Bootstrap (with Gulp) for a custom WordPress theme, and I seem to be facing some issues. While I am new to Bootstrap, I have previous experience with WordPress and Gulp (primarily with Foundation). Below is how I am includ ...

Move to the right with floating using Angular Material

I am dealing with a situation in which I have an md-card-content element. Inside it, there is a div and another md-card-content element. The challenge I am facing is that I want to move the contents of the inner md-card-content to the right. I tried usin ...

Make the div fill the entire width and height of its parent element

Here is the code I am working with: <div> <div class="inner"></div <img src="blabla" /> </div> My goal is to make the .inner div 100% width and 100% height of its parent div, which is dependent on the dimensions of th ...