Bootstrap5 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.
We can also define the number of columns according to our needs:
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 4 | 4 | 4 | |||||||||
| 4 | 8 | ||||||||||
| 6 | 6 | ||||||||||
| 12 |
Bootstrap 5's grid system is responsive, and columns automatically rearrange according to screen size.
Make sure the sum of columns in each row is equal to or less than 12.
Grid Classes
The Bootstrap 5 grid system has the following 6 classes:
- .col- for all devices.
- .col-sm- tablet - screen width equal to or greater than 576px.
- .col-md- desktop monitor - screen width equal to or greater than 768px.
- .col-lg- large desktop monitor - screen width equal to or greater than 992px.
- .col-xl- extra large desktop monitor - screen width equal to or greater than 1200px.
- .col-xxl- super large desktop monitor - screen width equal to or greater than 1400px.
Grid System Rules
Bootstrap5 Grid System Rules:
- Each row of the grid needs to be placed in a container with the
.container(fixed width) or.container-fluid(full width) class set, so that some margins and padding are automatically set. - Use rows to create horizontal groups of columns.
- Content must be placed in columns, and only columns can be direct children of a row.
- Predefined classes such as .row and .col-sm-4 can be used to quickly create grid layouts.
- Columns create gaps between column content through padding. This gap is created using negative margins on the .rows class to offset the first and last columns.
- Grid columns are created by spanning the specified 12 columns. For example, to set three equal columns, you need to use three .col-sm-4.
- Bootstrap 5 and Bootstrap 4 use flexbox (flexible box) instead of floats. A big advantage of Flexbox is that grid columns without a specified width will automatically be set toequal width and equal height columns . If you want to learn more about Flexbox, you can read our CSS Flexbox Tutorial 。
The following table summarizes how the Bootstrap grid system works across different devices:
| | Extra small devices
<576px | Tablets
≥576px | Desktop monitors
≥768px | Large desktop monitors
≥992px | Extra Large Desktop Monitor
≥1200px | Super Large Desktop Monitor
≥1400px | | --- | --- | --- | --- | --- | | Max container width | None (auto) | 540px | 720px | 960px | 1140px | 1320px | | Class prefix | .col- | .col-sm- | .col-md- | .col-lg- | .col-xl- | .col-xxl- | | Number of columns | 12 | | Gap width | 1.5rem (.75rem on each side of a column) | | Nestable | Yes | | Column ordering | Yes |
We use the above classes in combination to create more flexible page layouts.
**Tip:** Each class is scaled up proportionally, so if you want sm and md to have the same width, you only need to specify sm.
Basic Structure of the Bootstrap 5 Grid
The following code is the basic structure of a Bootstrap 5 grid:
Bootstrap5 Grid Basic Structure
<div class="row">
<div class="col--" div div>
<div class="row">
<div class="col--" div>
<div class="col--" div>
<div class="col--" div div>
<div class="row">
<div class="col" div>
<div class="col" div>
<div class="col" div div>First example: Create a row (
Second example: instead of adding a number to each col , let bootstrap handle the layout automatically, so each column in the same row has equal width: two "col" , each 50% wide. Three **"col"** each 33.33% wide, four **"col"** each 25% wide, and so on. Likewise, you can use .col-sm|md|lg|xl to set the responsive rules for the columns.
Next, let's look at some examples.
Creating Equal-width Columns, Bootstrap Auto Layout
Example
<div class="row">
<div class="col" .col div>
<div class="col" .col div>
<div class="col" .col div div>Try it »
Equal-width Responsive Columns
The following example demonstrates how to create equal-width responsive columns on tablets and larger screens. On mobile devices, i.e. when the screen width is less than 576px, the four columns will be stacked vertically:
Example
<div class="row">
<div class="col-sm-3" .col-sm-3 div>
<div class="col-sm-3" .col-sm-3 div>
<div class="col-sm-3" .col-sm-3 div>
<div class="col-sm-3" .col-sm-3 div div>Try it »
Unequal-width Responsive Columns
The following example demonstrates how to create unequal-width responsive columns on tablets and larger screens. On mobile devices, i.e. when the screen width is less than 576px, the two columns will be stacked vertically:
Example
<div class="row">
<div class="col-sm-4" .col-sm-4 div>
<div class="col-sm-8" .col-sm-8 div div>Try it »
Setting a Column Width
If you only set the width of one column, the other columns will automatically divide the remaining width. The following example sets column widths to 25%, 50%, 25%:
Example
<div class="row">
<div class="col" col div>
<div class="col-6" col-6 div>
<div class="col" col div div>Try it »
Tablets and Desktops
The following example shows two columns each 50% wide on desktop monitors; on tablets the left column is 25% and the right column is 75%; on small devices such as mobile phones they will be stacked.
Example
<div class="container-fluid">
<div class="row">
<div class=">col-sm-3 col-md-6
"
<p RUNOOB p div>
<div class=">col-sm-9 col-md-6
"
<
p
Runoob Tutorial
p
div
div
div
Try it »
Tablets, Desktops, Large Desktop Monitors, Extra-large Desktop Monitors
The following example has width ratios of 25%/75%, 50%/50%, 33.33%/66.67%, 16.67/83.33% on tablets, desktops, large desktop monitors and extra-large desktop monitors respectively; on small devices such as mobile phones they will be stacked.
Example
<div class="row">
<div class=">col-sm-3 col-md-6 col-lg-4 col-xl-2 p-3 bg-primary text-white
"
.col
div
<div class=">col-sm-9 col-md-6 col-lg-8 col-xl-10 p-3 bg-dark text-white
"
.col
div
div
Try it »
Nesting Columns
The following example creates a two-column layout, with another two columns nested inside one of the columns:
Example
<div class="row">
<div class="col-8" .col-8>
<div class="row">
<div class="col-6" .col-6 div>
<div class="col-6" .col-6 div div div>
<div class="col-4" .col-4 div div>Try it »
Display effect:

Offsetting Columns
Offset columns are set via the offset-- class. The first asterisk (*) can be sm、md、lg、xl, indicating the screen device type; the second asterisk (*) can be 1 to 11 a number.
To use offsets on large-screen displays, use .offset-md-* classes. These classes increase the left margin of a column by ***** columns, where ***** ranges from 1 to 11。
For example: .offset-md-4 moves .col-md-4 to the right by four columns.
Example
<div class="row">
<div class="col-md-4" .col-md-4 div>
<div class=">col-md-4 offset-md-4
"
.col-md-4 .offset-md-4
div
div
<div class="row">
<div class=">col-md-3 offset-md-3
"
.col-md-3 .offset-md-3
div
<div class=">col-md-3 offset-md-3
"
.col-md-3 .offset-md-3
div
div
<div class="row">
<div class=">col-md-6 offset-md-3
"
.col-md-6 .offset-md-3
div
div
Try it »