🎯 Bootstrap 5 CSS Grid – Use Native CSS Grid with Bootstrap Classes
🧲 Introduction – What Is Bootstrap 5 CSS Grid?
Bootstrap 5 supports CSS Grid layout as a powerful alternative to its default Flexbox-based grid system. By enabling the CSS Grid feature, you can use modern, 2-dimensional layouts for better alignment control—especially useful for complex UIs, asymmetric grids, or overlapping elements.
🎯 In this guide, you’ll learn:
- How to enable and use Bootstrap’s CSS Grid features
- The difference between CSS Grid and Flexbox grid in Bootstrap
- Code examples with line-by-line explanation
- When to use CSS Grid vs Flexbox grid
🧱 Enabling CSS Grid in Bootstrap 5
To use CSS Grid in Bootstrap 5, you must include the grid
class on a parent element and use col-start
, col-end
, row-start
, and row-end
utilities for positioning.
✅ Example – Basic CSS Grid in Bootstrap
<div class="d-grid gap-3">
<button class="btn btn-primary">Button 1</button>
<button class="btn btn-secondary">Button 2</button>
</div>
🔍 Code Explanation:
d-grid
: Converts the element to a CSS Grid containergap-3
: Adds spacing between grid items- This layout stacks children vertically with consistent spacing
🧮 Grid Template Columns with Utilities
You can define the number of columns with grid-template-columns
utility classes like grid-cols-2
, grid-cols-3
, etc.
✅ Example – Two-Column CSS Grid:
<div class="d-grid grid-template-columns-2 gap-3">
<div class="bg-light p-3">Item 1</div>
<div class="bg-secondary text-white p-3">Item 2</div>
</div>
⚠️ Note: This utility (
grid-template-columns-*
) isn’t native in Bootstrap 5 by default. You’ll need to define it via custom CSS:
.grid-template-columns-2 {
grid-template-columns: repeat(2, 1fr);
}
🔲 Explicit Grid Item Placement
Use utility classes to control how items span or start across grid lines.
✅ Example – Column Span:
<div class="d-grid" style="grid-template-columns: repeat(4, 1fr); gap: 1rem;">
<div class="bg-primary text-white p-3" style="grid-column: span 2;">Span 2</div>
<div class="bg-info text-white p-3">Normal</div>
<div class="bg-success text-white p-3">Normal</div>
</div>
🔍 Code Explanation:
grid-template-columns: repeat(4, 1fr)
: Creates four equal columnsgrid-column: span 2
: Makes the first item span across 2 columns
🔳 Combine Grid with Responsive Classes
Bootstrap doesn’t provide predefined grid-*
breakpoints like Flexbox does, but you can use media queries or mix grid with container and responsive display utilities.
✅ Example – Responsive Grid with Media Query:
<style>
.custom-grid {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
@media (min-width: 768px) {
.custom-grid {
grid-template-columns: 1fr 1fr;
}
}
</style>
<div class="custom-grid">
<div class="bg-warning p-3">Item A</div>
<div class="bg-dark text-white p-3">Item B</div>
</div>
🧮 Bootstrap CSS Grid vs Flexbox Grid
Feature | Flexbox Grid (.row , .col-* ) | CSS Grid (d-grid , custom) |
---|---|---|
Default in Bootstrap | ✅ Yes | ❌ No (opt-in via d-grid ) |
1D vs 2D Layout | 1D (rows or columns only) | 2D (rows + columns) |
Auto-sizing behavior | Built-in | Manual with grid-template-* |
Responsive utilities | Extensive support | Limited without custom utilities |
Complex layout support | Medium | Advanced (overlapping, spanning) |
🛠️ Best Practices for Bootstrap 5 CSS Grid
Tip | Why It Helps |
---|---|
Use d-grid sparingly | Not all components are optimized for grid |
Customize your own grid utilities | Bootstrap doesn’t ship full grid helpers yet |
Use Flexbox grid for simple layouts | Faster, easier to maintain |
Use CSS Grid for advanced positioning | Ideal for overlapping, complex rows/columns |
Test across breakpoints | Ensure layout works responsively |
📌 Summary – Recap & Usage Guidelines
Bootstrap 5 offers support for native CSS Grid layouts using d-grid
and custom grid utilities. While Flexbox remains the default layout system, CSS Grid allows more control in 2D layouts, nested patterns, and overlapping designs.
🔍 Key Takeaways:
- Use
d-grid
to enable CSS Grid layouts in Bootstrap 5 - Combine with
gap-*
and custom CSS for column/row control - CSS Grid is best for advanced layouts not easily handled by Flexbox
- For basic responsive layouts, Flexbox (
.row
,.col-*
) is still preferred
⚙️ CSS Grid in Bootstrap is a power tool—best used when you need precision in layout design beyond what Flexbox offers.
❓ FAQs – Bootstrap 5 CSS Grid
❓ Is CSS Grid enabled by default in Bootstrap 5?
✅ No. You must use the d-grid
class to activate it.
❓ Does Bootstrap 5 provide full CSS Grid utilities?
❌ Not yet. You’ll need to define some custom classes like grid-template-columns-*
.
❓ Can I mix Flexbox and CSS Grid in the same layout?
✅ Yes. Use Flexbox for layout and CSS Grid for internal component design.
❓ When should I use CSS Grid over Flexbox in Bootstrap?
✅ Use CSS Grid when you need 2D control (rows & columns) or overlapping elements.
❓ Is CSS Grid supported in all modern browsers?
✅ Yes. CSS Grid is supported in all major browsers including Chrome, Firefox, Edge, and Safari.
Share Now :