Bootstrap v3, Reverse Stacking Order of Full-Width Columns - css

Is it possible to reverse the order of full width columns in Bootstrap v3? For example...
-xs (both A and B are col-xs-12)
[ A ]
[ B ]
-sm (both A and B are col-sm-12)
[ B ]
[ A ]
I know that I could use hide/show techniques, but I want to avoid duplicate content because both A and B have a lot of dynamically generated content, and duplicating that content will slow the page down quite a bit. Thanks.

Use Bootstraps .col-md-push-* and .col-md-pull-* classes.
More on this, here: http://getbootstrap.com/css/#grid-column-ordering
Example:
<div class="container">
<div class="row">
<div class="col-md-9 col-md-push-3">.col-md-9 .col-md-push-3</div>
<div class="col-md-3 col-md-pull-9">.col-md-3 .col-md-pull-9</div>
</div>
</div>
CODEPEN DEMO
For full width column example using push/pull helper classes please see this JS bin:
<div class="row">
<div class="col-xs-12 col-sm-6 col-sm-push-6">
<p>test2</p>
</div>
<div class="col-xs-12 col-sm-6 col-sm-pull-6">
<p>test1</p>
</div>
</div>
http://jsbin.com/gazipa/2/edit?html,css,output
You can also use CSS transform to change it's ordering at a viewport breakpoint:
#media (max-width: 767px) {
.row.reorder-xs {
/*bring your own prefixes*/
transform: rotate(180deg);
direction: rtl; /* Fix the horizontal alignment */
}
.row.reorder-xs > [class*="col-"] {
/*bring your own prefixes*/
transform: rotate(-180deg);
direction: ltr; /* Fix the horizontal alignment */
}
}
http://jsbin.com/gazipa/3/edit?html,css,output

2018 Update Bootstrap 4
In Bootstrap 4, you can change the order of full-width (12 unit) columns using the flexbox ordering classes. For example,
<div class="container">
<div class="row">
<div class="col-12 order-sm-1 order-2">1</div>
<div class="col-sm-12 order-1">2 (first on xs)</div>
</div>
</div>
https://www.codeply.com/go/VUjKsM3cUD

For those on Bootstrap 3, just add a flexbox wrapper. Then you can add flex-direction with column reverse. The code would look something like this (assume mobile first)...
Markup:
<div class="row">
<div class="flex-switch">
<div class="col-xs-12 col-sm-6">
This is a column
</div>
<div class="col-xs-12 col-sm-6">
This is a column
</div>
</div>
</div>
CSS:
.flex-switch {
display: flex;
flex-direction: column-reverse;
#include sm-min {
flex-direction: row;
}
}

Related

In a flex column layout with a bigger and a smaller column, can I have the smaller column display full-width in case it's wrapped?

