Boostrap 4 grid re-order - css

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>

Related

How to order divs from 1 column to 3 differents columns in different order using bootstrap

The objective is to re-order the divs of a column when changing the size of the screen in 2 or 3 different columns and, with a different order.(*SEE THE IMAGE HERE *)With no big empty space between the divs besides the margins.
<div class="container-fluid">
<div class="row">
<div class="col-md-5 order-md-2 offset-md-7">**A**</div>
<div class="col-md-7 order-md-1">**B**</div>
<div class="order-md-5">**C**</div>
<div class="col-md-5 order-md-4 offset-md-7">**D**</div>
<div class="col-md-5 order-md-3 offset-md-5">**E**</div>
<div class="order-md-6">**F**</div>
</div>
</div>
I could put the things in order but there's a space (height) between A and E that I cannot join. Is this the correct way or there is another way?
I suppose if you tell me the way I can figure it out the way to do the 3rd column on my own.
Here in the image, the example; divs in 1 col need to have a different order and be in 3 columns
You need jquery to swap div content of column B and C
order class for column name D and E
Dont forget to reload the page when you resize to mobile screen in order to see the change on mobile devices because I am using jquery to live detect the mobile screen
Here is the link of jsfiddle: https://jsfiddle.net/ng9Lhfa5/
If you look at the live jsfiddle demo and you resize your screen upto mobile just press shift+enter so you will get to know the col has switch their position
$(document).ready(function() {
// Get the inner HTML (Column B and Column C in this case)
var x = document.getElementById("movefrom1").innerHTML
var y = document.getElementById("movefrom2").innerHTML
if (window.innerWidth <= 768) { // Check the browser's width
// Move the element to where you want (moveto id)
document.getElementById("b").innerHTML = x;
document.getElementById("c").innerHTML = y;
// Replace the element by an empty string.
document.getElementById("movefrom1").innerHTML = ""
document.getElementById("movefrom2").innerHTML = ""
}
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<div class="container-fluid text-center text-white">
<div class="row bg-primary">
<div class="col-lg-4 col-md-6 p-md-5" id="movefrom1">
Column-B
</div>
<div class="col-lg-4 col-md-6 order-0">
<div class="row ">
<div class="col-12 p-5">Column A</div>
<div class="col-12 p-md-0 p-5" id="b"></div>
<div class="col-12 p-md-0 p-5" id="c"></div>
<div class="col-12 p-5 order-md-4 order-5">Column E</div>
<div class="col-12 p-5 order-md-5 order-4">Column D</div>
</div>
</div>
<div class="col-lg-4">
<div class="row">
<div class="col-12 bg-dark p-md-5" id="movefrom2">Column C</div>
<div class="col-12 bg-secondary p-5">Column F</div>
</div>
</div>
</div>
</div>

Bootstrap 4 layout, grid [duplicate]

This question already has answers here:
Make a div span two rows in a grid
(2 answers)
Closed 3 years ago.
I'm trying to make a layout like on the attached picture, so the column on the left span over multiple rows . At the moment my code looks like:
<div class="row my-row">
<div class="col-6 my-col">1</div>
<div class="col-6 my-col">
<div class="row my-row">
<div class="col-lg-12 my-col">2</div>
<div class="col-lg-12 my-col">3</div>
</div>
</div>
</div>
Is it any other/better way to do it?
As well I don't really understand how this code does what it does, so would be nice if someone can explain?
Thanks.
You're doing it correctly for the most part. Bootstrap applies CSS styles to components on your page based on the class name of the component. In order to get items aligned hoizontally across the page, bootstrap uses the "row" class to create a vertical row. Inside of the "row" element, bootstrap sizes elements that are tagged with "col-#" based on the weight of the # following col. col-12 is the maximum width of the container, therefore you can create up to 12 columns (12 col-1's). In your example col-6 is a column that takes up half the page. In order to split the right half of that column further, an additional row is applied, and two more columns are created. These two columns should be col-6 in order to get each column to take exactly half of that row.
After these components you just need one more row and to get 1/3 of a row you will need three col-4 elements to create the last row. (4+4+4 = col-12 = full row)
Furthermore, Bootstrap not has the ability to automatically detect the size of a column (if you want them all equal). To take advantage of this, you don't need to put any number after col, just provide an element with a class of col inside a row and bootstrap will auto-size them to be equally spaced columns.
Bootstrap has extensive documentation for more help!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<section>
<div class="container">
<div class="row">
<div class="col-12">
<div class="row">
<div class="col-md-4 col-lg-4 col-12 col-sm-6 d-flex align-items-center">
1
</div>
<div class="col-md-8 col-lg-8 col-12 col-sm-6">
<div class="row">
<div class="col-md-6 col-sm-6 col-12 col-lg-6 d-flex align-items-center">
2
</div>
<div class="col-md-6 col-sm-6 col-12 col-lg-6 d-flex align-items-center">
3
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6 col-12 col-lg-6 d-flex align-items-center">
4
</div>
<div class="col-md-6 col-sm-6 col-12 col-lg-6 d-flex align-items-center">
5
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-lg-4 col-sm-4 col-12 d-flex align-items-center">
6
</div>
<div class="col-md-4 col-lg-4 col-sm-4 col-12 d-flex align-items-center">
7
</div>
<div class="col-md-4 col-lg-4 col-sm-4 col-12 d-flex align-items-center">
8
</div>
</div>
</div>
</section>
</body>
</html>
Try this out! It'll also vertically align your contents as you've showed on the image.
For reference go through https://getbootstrap.com/docs/4.0/utilities/flex/ this link. Also if you want it to be centered horizontally use .justify-content-center
id use something like this
<div class="row">
<div class="col-4">
1
</div>
<div class="col-8">
<div class="row">
<div class="col-6">
2
</div>
<div class="col-6">
3
</div>
<div class="col-6">
4
</div>
<div class="col-6">
5
</div>
</div>
</div>
<div class="col-4">
6
</div>
<div class="col-4">
7
</div>
<div class="col-4">
8
</div>
</div>
The trick is to use rows withing another column that's already there. This will let you have 2-> 5 within what looks like a column from 1.
6->8 can just be 3 col-4's in one row / the same row

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

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

Where to place bootstrap row class

Im fairly new to bootstrap and the concept of responsive design.
I was reading over the bootstrap docs and following some tutorials on w3schools. All mentions col- must = 12 in a single row class. They also mention you can combine different col classes and sizes example <div class="col-md-3 col-xs-6">
What I am not getting is when should you break the viewport with the </row> class when you combine different col sizes ?
Consider the following, where I want a mobile device to display 2 rows and 2 columns and on desktop a single column with 4 rows
<div class="container">
<div class="row">
<div class="col-md-3 col-xs-6">
</div>
<div class="col-md-3 col-xs-6">
</div>
<div class="col-md-3 col-xs-6">
</div>
<div class="col-md-3 col-xs-6">
</div>
</div><!--/row -->
</div><!--/container -->
Considering all columns inside rows must = 12, the row class would need to be closed on different points for mobile and desktop...?
How would I tackle the above problem, hope the question makes sense.
Thank you
Your code is correct and doesn't need more .rows. The W3schools tutorial is misleading, and it's wrong to say ".col-*-* should always add up to 12 for each row".
It's ok to have more (or less) than 12 column units in a Bootstrap .row. It's known as column wrapping, and will simply make the extra columns wrap to the next line...
From the Bootstrap docs:
"If more than 12 columns are placed within a single row, each group of
extra columns will, as one unit, wrap onto a new line"
That's why there are examples in the Bootstrap docs that demonstrate using more than 12 columns in a single .row. When using column wrapping you do need to be aware of responsive resets (known as "clearfix") if the columns vary in height.
There are many responsive scenarios (like your example) where it's necessary to have column units exceeding 12 in a single .row element. It's a common misconception that column units must be 12 or less in a single .row.
Similar Questions..
Bootstrap what will happen if I put more than 12 columns in a row?
Bootstrap 3 - row can I have columns add up to more then 12?
Just change the "col-md-3" class to "col-md-12" in all divs to show 4 rows and single column on desktop and two rows and two columns on mobile.
<div class="container">
<div class="row">
<div class="col-md-12 col-xs-6">
</div>
<div class="col-md-12 col-xs-6">
</div>
<div class="col-md-12 col-xs-6">
</div>
<div class="col-md-12 col-xs-6">
</div>
</div><!--/row -->
</div><!--/container -->
The .row class is not required inside a .container, but it is a good idea to include it anyways when you start incase you want multiple rows later on.
All that .row really does is make sure that all of the divs inside of it appear on their own line, separated from the previous and the following .rows
It's just proper nesting..the problem that you have mentioned in your question can be solved in the following way--
As you can see a mobile device will display 2 rows and 2 columns and on desktop a single column will have 4 rows--
Although it can be achieved using many other div nesting methods, I have shown only one such configuration to achieve your desired layout.
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style type="text/css">
.btn-xs {
display: inline-block;
margin-right: 2%;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xs-6 col-sm-6 col-lg-12 col-md-12">
AAAAAAA
</div>
<div class="col-xs-6 col-sm-6 col-lg-12 col-md-12">
BBBBBBBBBB
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-lg-12 col-md-12">
CCCCCCCCCCC
</div>
<div class="col-xs-6 col-sm-6 col-lg-12 col-md-12">
DDDDDDDDDDD
</div>
</div><!--/row -->
</div><!--/container -->
</div>
</body>
</html>

Bootstrap 3 second column collapse.. third column float right

In the code below for Bootstrap 3, I am attempting to get the 3rd column to not stack. I want it to always stay top right just as the left most column stays top left. Is there a simple way? I thought about just excluding that section from bootstrap and just apply css to float right, but surely in Bootstrap, I can do this.
<header id="t3-header" class="container t3-header">
<div class="row">
<!-- LOGO -->
<div class="col-xs-12 col-md-2 logo"> I stay always top left</div>
<div class="col-xs-12 col-md-8"> I should stackunder 1st column as browser resizes </div>
<!-- I want this column to not stack below column 2 as the browser resizes. -->
<div class="col-xs-12 col-md-2">
I should always stay top right
<div class="row">
<div class="col-lg-6"> </div>
<div class="col-lg-6"> </div>
</div>
</div>
</div>
If I've understood you correctly, you've got to make the "top right" div, appear first in the cascade. To do this, first it as the first column, then you can use pull-left and pull-right helper classes to trick the first two columns:
Secondly, you could remove the col-xs-12 alike in the example:
<header id="t3-header" class="container t3-header">
<div class="row">
<div class="col-md-2 pull-right">
I should always stay top right
<div class="row">
<div class="col-lg-6"></div>
<div class="col-lg-6"></div>
</div>
</div>
<!-- LOGO -->
<div class="col-sm-12 col-md-2 logo pull-left">
I stay always top left
</div>
<div class="col-sm-12 col-md-8 pull-left">
I should stackunder 1st column as browser resizes
</div>
</div>
</div>
Example: http://www.bootply.com/ee3E67pujY

Resources