Bootstrap Grid System
In this chapter, we will explain the Bootstrap Grid System.
Bootstrap provides a responsive, mobile-first fluid grid system that automatically divides into up to 12 columns as the screen or viewport size increases.
What is a Grid?
From Wikipedia:
In graphic design, a grid is a structure (usually two-dimensional) made up of a series of intersecting straight lines (vertical and horizontal) used to organize content. It is widely used in print design for layout and content structure. In web design, it is a method for quickly creating consistent layouts and using HTML and CSS effectively.
Simply put, grids in web design are used to organize content, make websites easy to browse, and reduce the load on the client side.
What is the Bootstrap Grid System?
The Bootstrap official documentation describes the grid system as follows:
Bootstrap includes a responsive, mobile-first, non-fixed grid system that appropriately expands to 12 columns as the device or viewport size increases. It includes predefined classes for simple layout options and powerful mixin classes for generating more semantic layouts.
Let's understand the above statement. Bootstrap 3 is mobile-first, in the sense that the Bootstrap code starts with small-screen devices (such as mobile phones and tablets) and then scales up to components and grids on large-screen devices (such as laptops and desktops).
Mobile-First Strategy
- Content
- Decide what is most important.
- Layout
- Design for smaller widths first.
- The base CSS is mobile-first, and media queries target tablets and desktops.
- Progressive Enhancement
- Add elements as the screen size increases.
Responsive grid system: as the screen or viewport size increases, the system automatically divides into up to 12 columns.
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 4 | 4 | 4 | |||||||||
| 4 | 8 | ||||||||||
| 6 | 6 | ||||||||||
| 12 |
How the Bootstrap Grid System Works
The grid system creates page layouts through a series of rows and columns that contain content. The following lists how the Bootstrap grid system works:
- Rows must be placed within a .container class to get proper alignment and padding.
- Use rows to create horizontal groups of columns.
- Content should be placed within columns, and only columns can be immediate children of rows.
- Predefined grid classes, such as .row and .col-xs-4, can be used to quickly create grid layouts. LESS mixin classes can be used for more semantic layouts.
- Columns create gutters between column content through padding. This padding is created through .rows negative margins on rows, offsetting the first and last columns.
- The grid system is created by specifying the twelve available columns you want to span. For example, to create three equal columns, use three .col-xs-4。
Media query
Media queries are quite nifty "conditional CSS rules". They only apply to some CSS based on certain specified conditions. If those conditions are met, the corresponding styles are applied.
Media queries in Bootstrap allow you to move, show, and hide content based on viewport size. The following media queries are used in the LESS files to create the key breakpoint thresholds in the Bootstrap grid system.
/* Small devices (phones, less than 768px) */
/* Bootstrap does not have media queries by default */
/* Small devices (tablets, 768px and up) */
@media (min-width: @screen-sm-min) { ... }
/* Medium devices (desktop computers, 992px and up) */
@media (min-width: @screen-md-min) { ... }
/* Large devices (desktop computers, 1200px and up) */
@media (min-width: @screen-lg-min) { ... }
We sometimes also include max-widthin media query code to limit the CSS effect to a smaller range of screen sizes.
@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) { ... }
A media query has two parts: first a device specification, then a size rule. In the case above, the following rules are set:
Let's look at the following line of code:
@media (min-width: @screen-sm-min) and (max-width: @screen-sm-max) { ... }
For all devices with min-width: @screen-sm-min , if the screen width is less than @screen-sm-max, some processing will be performed.
Grid Options
The following table summarizes how the Bootstrap grid system works across multiple devices:
| 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- |
| Number of columns | 12 | 12 | 12 | 12 |
| Min 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
The following is the basic structure of a Bootstrap grid:
...
....
Let's look at a few simple grid examples:
- Example: Stacked Horizontal
- Example: Medium and Large Devices
- Example: Phones, Tablets, and Desktops
Responsive Column Resets
The following example contains 4 grids, but when viewing on small devices, we cannot determine where the grids will be displayed.
To solve this problem, you can use .clearfix class and responsive utilitiesto solve it, as shown in the example below:
Example
<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
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
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
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
p
<
p
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut.
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
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
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
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
p
div
div
div
Try it »
Resize the browser window to see the changes, or view the effect on your phone.
Offsetting Columns
Offsets are a useful feature for more professional layouts. They can be used to give columns more space. For example,.col-xs-* classes do not support offsets, but you can achieve the same effect simply by using an empty cell.
To use offsets on large-screen displays, use .col-md-offset-* classes. These classes increase the left margin of a column by ***** columns, where ***** ranges from 1 to 11。
In the following example, we have
..
, and we will use .col-md-offset-3 class to center this div.
Example
<div class="container">
<h1>
Hello, world!
h1
<div class="row">
<div class=">
col-md-6 col-md-offset-3
"
style
=
"
background-color: #dedef8;box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;
"
<
p
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
p
div
div
div
Try it »
The result is as follows:
Nesting Columns
To nest the default grid within content, add a new .row, and within an existing .col-md-* column, add a set of .col-md-* columns. The nested row should contain a set of columns whose count must not exceed 12 (in fact, you are not required to fill all 12 columns).
In the example below, the layout has two columns, and the second column is divided into two rows of four boxes.
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
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
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 - Divided into Four 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
Consectetur art party Tonx culpa semiotics. Pinterest assumenda minim organic quis.
p
div
<div class="col-md-6" style=">
background-color: #B18904; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;
"
<
p
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
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
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
p
div
<div class="col-md-6" style=">
background-color: #B18904; box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;
"
<
p
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim.
p
div
div
div
div
div
Try it »
The result is as follows:
Column ordering
Another great feature of the Bootstrap grid system is that you can easily write columns in one order and display them in another.
You can easily change the order of built-in grid columns with .col-md-push-* and .col-md-pull-* classes, where ***** ranges from 1 to 11。
In the example below, we have a two-column layout, with a narrow left column as a sidebar. We will use .col-md-push-* and .col-md-pull-* classes to swap the order of these two columns.
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 the 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 the right
div
div
<br>
<div class="row">
<p>
After sorting
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 am on the 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 am on the right
div
div
div
Try it »
The result is as follows:
In graphic design, a grid is a structure (usually two-dimensional) made up of a series of intersecting straight lines (vertical and horizontal) used to organize content. It is widely used in print design for layout and content structure. In web design, it is a method for quickly creating consistent layouts and using HTML and CSS effectively.