I'm using Bootstrap 4. I have a flex layout like
.larger {
background: blue;
}
.smaller {
background: red;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<div class="row">
<div class="col larger"> larger column </div>
<div class="col-3 smaller"> smaller column </div>
</div>
Now when I resize the window to a very small width, the second column is displayed in a new line, all by itself. That's fine. However, it is still displayed in only a fraction of the line's width. That's bad. If there is a linebreak, I would like to have the divs fill the available size. How can I do that?
You are not obliged to use the row/col system:
div {
outline:1px solid red
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.1/dist/css/bootstrap.min.css">
<div class="d-flex flex-wrap">
<div class="w-75 flex-grow-1"> larger column </div>
<div class="flex-fill"> smaller column </div>
</div>
Kindly use this code
<div class="row">
<div class="col"> larger column </div>
<div class="col-12 col-md-3"> smaller column </div>
</div>
.col-md-3 takes a third of the screen on tablets and larger screens but on small screens it has no effect.
Since you've added a css tag, here's a pure CSS alternative solution:
.container{
display: flex;
flex-wrap: wrap;
}
.larger {
background: blue;
flex: 1;
width: 100%;
min-width: 75%;
}
.smaller {
background: red;
flex: 1;
}
<div class="container">
<div class="larger"> larger column </div>
<div class="smaller"> smaller column </div>
</div>
What I've done here is use flexbox to make use of flex-wrap for wrapping and flex-grow to make them grow to whatever space is available. Then lastly, using min-width to set the larger column to 3/4th of the row. If you're interested in flexbox and how it works, more on it here.
Follow the grid options.
Each row is made up of 12 columns. If you want the second element to take up 3 columns on small screen and above (~576px), but all 12 columns on less than that, the classes to use would be col-12 col-sm-3.
<link href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.1/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="row">
<div class="col"> larger column </div>
<div class="col-12 col-sm-3"> smaller column </div>
</div>

Equal height divs kept together side-by-side always

Using Bootstrap 3.x and creating a common 3 column layout. In desktop view I want all three divs in the same row and the same height. When shrunk down to the smallest width I want the first two divs to always remain next to each other but the third drop below. Always, the first two divs should be the same height. If the second div is shorter than the first, the third div ends up underneath the second, to the right of the first.
<div class="row">
<div class="col-sm-12 col-md-9">
<!-- keep these two divs together side by side and the same height -->
<div class="col-xs-6 col-sm-6 col-md-6">
this is panel one
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
this is panel two
</div>
</div>
<div class="col-sm-12 col-md-3">
<!-- this div should be beside the other two on large screens and drop below on xsmall screens -->
side bar ad
</div>
</div>
Here is another way to visualize the issue. The first two need to be the same height: http://www.bootply.com/29cNrJrEwT
Connie DeCinko, Hi there.
To have the divs flow like you ask here you would do that this way.
Here is the Fiddle.
You said that the first two blocks should have the same height so you would not have a issue with the 3rd block flowing below.
But, if say the second block is to be shorter than the first block then wrap the first 2 block in a row and also wrap the 3rd block in a row and add to the row for the 3rd block col-xs-12 so when on small screens it takes up the full width.
<div class="container">
<div class="row text-center">
<div class="col-xs-6 col-sm-4 block1"><h2>Block 1</h2></div>
<div class="col-xs-6 col-sm-4 block2"><h2>Block 2</h2></div>
<div class="col-xs-6 col-sm-4 block3"><h2>Block 3</h2></div>
</div>
</div>
If you do actually want to have the second block shorter.
Then you could do it this way.
Here is the Fiddle.
Note this is not using Flex ... Flex does not have full browser support.
<div class="container">
<div class="row-fluid col-xs-12 col-sm-8 text-center clear">
<div class="col-xs-6 col-sm-6 block4"><h2>Block 1</h2></div>
<div class="col-xs-6 col-sm-6 block5"><h2>Block 2</h2></div>
</div>
<div class="row-fluid col-xs-12 col-sm-4 text-center clear">
<div class="col-xs-6 col-sm-12 block6 "><h2>Block 3</h2></div>
</div>
</div>
Flexbox was created for this. Literally.
http://jsfiddle.net/rudiedirkx/6ka86vfx/
Define a flex container with display: flex
Tell its children to flex: 1
Except the bigger children, they're flex: 3 in this case
Children can be flex containers too
It's children have flex: 1 again, to make them equal size
Your class names aren't the most descriptive, but the CSS is otherwise very simple:
.row { /* flex container */
display: flex;
}
.row > div { /* all its children */
flex: 1;
}
.row > div:first-child { /* one of its children is bigger (3:1) and is a flex container itself */
display: flex;
flex: 3;
}
.row > div:first-child > div { /* all its children are equal size */
flex: 1;
}

Responsive Bootstrap - changing number of divs per row from desktop to mobile

New to responsive bootstrap here. I have 2 div's per row and 2 rows showing on my desktop. I used display: inline-block (see below link):
https://jsfiddle.net/9ya7kb67/
HTML:
<div class="parent">
<div class="child"> content </div>
<div class="child"> content </div>
<div class="child"> content </div>
<div class="child"> content </div>
</div>
CSS:
.parent {
width: 200px;
}
.child {
display: inline-block;
width: 80px;
}
However, I'd like to keep this layout in desktop but change it to 1 div per row (for 4 rows) on mobile using responsive bootstrap. How can I do this?
Check the bootstrap documentation for its grid system here: http://getbootstrap.com/css/#grid-example-mixed-complete
There are 4 different types of classes col-sm col-xs col-md col-lg each one fitted for different screen sizes and you can combine them to make dynamic grids for your site.
<div class="row">
<div class="col-xs-12 col-md-6">Test col</div>
<div class="col-xs-12 col-md-6">Test col</div>
<div class="col-xs-12 col-md-6">Test col</div>
<div class="col-xs-12 col-md-6">Test col</div>
</div>
I didn't exactly understand what your end goal is, but the code above is something similar to what you want. On small screens col-xs-12 will fill the entire row, on medium to large screen you will have two col-md-6 on each row. Make sure to read the bootstrap documentation to learn more about it.

Bootstrap align Columns of different height

I would like to be able to align an unknown number of columns with an unknown height. Since I do not know how many columns there will be it is not ideal for me to use multiple rows to split up the columns. I can almost achieve the outcome I want by using list items.
The one thing I don't like about using list items is that once the page hits the resize point I am left with the extra space on the right hand side. The top set is using list items and the bottom set is using bootstrap's col's. The problem with the bottom set is when the col's break they don't align to the furthest left position.
Is there a way to achieve this using bootstrap?
<div class="container-fluid">
<div class="row">
<ul>
<li class="list-item" style="height:200px;"></li>
<li class="list-item" style="height:120px;"></li>
<li class="list-item" style="height:100px;"></li>
</ul>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-3">
<div class="box" style="height:200px"></div>
</div>
<div class="col-xs-12 col-sm-6 col-md-3">
<div class="box" style="height:120px"></div>
</div>
<div class="col-xs-12 col-sm-6 col-md-3">
<div class="box" style="height:100px"></div>
</div>
<div class="col-xs-12 col-sm-6 col-md-3">
<div class="box"></div>
</div>
</div>
</div>
jsFiddle
Try this :
.row {
display: flex;
flex-wrap: wrap;
}
Another way to handle it, and still maintain Bootstrap's responsive columns is to use CSS to force a clear:left every x columns. For example, when you have 4 columns in a row:
.row > .col-md-3:nth-child(4n+1) {
clear: left;
}
http://codeply.com/go/OHg5vB0Xg3
You really don't need bootstrap to handle this. Here's one potential solution using inline-block. I imagine it's compatible with bootstrap.
.box {
margin: 15px;
width: 80px;
background-color: grey;
display: inline-block;
vertical-align: top;
}
<div>
<div class="box" style="height: 120px;"></div>
<div class="box" style="height: 20px;"></div>
<div class="box" style="height: 40px;"></div>
<div class="box" style="height: 60px;"></div>
<div class="box" style="height: 80px;"></div>
</div>
<div>
<div class="box" style="height: 20px;"></div>
<div class="box" style="height: 60px;"></div>
<div class="box" style="height: 80px;"></div>
</div>
Yes! There is a way. And it's a css-only solution. Try this:
.col-xs-6:nth-of-type(2n+3),
.col-xs-4:nth-of-type(3n+4),
.col-xs-3:nth-of-type(4n+5),
.col-xs-2:nth-of-type(6n+7),
.col-xs-1:nth-of-type(12n+13)
{
clear: both;
}
#media (min-width: 768) {
[class*="col-xs"][class*="col-sm"],
[class*="col-xs"][class*="col-md"],
[class*="col-xs"][class*="col-lg"]
{
clear: none;
}
.col-sm-6:nth-of-type(2n+3),
.col-sm-4:nth-of-type(3n+4),
.col-sm-3:nth-of-type(4n+5),
.col-sm-2:nth-of-type(6n+7),
.col-sm-1:nth-of-type(12n+13)
{
clear: both;
}
}
#media (min-width: 992) {
[class*="col-sm"][class*="col-md"],
[class*="col-sm"][class*="col-lg"]
{
clear: none;
}
.col-md-6:nth-of-type(2n+3),
.col-md-4:nth-of-type(3n+4),
.col-md-3:nth-of-type(4n+5),
.col-md-2:nth-of-type(6n+7),
.col-md-1:nth-of-type(12n+13)
{
clear: both;
}
}
#media (min-width: 1200) {
[class*="col-md"][class*="col-lg"]
{
clear: none;
}
.col-lg-6:nth-of-type(2n+3),
.col-lg-4:nth-of-type(3n+4),
.col-lg-3:nth-of-type(4n+5),
.col-lg-2:nth-of-type(6n+7),
.col-lg-1:nth-of-type(12n+13) {
clear: both;
}
}
// use .col-nobreak class to deactivate this fix
.col-nobreak {
clear: none !important;
}
First of all we begin with the column type for the smallest resolution (< 768) (col-xs-*). If the row breaks for the several column widths, we set the css property clear to clear: both.
In the next step we reset for the first breakpoint the css property clear with clear: both for all columns, which has a column width for higher resolutions (all columns width additional col-sm-x,col-md-x,col-lg-x) and set the break of one column-row for the col-sm-* type.
And so on...
With the .col-nobreak class you can deactivate the css hack.
You have to fulfill these requirements:
The cols for the parent row container must have the same size
The cols for the parent row must have the same html tag type (div, secion)

