What is a Grid?
Grid in web design organize and structures contents, makes the websites easy to scan and reduces the cognitive loads on users.
What is Bootstrap Grid?
Bootstrap Includes a responsive mobile first fluid grid system that appropriately scales up to 12 columns as the device or viewport size increases. It includes predefined classes for easy layout options, and also powerful mixins for generating more semantic layouts.
Now we will be explaining what the above statement simply means. Bootstrap 4 is a mobile first in the sense that the codes for Bootstrap now begins by targeting smaller screens such as mobile devices, tablets and "expands" components and grids for larger screens like desktops and laptops.
Mobile First Strategy
- Content - This determines what is most important.
- Layout - Design to smaller width first.
- Base CSS address mobile device first; media queries address for tablets, desktops.
- Progressive Enhancement - Add elements as screen size increases.
Working of Bootstrap Grid System
- Rows must be placed within a .container class for proper alignment and padding.
- Use rows to create horizontal groups of columns.
- Content should be placed within the columns, and only columns may be the immediate children of rows.
- Predefined grid classes like .row and .col-xs-4 are available for quick making of grid layouts. LESS mixins can also be used for more semantic layouts.
- Columns create gutters (caps between column contents) by the use of padding. That padding is offset in rows for the first and last column using negative margin on .rows.
- Grid columns are created by specifying the number of twelve available columns that you wish to span. For example, three equal columns would use three .col-xs-4.
Media Queries
Media Queries in Bootstrap allows you to move, show and hide content based on the viewport size. The following media queries below are used in LESS files to create the key breakpoints in Bootstrap grid system.
/* Extra small devices (phones, less than 768px) */ /* No media query since this is the default in Bootstrap */ /* Small devices (tablets, 768px and up) */ @media (min-width: @screen-sm-min) { ... } /* Medium devices (desktops, 992px and up) */ @media (min-width: @screen-md-min) { ... } /* Large devices (large desktops, 1200px and up) */ @media (min-width: @screen-lg-min) { ... }
@media (max-width: @screen-xs-max) { ... } @media (min-width: @screen-sm-min) and (max-width: @screen-sm-max) { ... } @media (min-width: @screen-md-min) and (max-width: @screen-md-max) { ... } @media (min-width: @screen-lg-min) { ... }
Let us consider this line -
@media (min-width: @screen-sm-min) and (max-width: @screen-sm-max) { ... }
Grid Options
Extra small devices Phones (<768px) | Small devices Tablets (≥768px) | Medium devices Desktops (≥992px) | Large devices Desktops (≥1200px) | |
---|---|---|---|---|
Grid behavior | Horizontal at all times | Collapsed to start, horizontal above breakpoints | Collapsed to start, horizontal above breakpoints | Collapsed to start, horizontal above breakpoints |
Max container width | None (auto) | 750px | 970px | 1170px |
Class prefix | .col-xs- | .col-sm- | .col-md- | .col-lg- |
# of columns | 12 | 12 | 12 | 12 |
Max column width | Auto | 60px | 78px | 95px |
Gutter width | 30px (15px on each side of a column) | 30px (15px on each side of a column) | 30px (15px on each side of a column) | 30px (15px on each side of a column) |
Nestable | Yes | Yes | Yes | Yes |
Offsets | Yes | Yes | Yes | Yes |
Column ordering | Yes | Yes | Yes | Yes |
Basic Grid Structure
<div class = "container"> <div class = "row"> <div class = "col-*-*"></div> <div class = "col-*-*"></div> </div> <div class = "row">...</div> </div> <div class = "container"> .... </div>
Responsive Column Resets
<div class = "container"> <div class = "row" > <div class = "col-xs-6 col-sm-3" style = "background-color: #dedef8; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> <p>The pain itself is the love.</p> </div> <div class = "col-xs-6 col-sm-3" style = "background-color: #dedef8; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> <p>The pain itself is fun, the main iterative process, but you do such times as low as fatigue and obesity. Utah For the least I will come, who will drive our school district to work? but to achieve the objectives from that advantage.</p> <p>The pain itself is fun, the main iterative process, but you do as low as candy.</p> </div> <div class = "clearfix visible-xs"></div> <div class = "col-xs-6 col-sm-3" style = "background-color: #dedef8; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> <p>For more information on what to do but to achieve the objectives of employment.</p> </div> <div class = "col-xs-6 col-sm-3" style = "background-color: #dedef8; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> <p>The pain itself is fun, the main iterative process, but you do such times as low as fatigue and obesity. Ut for at least.</p> </div> </div> </div>
Output
The pain itself is the love.
The pain itself is important, but I give it to the pain of some great pain. In order for the least to come, what of ours to exercise any school of labor except to take advantage of the objectives from it to achieve.
The pain itself is a lot of pain, but I give it the time to cut it down.
In order for the least to come, what of ours to exercise any school of labor except to take advantage of the objectives from it to achieve.
The pain itself is important, but I give it to the pain of some great pain. To have at least
Resize your viewport or check it out on your phone for a desired result of the above example.
Offset Columns
To use offsets on large screens, make use of .col-md-offset-* classes. These classes increases the left margin of a column by * columns where * ranges from 1 to 11.
Example
<div class = "container"> <h1>Hello, world!</h1> <div class = "row" > <div class = "col-xs-6 col-md-offset-3" style = "background-color: #dedef8; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> <p>The pain itself is the love.</p> </div> </div> </div>
Output
Hello, world!
The pain itself is the love.
Nesting Columns
Example
<div class = "container"> <h1>Hello, world!</h1> <div class = "row"> <div class = "col-md-3" style = "background-color: #dedef8; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> <h4>First Column</h4> <p>The pain itself is the love.</p> </div> <div class = "col-md-9" style = "background-color: #dedef8; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> <h4>Second Column- Split into 4 boxes</h4> <div class = "row"> <div class = "col-md-6" style = "background-color: #B18904; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> <p>Enhanced art party Tonx com semiotics. Pinterest is taking organic information over email.</p> </div> <div class = "col-md-6" style = "background-color: #B18904; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> <p>but I give them as much time as fatigue and great pain some. For more information on what we do The school district's labor was only to achieve the objectives of it.</p> </div> </div> <div class = "row"> <div class = "col-md-6" style = "background-color: #B18904; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> <p>what is our employment school district's employment objectives please follow it out of it.</p> </div> <div class = "col-md-6" style = "background-color: #B18904; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> <p>The pain itself is love, the main iterative process, but I give them as much time as fatigue and great pain some. For more information on how.</p> </div> </div> </div> </div> </div>
Output
Hello, world!
First Column
The pain itself is the love.
Second Column- Split into 4 boxes
Enhanced art party Tonx com semiotics. Pinterest is taking organic information over email.
but every time I give it as low as toil and to some great pain. In order for the least to come, what of ours to exercise any school of labor except to take advantage of the objectives from it to achieve.
any of us exercise any school of employment.
The pain itself is important, but I give it to the pain of some great pain. For more information on how.
Column Ordering
Example
<div class = "container"> <h1>Hello, world!</h1> <div class = "row"> <p>Before Ordering</p> <div class = "col-md-4" style = "background-color: #dedef8; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> I am on left </div> <div class = "col-md-8" style = "background-color: #dedef8; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> I am on right </div> </div> <br> <div class = "row"> <p>After Ordering</p> <div class = "col-md-4 col-md-push-8" style = "background-color: #dedef8; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> I was on left </div> <div class = "col-md-8 col-md-pull-4" style = "background-color: #dedef8; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;"> I was on right </div> </div> </div>
Output
Hello, world!
Before Ordering
After Ordering
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.