Introduction: What is Bootstrap and Why Use It?
Bootstrap is the world’s most popular CSS framework for building responsive, mobile-first websites. Created by Twitter developers, it provides a comprehensive collection of pre-built components and utility classes that streamline web development. Bootstrap helps you:
- Build responsive layouts quickly without writing custom CSS
- Maintain consistency across projects with standardized components
- Accelerate development with ready-to-use elements
- Ensure cross-browser compatibility and mobile responsiveness
Core Bootstrap Classes by Category
Container and Grid System
| Class | Purpose | Example |
|---|---|---|
.container | Fixed-width container, centered on page | <div class="container">...</div> |
.container-fluid | Full-width container (100% width) | <div class="container-fluid">...</div> |
.container-{breakpoint} | 100% width until specified breakpoint | <div class="container-lg">...</div> |
.row | Container for columns | <div class="row">...</div> |
.col | Equal-width column | <div class="col">...</div> |
.col-{size} | Specific width column (1-12) | <div class="col-6">...</div> |
.col-{breakpoint}-{size} | Responsive column widths | <div class="col-md-6">...</div> |
Breakpoint Reference
| Breakpoint | Class infix | Dimensions |
|---|---|---|
| Extra small | None | < 576px |
| Small | sm | ≥ 576px |
| Medium | md | ≥ 768px |
| Large | lg | ≥ 992px |
| Extra large | xl | ≥ 1200px |
| Extra extra large | xxl | ≥ 1400px |
Spacing and Sizing
Margin and Padding
- Format:
{property}{sides}-{size} - Property:
m(margin),p(padding) - Sides:
t(top),b(bottom),s(start),e(end),x(horizontal),y(vertical), blank (all sides) - Size:
0to5,auto
| Class | Effect |
|---|---|
mt-3 | margin-top: 1rem |
pb-4 | padding-bottom: 1.5rem |
mx-auto | Center element horizontally |
py-2 | padding-top: 0.5rem; padding-bottom: 0.5rem |
Width and Height
w-{size}:25%,50%,75%,100%,autoh-{size}:25%,50%,75%,100%,automw-100:max-width: 100%mh-100:max-height: 100%
Text Formatting
| Category | Classes | Effect |
|---|---|---|
| Alignment | text-start, text-center, text-end | Text alignment |
| Wrapping | text-wrap, text-nowrap | Text wrapping behavior |
| Weight | fw-bold, fw-normal, fw-light | Font weight |
| Style | fst-italic, fst-normal | Font style |
| Size | fs-1 through fs-6 | Font size |
| Decoration | text-decoration-none, text-decoration-underline | Text decoration |
| Transform | text-uppercase, text-lowercase, text-capitalize | Text case |
Colors and Background
Text Colors
text-primary,text-secondary,text-success,text-dangertext-warning,text-info,text-light,text-dark,text-muted
Background Colors
bg-primary,bg-secondary,bg-success,bg-dangerbg-warning,bg-info,bg-light,bg-dark,bg-transparent
Bootstrap Components Classes
Buttons
Button Styles
btn btn-primary,btn btn-secondary,btn btn-successbtn btn-danger,btn btn-warning,btn btn-infobtn btn-light,btn btn-dark,btn btn-link
Button Sizes
btn-lg,btn-sm
Button States
active,disabled
Button Outline
btn-outline-*(all color variants)
Alerts
alert alert-primary,alert alert-secondary,alert alert-successalert alert-danger,alert alert-warning,alert alert-infoalert alert-light,alert alert-darkalert-dismissiblewithfadeandshowfor dismissible alerts
Forms
Form Controls
form-control– Basic text inputform-control-lg,form-control-sm– Size variantsform-select– Dropdown selectform-check– Checkboxes and radio buttonsform-check-input– Input within form checkform-check-label– Label for form checkform-range– Range inputform-floating– Floating labels
Form Layout
form-label– Label for form controlform-text– Additional help textform-group– Grouping form elementsinput-group– Input with addons
Cards
Basic Card
card– Card containercard-header– Card header sectioncard-body– Main card contentcard-footer– Card footer sectioncard-title– Card titlecard-subtitle– Card subtitlecard-text– Card text contentcard-link– Card linkscard-img-top,card-img-bottom– Card images
Card Sizing and Layout
card-group– Group cards with equal heightcard-deck– Fixed width cards with same heightcard-columns– Masonry-like card grid
Navigation
Navbar
navbar– Basic navbarnavbar-expand-*– Responsive breakpointsnavbar-light,navbar-dark– Color schemesnavbar-brand– Brand elementnavbar-nav– Navigation containernav-item– Navigation itemnav-link– Navigation link
Nav
nav– Basic navigationnav-tabs– Tabbed navigationnav-pills– Pill navigationnav-fill– Full-width nav itemsnav-justified– Equal-width items
Tables
table– Basic tabletable-striped– Striped rowstable-bordered– Bordered tabletable-hover– Hover effect on rowstable-sm– Compact tabletable-responsive– Responsive tabletable-dark– Dark theme table
Utilities
Flex
Flex Container
d-flex,d-inline-flex– Display flexflex-row,flex-row-reverse– Directionflex-column,flex-column-reverse– Column directionflex-wrap,flex-nowrap– Wrappingjustify-content-*– start, end, center, between, around, evenlyalign-items-*– start, end, center, baseline, stretchalign-content-*– start, end, center, between, around, stretch
Flex Items
flex-fill– Force items to equal widthflex-grow-*– 0, 1 – Control item growthflex-shrink-*– 0, 1 – Control item shrinkalign-self-*– start, end, center, baseline, stretch
Display
d-none,d-inline,d-block,d-tabled-{breakpoint}-{value}– Responsive displayd-print-none,d-print-block– Print display
Border
border– Add borderborder-{side}– top, end, bottom, startborder-0– Remove borderborder-{color}– Border colorrounded,rounded-circle,rounded-pill– Border radius
Shadow
shadow-none,shadow-sm,shadow,shadow-lg– Box shadows
Responsive Utility Classes
Visibility
visible,invisible– Visibility controld-none d-md-block– Hide on small, show on medium+d-md-none d-lg-block– Show on small, hide on medium, show on large+
Responsive Spacing
{property}{sides}-{breakpoint}-{size}- Example:
mt-md-3– Apply margin-top on medium screens and up
Common Challenges and Solutions
Vertical Centering
Â
html
<!-- Method 1: Using flex -->
<div class="d-flex align-items-center" style="height: 200px;">
<div>Centered content</div>
</div>
<!-- Method 2: Using position utilities -->
<div class="position-relative" style="height: 200px;">
<div class="position-absolute top-50 start-50 translate-middle">
Centered content
</div>
</div>Equal Height Columns
Â
html
<div class="row">
<div class="col d-flex">
<div class="card flex-fill">
Content
</div>
</div>
<div class="col d-flex">
<div class="card flex-fill">
More content
</div>
</div>
</div>Sticky Header
Â
html
<nav class="navbar sticky-top bg-light">
Sticky navbar
</nav>Best Practices and Tips
- Mobile-First Approach
- Start with base styles for mobile
- Add breakpoint classes as needed for larger screens
- Component Combinations
- Combine utility classes to create custom components
- Example:
class="d-flex justify-content-between align-items-center bg-light p-3 rounded"
- Custom CSS Organization
- Use Bootstrap utilities first before writing custom CSS
- Override variables in a separate Sass file rather than modifying Bootstrap source
- Responsive Images
- Always use
img-fluidclass on images to make them responsive - Consider
img-thumbnailfor bordered images
- Always use
- Performance Tips
- Use Bootstrap’s custom build to include only the components you need
- Load minified CSS and JS files in production
- Consider using the CDN for faster load times
Resources for Further Learning
- Official Documentation
- Learning Resources
- Tools and Extensions
- Bootstrap Icons
- Bootstrap Themes
- Bootswatch – Free themes for Bootstrap
- Community Resources
Remember that Bootstrap is highly customizable. While this cheatsheet covers the most common classes, you can always extend Bootstrap with your own custom styles and components to match your project’s specific needs.
