How to order divs in bootstrap 4? [duplicate] - html

This question already has answers here:
Order columns through Bootstrap4
(6 answers)
Closed 3 years ago.
I have to divs next to each other in a row div. On mobile, i want the right_side div(col-md-9) to be the first div.
I tryed ordering it with flex, but it messed up all my site.
<div class="row">
<div class="col-md-3">Left side</div>
<div class="col-md-9">Right side, but first on mobile or tablet</div>
<div class="clearfix"></div>
</div>
If i am viewing the site on mobile or tablet, i want the col-md-9 div as first div.

#media screen and (max-width: 767px) {
.col-md-3 {
order: 2;
}
.col-md-9 {
order: 1;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<div class="row">
<div class="col-md-3">Left side</div>
<div class="col-md-9">Right side, but first on mobile or tablet</div>
<div class="clearfix"></div>
</div>

there is an option to reverse the row using class. here is the code please try it once.
<div class="row flex-row-reverse">
<div class="col-md-9">Right side, but first on mobile or tablet</div>
<div class="col-md-3">Left side</div>
<div class="clearfix"></div>
</div>

Just use order property.
#media only screen and (max-width: 760px) {
.col-md-3 {
order:2;
}
}
#media only screen and (max-width: 760px) {
.col-md-3 {
order:2;
}
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="row">
<div class="col-md-3">Left side</div>
<div class="col-md-9">Right side, but first on mobile or tablet</div>
<div class="clearfix"></div>
</div>

Use Column Ordering to accomplish this.
col-md-push-6 will "push" the column to the right 6 and col-md-pull-6 will "pull" the column to the left on "md" or greater view-ports. On any smaller view-ports the columns will be in normal order again.
I think what throws people off, is that you have to put B above A in your HTML. There may be a different way to do this where A can go above B in the HTML, but I'm not sure how to do it...
See Code
<div class="row">
<div class="col-md-6 col-md-push-6">B</div>
<div class="col-md-6 col-md-pull-6">A</div>
</div>
view-port >= md
|A|B|
view-port < md
|B|
|A|

you can do with flex and order as below example.
If it is messing-up with all sites, then give your custom class and then give CSS.
#media screen and (max-width: 531px) {
.row { display: flex; flex-direction: column; }
.two { order: 1; }
.one { order: 2 }
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css">
<div class="row">
<div class="col-md-3 one">Left side</div>
<div class="col-md-9 two">Right side, but first on mobile or tablet</div>
<div class="clearfix"></div>
</div>

Related

how to change alignment of div in mobile version

I have 3 div in my html. div1, div2,div3. Now in mobile version I want to change the alignment of those div. Please see the below images for better understand.
HTML
<div class="row">
<div class="col-md-12">
Div1
</div>
<div class="col-md-9" style="float:right;">
Div3
</div>
<div class="col-md-3" style="float:left;">
Div2
</div>
</div>
NOTE: I am using bootstrap 3
How to do that?
Anybody help please ?
You need to use Grid: column ordering
According to the mobile-first concept you need to define default view for mobile first in the right order.
section 1
section 3
section 2
Then assign col-md-push-3 for section 3 and col-md-pull-9 for section 2 and define its width using col-md- class.
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-xs-12">
<div>section 1</div>
</div>
<div class="col-xs-12 col-md-9 col-md-push-3">
<div>section 3</div>
</div>
<div class="col-xs-12 col-md-3 col-md-pull-9">
<div>section 2</div>
</div>
</div>
</div>
I answered something similar to this question before. reordering col-12 are not possible with bootstrap-3 (push/pull). You can use grid together with a media query to do this.
<div class="row reordered">
<div class="col-md-12">
Div1
</div>
<div class="col-md-3">
Div2
</div>
<div class="col-md-9">
Div3
</div>
</div>
css
#media screen and (max-width: 992px) { //col-md breakpoint
.reordered {
display: grid;
}
.reordered.col-md-3 {
order: 2;
}
.reordered.col-md-9 {
order: 1;
}
}
use col-sm-12 hopefully it will work
try this:
#media only screen and (max-width: 400px) {
.row {
display: grid !important;
grid-template-columns: auto !important;
}
}

