Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display divs in a different order depending on screen size in Bootstrap 3?

I have a Bootstrap two column layout which collapses to one on narrow screens.

Bad ASCII art:

+-------------------+-------------------+
|  Div A            |  Div B1           |
|                   +-------------------+
|                   |  Div B2           |
|                   +-------------------+
|                   |  Div B3           |
+-------------------+-------------------+

collapsing to

+-------------------|
|  Div B1           |
+-------------------+
|  Div B2           |
+-------------------+
|  Div B3           |
+-------------------+
|  Div A            |
|                   |
|                   |
|                   |
|                   |
+-------------------+

A has class col-md-6, B1-B3 are contained in a div B with class col-md-6 col-md-push-6. This works just fine, but the layout would be even nicer as

+-------------------|
|  Div B1           |
+-------------------+
|  Div A            |
|                   |
|                   |
|                   |
|                   |
+-------------------+
|  Div B2           |
+-------------------+
|  Div B3           |
+-------------------+

Is that achievable with reasonable amounts of code?

like image 600
Nils Weinander Avatar asked Jan 18 '16 21:01

Nils Weinander


People also ask

How do I change the order of divs in Bootstrap?

You can order the elements using a flex container. The outer div is set to "display: flex;" and the inside elements get given an order. By giving element B an order of 1, and element A an order 2. Element B will be placed before A even though A is first in the code.

How do I change Bootstrap 3 column order on mobile layout?

Try using col-lg-push and col-lg-pull to reorder the columns in large screens and display the sidebar on the left and main content on the right.

How do I keep my div the same size?

if you set width:500px; and height:300px; (or whatever size you want) the div should stay the same size. Also, you can set overflow:hidden; so that if whatever is in the div goes beyond those bounds, it is not shown, and does not resize the div. Save this answer. Show activity on this post.


1 Answers

It makes more sense when you design it thinking about what is going to look like on mobile first. A simple pull-right and pull-left and the right classes and architecture and you have no media hacks to use at all.

Disclaimer: Be careful, as the only downside to this is losing the tab sequence A1- B1- B2- B3 ;)

See the code

<div class="container">
    <div class="col-xs-12 col-md-6 pull-right">
         <div class="box">B1</div>
    </div>
    <div class="col-xs-12 col-md-6 pull-left">
         <div class="box a1">A1</div>
    </div>
    <div class="col-xs-12 col-md-6">
         <div class="box">B2</div>
    </div>
    <div class="col-xs-12 col-md-6">
         <div class="box">B3</div>
    </div>
</div>

CSS (this is only for demo beautification and distinction of boxes. You wont need this except the no-padding reset)

.container div{
    padding:0;
}
.box{
    background:red;
    height:40px;
    color:#fff;
    padding:10px;
    text-align:center;
    border:1px solid #111;
}
.box.a1{
    background:blue;
    height:120px;
}

See the demo

like image 141
LOTUSMS Avatar answered Nov 15 '22 03:11

LOTUSMS