Bootstrap Grids
The Bootstrap grid is a system of rows and columns, built on flexbox, that lets you lay out a page in up to twelve columns and change that layout at different screen widths. You use it whenever content should sit side by side on larger screens but stack neatly on phones.
Containers, rows and columns
Every grid follows the same nesting order:
- A
.container(fixed max width per breakpoint) or.container-fluid(full width) sets the outer bounds and side padding. - A
.rowis a flex container that holds columns and offsets their padding with negative margins. - Columns, such as
.color.col-md-6, are the only direct children of a row and hold your actual content.
Skipping the row and putting columns straight into a container breaks the spacing, because the gutters are designed around that row-column pairing.
Reading the column classes
A class like col-md-4 has three parts: col, a breakpoint (md), and a span out of twelve (4). It means "from the medium breakpoint upward, take four of twelve columns". Below that breakpoint the column falls back to full width.
| Infix | Applies from width |
|---|---|
| (none) | 0 and up |
| sm | 576px |
| md | 768px |
| lg | 992px |
| xl | 1200px |
| xxl | 1400px |
Bootstrap is mobile first, so a breakpoint class affects that size and everything larger. You combine classes to describe several layouts at once.
<div class="container">
<div class="row g-3">
<div class="col-12 col-md-6 col-lg-4">Card one</div>
<div class="col-12 col-md-6 col-lg-4">Card two</div>
<div class="col-12 col-md-12 col-lg-4">Card three</div>
</div>
</div>
On a phone, each card takes the full width. From 768px, the first two sit side by side and the third fills the next line. From 992px, all three share one line.
Auto-width columns and gutters
Plain .col without a number divides the row equally, so three .col elements each take a third. .col-auto sizes a column to its content. You can mix them: a fixed .col-3 sidebar next to a .col that absorbs the remaining space.
Gutters are the gaps between columns. In Bootstrap 5 you control them on the row with g-* (both directions), gx-* (horizontal) and gy-* (vertical), from 0 to 5. The row-cols-* classes, such as row-cols-md-3, set how many columns appear per line without adding a class to each child.
Common grid mistakes
- Spans adding to more than twelve. Columns that total more than twelve in one row wrap onto a new line. Sometimes that is what you want, but often it is an accident.
- Styling columns directly. Adding borders or backgrounds to the column element makes the padding look uneven. Put a card or inner
divinside the column and style that. - Using old classes. Bootstrap 4 helpers like
no-guttersand the-xs-infix do not exist in Bootstrap 5. Useg-0and the infix-freecol-*instead.
Nesting a grid
You can place a new .row inside a column to split it further. The nested row again counts to twelve, relative to its parent column, not the whole page.
The habit to build: start with the phone layout using col-12 or plain col, then add md and lg classes only where the layout should change.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.