Bootstrap 4 – Grid and Columns
In Bootstrap, the screen is divided into 12 columns, regardless of the screen size. Even a small screen on an old phone will be split into 12 columns, and Bootstrap will manage them. Let’s see how the Bootstrap grid system works on different devices with different sizes. A total of 5 size levels are supported.Extra small <576px |
Small ≥576px |
Medium ≥768px |
Large ≥992px |
Extra large ≥1200px |
|
---|---|---|---|---|---|
Maximum container width | None (auto) | 540px | 720px | 960px | 1140px |
Class prefix | .col- |
.col-sm- |
.col-md- |
.col-lg- |
.col-xl- |
Number of columns | 12 | ||||
Margin width | 30px (auto 15px on each side of columns) |
Automatic Column Placement – Equal Column Width
To set equal column widths in a container, use the basic class `col-sm`.Cell 1 of 2
Cell 2 of 2
Cell 1 of 3
Cell 2 of 3
Cell 3 of 3
Cell 1 of 2
Cell 2 of 2
Cell 1 of 3
Cell 2 of 3
Cell 3 of 3
When we don’t specify exact viewport sizes, it is divided equally among all cells. Each level starts with the minimum viewport size (col-sm) and automatically applies to larger devices unless overridden.
Three columns of different widths will display on devices with a screen width of 720px or more.
Different Column Sizes in a Row
There are 12 columns in a row. We can assign how many columns a column will occupy.Automatic Layout
.col-md-3
.col-md-6
.col-md-3
Column with Adaptive Width
1 of 3
Column with adaptive width
3 of 3
1 of 3
Column with adaptive width
3 of 3
Display on Different Devices: Mobile and Desktop
The Bootstrap v4 grid system has five levels of classes: xs (very small, this class suffix is not used), sm (small), md (medium), lg (large), and xl (extra large). You can use virtually any combination of these classes to create more dynamic and flexible layouts. Each level of classes is scalable, which means that if you plan to set the same width for md, lg, and xl, you only need to specify md..col-6 .col-md-8
.col-6 .col-md-4
.col-12 .col-md-8
.col-12 .col-md-4
.col-6 .col-md-4
.col-6 .col-md-4
.col-6 .col-md-4
Display on Mobile, Tablet, and Desktop
If we want to control the display on each device, with a different layout for mobile, tablet, and large desktop monitors, we need to specify the column size for each device..col-sm-6 .col-lg-8
.col-sm-6 .col-lg-4
Reverse Example:
<div class="col-12 col-sm-12 col-md-4 col-lg-8 col-xl-8">Text</div>The cell on mobile devices up to 576px (col-) and 768px (col-sm-) will occupy 12 columns, i.e., the entire row – col-12. On devices larger than 768px and up to 992px, the cell will occupy 4 columns (col-md-4), and on devices larger than 992px (col-lg-) and over 1200px (col-xl-), the cell will occupy 8 columns. This code can be shortened without losing its meaning to the following:
<div class="col-12 col-md-4 col-lg-8">Text</div>