Bootstrap Guide
This is a practical cheat sheet for using Bootstrap effectively in real projectsβfocused on when to use components, how to use them, and best practices.
π― What is Bootstrap?
Bootstrap is a CSS framework that provides:
- Prebuilt UI components (buttons, cards, navbars)
- Utility classes (spacing, colors, layout)
- Responsive grid system
It helps you build interfaces faster without writing everything from scratch.
π§ When to Use Bootstrap
Use Bootstrap when:
- You want fast UI development
- You need responsive design
- You donβt want to write custom CSS for everything
Avoid overusing Bootstrap when:
- You need highly custom animations
- You want a fully unique design system
π¦ Core Concepts
1. Container Systemβ
<div class="container">
Content here
</div>
### Variants:
- container β fixed width
- container-fluid β full width
---
## 2. Grid System
Bootstrap uses a 12-column layout.
```bash
<div class="row">
<div class="col-md-6">Left</div>
<div class="col-md-6">Right</div>
</div>
Breakpoints:
- col-sm-* β small devices
- col-md-* β tablets
- col-lg-* β desktops
π¨ Buttons (VERY IMPORTANT)
Basic Syntax
<button class="btn btn-primary">Click Me</button>
Button Types (When to Use)β
| Class | Color | Use Case |
|---|---|---|
btn-primary | Blue | Main action (Save, Submit) |
btn-secondary | Gray | Secondary action |
btn-success | Green | Confirm, Approve |
btn-danger | Red | Delete, Remove |
btn-warning | Yellow | Edit, Warning |
btn-info | Light Blue | Info, View |
btn-light | White | Neutral UI |
btn-dark | Black | Strong contrast |
Examplesβ
Primary Actionβ
<button class="btn btn-primary">
Save Changes
</button>
Delete Actionβ
<button class="btn btn-danger">
Delete
</button>
Edit Actionβ
<button class="btn btn-warning">
Edit
</button>
Button Sizesβ
btn-lg β large
btn-sm β small
Example:
```bash
<button class="btn btn-primary btn-sm">Small Button</button>
π¨ Colors (Utilities)
Bootstrap includes color utility classes:
Text Colorsβ
text-primary
text-success
text-danger
text-warning
text-info
text-muted
Background Colorsβ
bg-primary
bg-success
bg-danger
bg-warning
bg-info
bg-light
bg-dark
Exampleβ
<div class="bg-primary text-white p-3">
Important Section
</div>
π Spacing System
Bootstrap uses a spacing scale:
Format:
m = margin
p = padding
t = top
b = bottom
s = start (left)
e = end (right)
x = left/right
y = top/bottom
Examplesβ
mt-3 β margin-top mb-4 β margin-bottom px-5 β padding left/right py-3 β padding top/bottom
π§± Common Componentsβ
Cardsβ
<div class="card">
<div class="card-body">
Content here
</div>
</div>
Navbarβ
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
Navbar content
</nav>
Alertsβ
<div class="alert alert-success">
Success message
</div>
π‘ Best Practicesβ
- β Use Bootstrap for layout first
- β Only add custom CSS for branding
- β Keep spacing consistent (py-5, mb-4)
- β Use button colors consistently (donβt mix meanings)
- β Prefer utility classes over custom CSS
π Quick Mental Modelβ
Think like this:
- btn-primary β main action
- btn-danger β destructive action
- btn-warning β edit/change
- btn-success β confirm/complete
π Summaryβ
Bootstrap helps you:
- Build faster
- Stay consistent
- Avoid repetitive CSS
But the key is using it consistently and intentionally, not randomly.