How can I reverse the order of columns in Bootstrap 4? - css

I have this simple scenario:
<div class="row">
<div class="col-md-12 col-lg-6 col-xl-7">A</div>
<div class="col-md-12 col-lg-6 col-xl-5">B</div>
</div>
basically if md
A
B
I would like it, if md
B
A
I have tried many variants found on web, like flex-first and whatnot.. can't seem to get it to work
Any ideas?

If you want to change order on md and larger sizes you can use order-md-, this is provided by bootstrap. It looks like, if you want to change order only on md size you will have to define normal order on one larger size Fiddle
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />
<div class="row">
<div class="col-md-12 order-md-2">A</div>
<div class="col-md-12 order-md-1">B</div>
</div>

It's also possible to use the flex- helper classes to solve this issue.
This solution allows you to reverse the order without the order count on the columns.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />
<div class="row flex-column-reverse flex-lg-row">
<div class="col-md-12 col-lg-6 col-xl-7">A</div>
<div class="col-md-12 col-lg-6 col-xl-5">B</div>
</div>
The flex-direction is now reversed by default (md) and will be overriden on the lg breakpoint.

For Bootstrap v4.1 you can use
<div class="row flex-row-reverse">
<div class="col-md-12 col-lg-6 col-xl-7">A</div>
<div class="col-md-12 col-lg-6 col-xl-5">B</div>
</div>
More information here: https://getbootstrap.com/docs/4.1/utilities/flex/#direction

Easiest solution will be:
.row{
flex-direction: row-reverse;
}

This works for me:
<div class="col-md order-1">First column</div>
<div class="col-md order-md-1">Second column</div>
Output on responsive state:
Second column
First column

I know the question is about Bootstrap 4, but I saw some people asking how to do it in version 5. Here's the example:
<div class="col-md-6 order-2 order-md-1">
<!-- YOUR CODE -->
</div>
<div class="col-md-6 order-1 order-md-2">
<!-- YOUR CODE -->
</div>
ref: https://getbootstrap.com/docs/5.0/layout/columns/#order-classes

For Bootstrap 5, the class names have changed to order-x where x is the order, i.e. (directly from the docs):
<div class="container">
<div class="row">
<div class="col">
First in DOM, no order applied
</div>
<div class="col order-5">
Second in DOM, with a larger order
</div>
<div class="col order-1">
Third in DOM, with an order of 1
</div>
</div>
</div>
See more at https://getbootstrap.com/docs/5.0/layout/columns/#reordering

Related

Bootstrap cols not in one row

I have an issue with col that aren't in one row. I have all of them in one container, row and 3 columns col-md-5, col-md-2 and col-md-5. All paddings and margins are set from CSS bootstrap.
<div class="container">
<div class="row">
<div class="col-xs-12">
<div>
<div class="col-md-5 col-xs-12">
<div>
<div class="row-7 clearfix">
<p class="text">Výtečně<br>chutnám</p>
<img class="text-2" src="../images/unk.png" alt="!" title="!">
</div>
<div class="button-holder">Koupit</div>
</div>
</div>
<div class="col-md-2 col-xs-12 mobile-hide">
<div class="line"></div>
</div>
<div class="col-md-5 col-xs-12">
<p class="text-3">TEXT</p>
</div>
</div>
</div>
</div>
</div>
Dev page here: http://dev.ekolok.cz/
Thank you for advice and help.
I don't know what exactly your layout should look like but here is how bootstrap column layout should look:
<div class="container">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
</div>
</div>
The problem in your code is that columns aren't a direct child of row etc.
Try to refactor the code and see if you understand what is going on...
Bootstrap columns must add up to 12 per row. If you would like multiple elements in a row, your 'col-whatevers' must add up to 12.
For example if you want your title and your button on the same row your code would need to look like this:
<div class="container">
<div class="row">
<div class="col-6">
<p class="text">Výtečně<br>chutnám</p>
</div>
<div class="col-6">
<img class="text-2" src="../images/unk.png" alt="!" title="!">
</div>
</div>
</div>
You would then need to add another 'row' element and fill that up with columns that add up to 12. Not sure if this is what you're asking but it's what I could make out from your question.
Also, empty 'divs' are redundant, it doesn't mean anything. You can remove all your tags that don't carry a class, id etc.

How to reorder bootstrap cols on mobile?