Automatically adjust column width in bootstrap

I am not that much good in designs and I have a design issue which is dragging me for hours. If anyone can help me on that would be appreciable.
I have a div with a .container class and inside that, we have three div's. One with a class .col-md-3 another with a class .col-md-6 and the third one with class .col-md-3. The third column shouldn't be there at all. In some cases, it only the first and second div will be there.
Can we make the second div taken the space of third one if it is not there ? Is there any method in bootstrap for this.
Please help me.
you can use col classes to div. it will automatically adjust if one div is removed from the row
check https://getbootstrap.com/docs/4.1/layout/grid/#equal-width for more information
.col {
background-color: rgba(86,61,124,.15);
border: 1px solid rgba(86,61,124,.2);
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col">
1 of 2
</div>
<div class="col">
2 of 2
</div>
</div>
<div class="row">
<div class="col">
1 of 3
</div>
<div class="col">
2 of 3
</div>
<div class="col">
3 of 3
</div>
</div>
</div>
you can check by removing one column from the row
You heck a little but on bootstrap 3. by adding some of custom CSS.
Please View in full page mode
$('button').click(function(){
$('#toggle_col').toggle();
});
.col-sm-3 {
background-color: red;
}
.col-sm-6 {
background-color: blue;
}
/* this will do the trick */
#media (min-width: 768px)
.flex-row {
display: flex;
flex-wrap: wrap;
}
}
.flex-col {
flex: 1 0 auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="container">
<div class="row flex-row">
<div class="col-sm-3">3</div>
<div class="col-sm-6 flex-col">6</div>
<div class="col-sm-3" id="toggle_col">3</div>
</div>
</div>
<button>Toggle Column</button>

Switch bootstrap columns on mobile view

Consider the following:
<div class="row">
<div id="side_panel" class="col-md-3 cold-xs-12">
Side panel
</div>
<div id="mainContentRight" class="col-md-9 cold-xs-12 ">
Main content area
</div>
</div>
Is there a way to switch the order of these columns for mobile view so that the main content appears before the side panel?
Bootstrap is a mobile first platform so change your mark up to how you want it to look on the mobile with the full xs-12. You can then use push and pull to move the column. Here is a link to the doc's.
<div class="row">
<div id="mainContentRight" class="col-md-9 col-md-push-3 cold-xs-12 ">
Main content area
</div>
<div id="side_panel" class="col-md-3 col-md-pull-9 cold-xs-12">
Side panel
</div>
</div>
Unfortunately, You can not reorder using push and pull for 12 column classes i.e. col-xs-12. Either change the markup to be able to include the 12 column grid system or use a simple solution using flexbox below:
#media (max-width: 768px) {
.reorder {
display: flex;
flex-direction: column;
}
#side_panel {
order: 2;
}
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="row reorder">
<div id="side_panel" class="col-md-3 cold-xs-12">
Side panel
</div>
<div id="mainContentRight" class="col-md-9 cold-xs-12">
Main content area
</div>
</div>

Bootstrap 3 change fullwidth column order

One of our designers has created a layout where the footer content order changes from desktop to mobile. I'm using bootstrap and would like to do it as simply as possible. I believe the following is the correct markup but it doesn't seem to work for full width columns?
HTML
<div class="container">
<div class="row">
<div class="col-sm-12 col-sm-push-12">
<div class="row">
<div class="col-md-12">
<p>Ooo content</p>
</div>
</div>
</div>
<div class="col-sm-12 col-sm-pull-12">
<div class="row">
<div class="col-md-12">
<p>Ooo more content</p>
</div>
</div>
</div>
</div>
</div>
Example: https://jsfiddle.net/rwydcepc/
The aim of the above is to switch the top column with the bottom column when the sm is applied. This doesn't seem to happen though and everything screws up?
Is there another way to do this using bootstrap or with some simple additional css, ideally I'd like to avoid duplicate content and showing / hiding or relying on Javascript.
There is a similar question here but it doesn't answer the question: Change the order of col-*-12 columns in Bootstrap 3 by using push/pull
There is already a solution using Bootstrap reordering but not for 100% width columns, the below solution uses Flexbox method.
#media (max-width: 768px) {
.reorder .row {
display: flex;
display: -ms-flex;
flex-direction: column;
}
.reorder .row .item1 {
order: 2;
}
.reorder .row .item2 {
order: 1;
}
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="container reorder">
<div class="row">
<div class="col-sm-12 item1">
<p>Ooo content</p>
</div>
<div class="col-sm-12 item2">
<p>Ooo more content</p>
</div>
</div>
</div>