Change the order of col-*-12 columns in Bootstrap using push/pull

I have two columns of the same size (.col-xs-12) and I would change their place when the screen size correspond to that of a mobile device. I would place them in the reverse order.
I have read that push and pull bootstrap directives help to accomplish that, but is it possible to change the place of two columns of the same size with the following classes?
div.col-xs-12.col-xs-push-12
p test1
div.col-xs-12.col-xs-pull-12
p test2
Actually you can not reorder the columns having .col-*-12 by push/pull helper classes. The sum of columns exceeds the default 12 columns which is defined by #grid-columns.
You could either change the order of columns in HTML and then use the ordering classes on larger screens as follows:
EXAMPLE HERE
<div class="row">
<div class="col-xs-12 col-sm-6 col-sm-push-6">
<p>test2</p>
</div>
<div class="col-xs-12 col-sm-6 col-sm-pull-6">
<p>test1</p>
</div>
</div>
Or use this fancy approach to reverse the ordering of the columns that are placed vertically under each other:
EXAMPLE HERE
#media (max-width: 767px) {
.row.reorder-xs {
transform: rotate(180deg);
direction: rtl; /* Fix the horizontal alignment */
}
.row.reorder-xs > [class*="col-"] {
transform: rotate(-180deg);
direction: ltr; /* Fix the horizontal alignment */
}
}
It's worth noting that CSS transforms are supported in IE9 as well; Just don't forget to add vendor prefixes.
In Bootstrap 4, you can change the order of full-width (12 unit) columns using the flexbox ordering classes.
Update 2017 - Bootstrap 4 alpha 6
In 3.x you could only push/pull columns left or right (horizontally). With the new flexbox ordering utils in 4.x, it's now possible to change the order of columns vertically...
<div class="container">
<div class="row">
<div class="col-12">1</div>
<div class="col-sm-12 flex-first flex-sm-unordered">2 (first on xs)</div>
</div>
</div>
http://www.codeply.com/go/7RUJORgxBK
Update Bootstrap 4 Beta
The alpha flex- ordering classed have changed to order- classes.
<div class="container">
<div class="row">
<div class="col-12 order-sm-1 order-2">1</div>
<div class="col-sm-12 order-1">2 (first on xs)</div>
</div>
</div>
https://www.codeply.com/go/VUjKsM3cUD
You can totally do it, see Bootstrap's Grid Column Ordering
But of course your example will have no effect since xs-12 is a full width column, so this will apply only to models where the sum of the columns is 12 (or if 16 or whatever if you customize your Bootstrap grid). See the Bootstrap example on that same page for illustrative purposes:
<div class="row">
<div class="col-md-9 col-md-push-3">.col-md-9 .col-md-push-3</div>
<div class="col-md-3 col-md-pull-9">.col-md-3 .col-md-pull-9</div>
</div>
If you need to reorder cols for a responsive case like
div.col-xs-12.col-sm-9 # this should be on the bottom for col-xs-12
p test1
div.col-xs-12.col-sm-3 # this should be on the top for col-xs-12
p test2
you could use a .pull-right class and reverse the column order.
div.col-xs-12.col-sm-3.pull-right
p test2
div.col-xs-12.col-sm-9
p test1
then they are in order for col-xs-12 and appear correctly for the other breakpoints.
In case anyone comes here with a similar issue like me, only finding push/pull doesn't fit your needs, because either col-xs-12 wont pull/push or using more than 2 columns makes it tougher to figure out the push/pull values here is my solution.
Below is the fancy solution by #hashemquolami
#media (max-width: 767px) {
.row.reorder-xs {
transform: rotate(180deg);
direction: rtl; /* Fix the horizontal alignment */
}
.row.reorder-xs > [class*="col-"] {
transform: rotate(-180deg);
direction: ltr; /* Fix the horizontal alignment */
}
}
Although this approach works fine, I have a different solution:
The bootstrap grid works by floating the columns left, this can easily be altered with css. Look at the markup below, as bonus col-md-offset-1 reversed to emulate 5 centered columns.
HTML
<div class="container">
<div class="row reverseOrder">
<div class="col-md-2 col-md-offset-1">A</div>
<div class="col-md-2">B</div>
<div class="col-md-2">c</div>
<div class="col-md-2">d</div>
<div class="col-md-2 ">e</div>
</div>
</div>
CSS
#media screen and ( min-width: 992px) {
.reverseOrder [class^="col-"] {
float: right;
}
.reverseOrder .col-md-offset-1 {
margin-right: 8.333333333333332%;
margin-left: 0px;
}
}
JSFIDDLE
I had same problem and solved it this way:
HTML
<div class="col-md-10 col-sm-10 col-xs-12 more-than" style="display: none;">
<p>test</p>
</div>
<div class="col-md-2 col-sm-2 col-xs-12">
<p>test</p>
</div>
<div class="col-md-10 col-sm-10 col-xs-12 less-than">
<p>test</p>
</div>
CSS
#media (max-width: 767px){
.less-than {
display: none;
}
.more-than {
display: block !important;
}
}

Resources