🏗️ Bootstrap Grid System: Building Websites Like LEGO Blocks
The Big Idea
Imagine you have a big empty shelf. You want to organize your toys on it. You could put everything randomly… OR you could use special boxes that snap together perfectly!
That’s what Bootstrap Grid is! It’s a magic system that helps you arrange things on a webpage—like putting toys in perfectly organized boxes on a shelf.
🎪 Our Story: The Magic Bookshelf
Once upon a time, there was a librarian named Greta the Grid Master. She had a very special bookshelf that was exactly 12 invisible slots wide.
“Why 12?” asked a curious visitor.
“Because 12 can be divided many ways!” Greta smiled. “Into 2, 3, 4, 6, or 12 equal parts!”
Let’s learn how Greta organizes her magical bookshelf!
📦 The Grid System
The Grid System is Bootstrap’s way of organizing content on a page.
Think of it like a waffle maker:
- The waffle maker has a grid of squares
- Each square is the same size
- You can fill squares however you want!
<div class="container">
<!-- Everything goes
inside here! -->
</div>
The container is your waffle maker. It holds everything together and keeps it centered on the page.
graph TD A[🖥️ Your Webpage] --> B[📦 Container] B --> C[Row 1] B --> D[Row 2] B --> E[Row 3]
📏 Rows: The Horizontal Lines
Rows are like shelves in a bookcase.
Each shelf (row) goes across the page from left to right. You stack books (content) on each shelf.
<div class="container">
<div class="row">
<!-- Shelf 1: stuff here -->
</div>
<div class="row">
<!-- Shelf 2: stuff here -->
</div>
</div>
✨ Key Rule
Always put your columns INSIDE a row. Rows are the home for columns!
Think of it this way:
- Container = The bookcase
- Row = A shelf
- Columns = The dividers on that shelf
📊 Columns: Dividing the Space
Remember Greta’s 12 invisible slots? Those are columns!
Every row has 12 invisible columns. You decide how many each item takes up.
<div class="row">
<div class="col">
I take some space!
</div>
<div class="col">
I take some space too!
</div>
</div>
When you use col without a number, Bootstrap shares the space equally among all columns.
graph TD A[Row = 12 slots total] --> B[Col 1] A --> C[Col 2] A --> D[Col 3] B --> E[Takes 4 slots] C --> F[Takes 4 slots] D --> G[Takes 4 slots]
🎯 Column Auto-sizing
Sometimes you want a column to be only as wide as its content.
Like a rubber band that wraps tightly around whatever’s inside!
<div class="row">
<div class="col-auto">
Short text
</div>
<div class="col">
This takes the
remaining space!
</div>
</div>
| Class | Behavior |
|---|---|
col-auto |
Shrinks to fit content |
col |
Expands to fill remaining |
Real Example:
- A small label + a big input field
- The label is
col-auto(small) - The input is
col(fills the rest)
⚖️ Column Equal Width
Want all columns to be the exact same size? Easy!
Just use col on each one without numbers.
<div class="row">
<div class="col">Box 1</div>
<div class="col">Box 2</div>
<div class="col">Box 3</div>
</div>
Bootstrap does the math for you:
- 3 columns? Each gets 4 slots (12 ÷ 3 = 4)
- 4 columns? Each gets 3 slots (12 ÷ 3 = 3)
- 2 columns? Each gets 6 slots (12 ÷ 2 = 6)
🍕 Pizza Slicing
| Columns | Slots Each | Like… |
|---|---|---|
| 2 | 6 each | Half pizza |
| 3 | 4 each | Third pizza |
| 4 | 3 each | Quarter pizza |
| 6 | 2 each | Sixth pizza |
| 12 | 1 each | Tiny slices! |
📐 Column Specific Width
Want exact control? Tell Bootstrap exactly how many of the 12 slots each column should take!
<div class="row">
<div class="col-8">
I'm wider! (8 slots)
</div>
<div class="col-4">
I'm smaller (4 slots)
</div>
</div>
The numbers must add up to 12 or less!
Common Layouts
| Layout | Code |
|---|---|
| Sidebar + Content | col-3 + col-9 |
| Two equal halves | col-6 + col-6 |
| Three equal parts | col-4 + col-4 + col-4 |
| Hero + Two small | col-6 + col-3 + col-3 |
graph LR A[col-3<br>Sidebar] --> B[col-9<br>Main Content]
⚠️ What if numbers add up to more than 12?
Extra columns wrap to the next line! Like books that don’t fit on a shelf—they go to the shelf below.
📱 Column Responsive Classes
This is the MAGIC part!
Your website looks different on phones vs. computers. Responsive classes let you change the layout based on screen size!
The Breakpoints (Screen Sizes)
| Class | Screen Width | Device |
|---|---|---|
col- |
< 576px | 📱 Tiny phones |
col-sm- |
≥ 576px | 📱 Phones |
col-md- |
≥ 768px | 📲 Tablets |
col-lg- |
≥ 992px | 💻 Laptops |
col-xl- |
≥ 1200px | 🖥️ Desktops |
col-xxl- |
≥ 1400px | 🖥️ Big screens |
Example: Stack on Phone, Side-by-Side on Desktop
<div class="row">
<div class="col-12 col-md-6">
Full width on phone,
Half on tablet+
</div>
<div class="col-12 col-md-6">
Full width on phone,
Half on tablet+
</div>
</div>
What happens:
- 📱 Phone: Each box takes 12 slots = full width = stacked
- 📲 Tablet+: Each box takes 6 slots = half width = side by side
graph TD subgraph Phone A[Box 1 - Full Width] B[Box 2 - Full Width] end subgraph Tablet C[Box 1] --- D[Box 2] end
Another Example: 3 Cards
<div class="row">
<div class="col-12 col-sm-6 col-lg-4">
Card 1
</div>
<div class="col-12 col-sm-6 col-lg-4">
Card 2
</div>
<div class="col-12 col-sm-6 col-lg-4">
Card 3
</div>
</div>
| Screen | Each Card | Result |
|---|---|---|
| 📱 Tiny | 12 slots | 1 per row (stacked) |
| 📱 Phone | 6 slots | 2 per row |
| 💻 Laptop | 4 slots | 3 per row |
🎨 Putting It All Together
Let’s build a real layout for a blog page!
<div class="container">
<!-- Header Row -->
<div class="row">
<div class="col-12">
<h1>My Blog</h1>
</div>
</div>
<!-- Content Row -->
<div class="row">
<!-- Main Article -->
<div class="col-12 col-lg-8">
<article>...</article>
</div>
<!-- Sidebar -->
<div class="col-12 col-lg-4">
<aside>...</aside>
</div>
</div>
</div>
Result:
- 📱 On phones: Everything stacks nicely
- 💻 On laptops: Article (8 slots) + Sidebar (4 slots) side by side
🏆 Summary: The Golden Rules
- Container wraps everything
- Rows go inside containers
- Columns go inside rows
- Every row has 12 invisible slots
- Use
colfor equal widths - Use
col-autoto shrink to content - Use
col-NUMBERfor exact widths - Use
col-BREAKPOINT-NUMBERfor responsive magic
🚀 You Did It!
You now understand Bootstrap’s Grid System! It’s like having a magic organizing system that:
- ✅ Keeps things neat and aligned
- ✅ Adjusts automatically for different screens
- ✅ Makes building websites FAST
Remember: 12 slots per row. Mix and match. Stack on small screens. Spread out on big screens.
You’re now a Grid Master, just like Greta! 🎉