I have 4 cols as follows on desktop, and I would like them to reorder in such a way that it appears as depicted below on mobile.
Do I need custom css for this?
(I Cannot post images yet, so here is the link to the image:)
https://imgur.com/a/sasLETe
I have tried keeping them in parent cols of two in each, but to no-avail.
I am a backend guy, so I am not too familiar with frontend stuff.
Current HTML:
<div class="row">
<div class="col-md-6 col-sm-12">
<div>A</div>
<div>C</div>
</div>
<div class="col-md-6 col-sm-12">
<div>B</div>
<div>D</div>
</div>
</div>
I would like it to behave as I have depicted in the image.
How do I go about doing this? I do not want to resort to JavaScript. Any help is appreciated.
Thanks.
If you play with your html structure then no need to have custom css.
Just update classes and content in your code as follows:
<div>
<div class="row">
<div class="col-md-6 col-sm-12">A</div>
<div class="col-md-6 col-sm-12">B</div>
</div>
<div class="row" >
<div class="col-md-6 col-sm-12">C</div>
<div class="col-md-6 col-sm-12">D</div>
</div>
</div>
```
You can use this code , bootstrap only
<section>
<div class="container my-5">
<div class="row">
<div class="col-md-6">A</div>
<div class="col-md-6">B</div>
</div>
<div class="row">
<div class="col-md-6">c</div>
<div class="col-md-6">d</div>
</div>
</div>
</section>

Boostrap 4 grid re-order

I have the following HTML code using Bootstrap 4:
<div class="row">
<div class="col-12 col-sm-4">
First, but unordered
</div>
<div class="col-12 col-sm-4">
Second, but unordered
</div>
<div class="col-12 col-sm-4 order-sm-1">
Third, but first
</div>
</div>
I am expecting the third div to be placed in first position on sm screens but it is not. Here is a JSFiddle What have I missed? Thanks!
The order class is not really handeling the order of the elements it's instead like a swap system. So you need to declare an order in other divs too
<div class="row">
<div class="col-12 col-sm-4 order-sm-12">
First, but unordered
</div>
<div class="col-12 col-sm-4 order-sm-12">
Second, but unordered
</div>
<div class="col-12 col-sm-4 order-sm-1">
Third, but first
</div>
</div>
Here's a Fiddle
EDITED
I am changing the code in order to adapt it for bootstrap 4 after user comment of #Andrei Gheorghiu
you can use the class order-sm-first which sets the to order=-1, with this code the third div will be displayed as first for screens that are sm or larger meaning larger than 576px
<div class="row">
<div class="col-12 col-sm-4">
First, but unordered
</div>
<div class="col-12 col-sm-4">
Second, but unordered
</div>
<div class="col-12 col-sm-4 order-sm-first">
Third, but first
</div>
</div>
here you can see the forked version of you jsfiddle
tl;dr:
Change your markup to:
<div class="container">
<div class="row">
<div class="col-12 col-sm-4">
First, but unordered
</div>
<div class="col-12 col-sm-4">
Second, but unordered
</div>
<div class="col-12 col-sm-4 order-first order-md-0">
Third, but first
</div>
</div>
</div>
the details
.order-sm-1 will place order: 1 on the element, according to lines #53-#55 in _grid-framework.scss:
#for $i from 0 through $columns {
.order#{$infix}-#{$i} { order: $i; }
}
But placing order: 1 on one column without the rest having an order will send that column at the end of the stack, because the default value of order is 0. Any following siblings without an order will be placed before your column.
In the same file, at line #49, we find .order#{$infix}-first (which sets order:-1). This will place a column before the rest, as well as .order#{$infix}-last will place it after all the rest.
To sum up:
use .order-sm-last and .order-sm-first to make a column go to front or back of the stack
use .order-sm-#{n} to give it an order but also set the order on siblings (order-sm-1 will make a column first if you place order-sm-2 (or higher - up to 12) on the other columns).
Note they only defined 12 levels for order, .order-sm-13 won't work, unless you define it yourself.
As a final note: if you want your column ordered on sm and below, your should replace order-sm-1 with order-first order-md-0. .order-first makes it first on all intervals and .order-md-0 sets its order back to 0 on md and above, so it keeps its place in the layout:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<div class="container">
<div class="row">
<div class="col-12 col-sm-4">
First, but unordered
</div>
<div class="col-12 col-sm-4">
Second, but unordered
</div>
<div class="col-12 col-sm-4 order-first order-md-0">
Third, but first
</div>
</div>
</div>

Offset in Bootstrap is not working.

SO I'm dusting up on bootstrap atm, and decided as a practice to create some grids with offsets.
I have no idea what's going on.
<div class="row">
<div class="col-md-4">.col-md-4</div>
<div class="col-md-4 offset-md-4">.col-md-4 .offset-md-4</div>
</div>
<div class="row">
<div class="col-md-3 offset-md-3">.col-md-3 .offset-md-3
</div>
<div class="col-md-3 offset-md-3">.col-md-3 .offset-md-3
</div>
<div class="row">
<div class="col-md-6 offset-md-3">.col-md-6 .offset-md-3</div>
</div>
These should make it three rows, first with a space in between the div's, etc
but it's just not working.
Any elucidation as to why would be appreciated. I append codepen link below.
https://codepen.io/anon/pen/QgXPae
Use
<div class="col-md-4 col-md-offset-4">.col-md-4 .offset-md-4</div>
it should be col-md-offset-3
you can see the documentation of grid in this link
[offsets][1]http://getbootstrap.com/css/#grid

How to achieve this Bootstrap layout?

I have a Bootstrap layout that I would like to create but I can't figure out how I would accomplish it. Basically, on desktop sizes the layout will be a 4-column, a 6-column and a 2-column next to each other in that order, and then mobile I would like for the 4 and the 2-column to remain next to each other and for the 6-column to drop below.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-xs-8 col-sm-4">
A
</div>
<div class="col-sm-6">
B
</div>
<div class="col-xs-4 col-sm-2">
C
</div>
<div class="clearfix"></div>
</div>
</div>
So on mobile I want "A" and "C" to be inline and "B" to be a full 12-columns below "A" and "C"
You can do it with regular bootstrap push and pull and don't need to rely on absolute positioning. See bootstrap docs here: http://getbootstrap.com/css/#grid-column-ordering
Easily change the order of our built-in grid columns with .col-md-push-* and .col-md-pull-* modifier classes.
You could try something like this: - as long as this container is relatively poisitioned, it should be fine.
https://jsfiddle.net/znwsk6fw/
<div class="container">
<div class="row">
<div class="col-xs-8 col-sm-4">
A
</div>
<div class="col-sm-6 col-md-6 col-xs-12 ">
B
</div>
<div class="col-xs-4 col-sm-2 col-md-2 column_c">
C
</div>
<div class="clearfix"></div>
</div>
</div>
#media screen and (max-width:768px) {
.column_c {
position:absolute;
right:0;
}
}

Resources