Bootstrap Push/Pull Issues

I'm fairly new to Bootstrap, and am having issues getting the alignment of elements to change right using push and pull. Here is the desired result for small screen sizes (xs and above):
-------------------
| Search |
-------------------
| Directory |
-------------------
| Preferences |
-------------------
Then for all screens sized md and above, I want this layout:
-------------------
| Search | Pref.|
-------------------
| Directory |
-------------------
I've read that Bootstrap layouts should be designed mobile-first, so my code is as follows:
.a {
background-color: green;
}
.b {
background-color: blue;
}
.c {
background-color: red;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-xs-12 col-md-7 a">Search</div>
<div class="col-xs-12 b col-md-push-12">Directory</div>
<div class="col-xs-12 col-md-5 c col-md-pull-7">Preferences</div>
</div>
</div>
Here's a Bootply with my code. It works as expected for the small screens, with each div stacked in the correct order, but I can't figure out why push and pull are just moving elements left and right, not joining them as I thought they would (even when I mess with the number of columns to push/pull). Can anyone point me in the right direction?
As has been mentioned, right now, there is no way to do this using only bootstrap classes. push, pull and offset will only shift elements horizontally, not vertically.
However, If you are targeting browsers that have support for it, you can use a combination of flexbox and media queries to achieve this re-ordering without needing to duplicate content.
Support breakdown: http://caniuse.com/#feat=flexbox
First, remove your push and pull classes and wrap your columns in a div.
<div class="container">
<div class="row">
<div class="flexBox">
<div class="a col-xs-12 col-md-7">Search</div>
<div class="b col-xs-12 ">Directory</div>
<div class="c col-xs-12 col-md-5">Preferences</div>
</div>
</div>
</div>
Second, use some CSS rules to set up the display of the wrapper div.
.flexBox
{
display:flex;
flex-wrap:wrap;
}
Lastly, set up a media query which will re-order the elements once the width is above the xs breakpoint (768px)
#media all and ( min-width: 768px ) {
.flexBox .a { order:1; }
.flexBox .b { order:3; }
.flexBox .c { order:2; }
}
Full example below.
.a { background-color:green; }
.b { background-color:blue; }
.c { background-color:red; }
.flexBox {
display:flex;
flex-wrap:wrap;
}
#media all and ( min-width: 768px ) {
.flexBox .a { order:1; }
.flexBox .b { order:3; }
.flexBox .c { order:2; }
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="flexBox">
<div class="a col-xs-12 col-md-7">Search</div>
<div class="b col-xs-12 ">Directory</div>
<div class="c col-xs-12 col-md-5">Preferences</div>
</div>
</div>
</div>
this can become very confusing... just make a separate div for phone screens if you are reordering the structure on different screens. Use the hidden-(screen-size) in Bootstrap 3. The bootstrap push and pull works horizontally but not vertical realignment (that I know of). Hopefully this is correct below.. I have been using other things now for this and it has been a while.
<div class="container">
<div class="hidden-sm row">
<div class="col-xs-12 a">Search</div>
<div class="col-xs-12 b">Directory</div>
<div class="col-xs-12 c">Preferences</div>
</div>
<div class="hidden-xs row">
<div class="col-sm-7 a">Search</div>
<div class="col-sm-5 c">Preferences</div>
<div class="col-sm-12 b">Directory</div>
</div>
</div>
Instead using the push and pull, i used hidden and visible. Hope the code below helps.
<div class="row">
<div class="col-xs-12 col-md-6 a">Search</div>
<div class="col-xs-12 col-md-6 visible-md visible-lg c">Preferences</div>
<div class="col-xs-12 b">Directory</div>
<div class="col-xs-12 hidden-md hidden-lg c">Preferences</div>
</div>