Bootstrap 5 Tutorial
v5.3.0Bootstrap 5 Tutorial
Bootstrap 5 Grid System
How the Grid Works
Containers
Provide a means to center your site's contents.
.containerRows
Horizontal groups of columns that ensure columns line up properly.
.rowColumns
Content goes inside columns. Bootstrap uses a 12-column system.
.col-*Basic Grid Structure
Simple Grid Example
<div class="container">
<div class="row">
<div class="col-md-4">Column 1 (33%)</div>
<div class="col-md-4">Column 2 (33%)</div>
<div class="col-md-4">Column 3 (33%)</div>
</div>
</div>Grid Columns Explained
Column Classes Breakdown
| Class Prefix | Breakpoint | Example | Width |
|---|---|---|---|
col- | Extra small (xs) <576px | col-6 | 6/12 = 50% width |
col-sm- | Small (sm) ≥576px | col-sm-4 | 4/12 = 33.33% width |
col-md- | Medium (md) ≥768px | col-md-3 | 3/12 = 25% width |
col-lg- | Large (lg) ≥992px | col-lg-2 | 2/12 = 16.66% width |
col-xl- | X-Large (xl) ≥1200px | col-xl-1 | 1/12 = 8.33% width |
col-xxl- | XX-Large (xxl) ≥1400px | col-xxl-1 | 1/12 = 8.33% width |
Grid Examples
Example 1: Equal Width Columns
<div class="row">
<div class="col">Equal width</div>
<div class="col">Equal width</div>
<div class="col">Equal width</div>
</div>.col without a number automatically sizes columns to equal width.Example 2: Specified Column Widths
<div class="row">
<div class="col-8">8 columns (66.66%)</div>
<div class="col-4">4 columns (33.33%)</div>
</div>Example 3: Responsive Columns
<div class="row">
<div class="col-12 col-md-8">
Full mobile, 8-col desktop
</div>
<div class="col-12 col-md-4">
Full mobile, 4-col desktop
</div>
</div>Grid Breakpoints in Action
Responsive Grid Behavior
<div class="row">
<div class="col-12 col-sm-6 col-md-4 col-lg-3 col-xl-2">Item</div>
<div class="col-12 col-sm-6 col-md-4 col-lg-3 col-xl-2">Item</div>
<!-- More items -->
</div>| Screen Size | Class Applied | Columns per Row | Visual |
|---|---|---|---|
| Mobile (xs) <576px | col-12 | 1 column | 📱 (Stacked) |
| Small (sm) ≥576px | col-sm-6 | 2 columns | 📱📱 (2 per row) |
| Medium (md) ≥768px | col-md-4 | 3 columns | 💻💻💻 (3 per row) |
| Large (lg) ≥992px | col-lg-3 | 4 columns | 🖥️🖥️🖥️🖥️ (4 per row) |
| X-Large (xl) ≥1200px | col-xl-2 | 6 columns | 🖥️🖥️🖥️🖥️🖥️🖥️ (6 per row) |
Row and Column Features
Gutters (Gaps)
Horizontal and vertical spacing between columns.
Gutter Classes:
g-0- No guttersg-1tog-5- Gutter sizesgy-*- Vertical gutters onlygx-*- Horizontal gutters only
Vertical Alignment
Align columns vertically within a row.
Alignment Classes:
align-items-startalign-items-centeralign-items-endalign-self-*(for individual columns)
Horizontal Alignment
Align columns horizontally within a row.
Justify Classes:
justify-content-startjustify-content-centerjustify-content-endjustify-content-betweenjustify-content-around
Column Wrapping
Columns will wrap to the next line when they don't fit.
9 + 4 = 13 columns, so the second column wraps to the next line.
Grid Offset and Ordering
Offset Columns
Move columns to the right by adding offset classes.
<div class="row">
<div class="col-md-4 offset-md-4">
<!-- This column will be centered -->
</div>
</div>Offset classes: offset-*, offset-sm-*, offset-md-*, etc.
Column Ordering
Change visual order without changing HTML structure.
<div class="row">
<div class="col order-2">Appears second</div>
<div class="col order-1">Appears first</div>
</div>Practical Grid Layouts
Common Layout Patterns
1. Sidebar Layout
Sidebar
Navigation menu
Main Content
Primary content area
2. Card Grid
Card 1
Card content
Card 2
Card content
Card 3
Card content
3. Hero Section
Hero Title
Hero description text
Grid Best Practices
Tips for Effective Grid Usage
- Always wrap columns in a
.row - Rows should be placed inside a
.containeror.container-fluid - Use responsive breakpoints for mobile-first design
- Keep total column units ≤ 12 per row
- Use
g-*classes for consistent spacing - Test your grid on different screen sizes
- Use utility classes for alignment and spacing
Common Grid Mistakes
- ❌ Forgetting the
.rowwrapper - ❌ Using columns outside of containers
- ❌ Not considering mobile breakpoints
- ❌ Overusing nested grids unnecessarily
- ❌ Ignoring column wrapping behavior