Reordering divs responsively with Twitter Bootstrap?

Reordering divs responsively with Twitter Bootstrap?

I am working with Twitter Bootstrap v3 and want to reorder elements responsively, using column ordering and offsetting.
This is how I would like to use my elements. At -xs or -sm breakpoints, with the containing div stacked 4 to a row:

At -md or -lg breakpoints, with the containing div stacked 2 to a row:

This is the current code – I know how to set the classes on the containing div, but not on A, B and C:

Content of A
Content of B
Content of C

… more containing divs…

I can’t figure out how to get A, B and C in the right order with Bootstrap 3’s column ordering and offsetting. Is it actually possible?
This is as far as I’ve got using JSFiddle: http://jsfiddle.net/3vYgR/

Solutions/Answers:

Solution 1:

I think you want to look at the push and pull classes…

<div class="row">
   <div class="a col-xs-12 col-sm-12 col-md-6 col-lg-6 col-lg-push-6 col-md-push-6">Content of A</div>
   <div class="b col-xs-12 col-sm-12 col-md-6 col-lg-6 col-lg-pull-6 col-md-pull-6">Content of B</div>
   <div class="c col-xs-12 col-sm-12 col-md-6 col-lg-6 col-lg-push-6 col-md-push-6">Content of C</div>
</div>

Demo: http://bootply.com/77853

Related:  html5 search input does not work with bootstrap

Solution 2:

Any reason to not use more traditional methods on this, ie. floating B left and the other elements to the right?

Like if you added this to current CSS in your jsfiddle:

.b {
    float: left;
    width: 50%;
    height: 100px;
}
.a, .c {
    float: right;
    width: 50%;   
    height: 50px;
}

See http://jsfiddle.net/E6BFk/

I’m not sure that Bootstrap’s column ordering could accomplish the stacking effect, perhaps only the re-ordering… Though the documentation is pretty slim on that feature.

Solution 3:

I tried the push/pull as shown in the answer by Skelly but C ends up in a new row once heights are applied: http://www.bootply.com/Q3djHYawgb#

If B can be shown first when shown in small size, then I think it makes more sense to create a row inside the right hand side column to contain A and C:

<div class="containing row">
    <div class="b col-xs-12 col-md-6">Content of B</div>
    <div class="ac col-xs-12 col-md-6">
        <div class="row">
            <div class="a col-xs-12">Content of A</div>
        </div>
        <div class="row">
            <div class="c col-xs-12">Content of C</div>
        </div>
    </div>
</div>

See http://jsfiddle.net/hoodoo99/L2BE9/

Related:  Bootstrap export options works for 5,000 rows and failed for 16,000 rows with network failure

Solution 4:

The answer provided by @skelly is great starting point, but does cause some vertical alignment issues. Imagine A is a tall div, then C is vertically aligned below A.:

[A] [B]
[ ]
    [C]

Also, If you want to take this a step further you may also need to consider using clearfix blocks. For example;

<!-- Add clearfix for only the required viewport(s) -->
<div class="clearfix visible-md-block visible-lg-block"></div>

Without clearfix md or lg might end up as:

[A] [B]
    [C] [D]

This is particularly important if you want more than 2 elements in the final “column”, or the heights of the elements start throwing other elements out of alignment.

With the clearfix md or lg would be:

[A] [B]
    [C]
    [D]

Example fiddle: http://jsfiddle.net/L5174o8d/

References