1. What is Responsive Design? Why Bootstrap5?¶
We now use more and more devices: mobile phones, tablets, laptops, desktops… with screen sizes ranging from a few inches to dozens of inches. Responsive Design ensures web pages automatically adjust their layout based on the device’s screen width, providing a good browsing experience on any device.
As a popular front-end framework, Bootstrap5 helps us quickly achieve responsive effects through preset breakpoints and components, eliminating the need to write complex CSS media queries from scratch.
2. Understanding Breakpoints: Bootstrap5’s “Screen Dividers”¶
Breakpoints are the “critical points” where web page layouts switch. When the screen width reaches or exceeds a breakpoint, the web page style adjusts automatically.
Bootstrap5 provides the following default breakpoints (unit: px):
| Breakpoint Name | Screen Width | Use Case |
|---|---|---|
xs |
<576px |
Extra small screens (portrait mobile) |
sm |
≥576px |
Small screens (landscape mobile/tablet) |
md |
≥768px |
Medium screens (tablet) |
lg |
≥992px |
Large screens (laptop) |
xl |
≥1200px |
Extra large screens (desktop) |
xxl |
≥1400px |
XXL screens (large monitors) |
3. Controlling Styles with Breakpoints: Bootstrap5’s “Responsive Switches”¶
Bootstrap5 enables responsive style control through responsive utility classes or the grid system, combined with breakpoint prefixes (e.g., sm-, md-).
1. Show/Hide Elements (Responsive Utility Classes)¶
.d-none: Always hides an element..d-*: Shows an element at a specific breakpoint, formatted as.d-<breakpoint>-<display-type>(e.g.,.d-sm-block).<display-type>:block(block),inline(inline),flex(flex), etc.
Example:
Hide a button on mobile portrait (<576px) and show it on medium screens (≥768px):
<button class="btn btn-primary d-sm-none d-md-block">
Hidden on mobile, visible above medium screens
</button>
2. Grid System: “Responsive Skeleton” for Multi-Column Layouts¶
Bootstrap5’s grid system uses rows (.row) and columns (.col-*) to create responsive layouts. Column widths are controlled by breakpoint prefixes, formatted as .col-<breakpoint>-<number-of-columns>.
Default Logic: Columns are summed from left to right, with a total of 12 columns (e.g., col-md-6 means occupying 6 columns on medium screens and above, i.e., half the screen).
Example: Single column on mobile, double column on tablet, triple column on desktop
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-6 col-lg-4">
<!-- First column: 12 columns on mobile, 6 on tablet, 4 on desktop -->
<p>Layout: 1 column (mobile), 2 columns (tablet), 3 columns (desktop)</p>
</div>
<div class="col-sm-12 col-md-6 col-lg-4">
<!-- Second column -->
<p>Same logic</p>
</div>
<div class="col-sm-12 col-md-6 col-lg-4">
<!-- Third column -->
<p>Same logic</p>
</div>
</div>
</div>
4. Multi-Device Adaptation Practical: From Mobile to Desktop¶
1. Mobile-First: Single-Column Layout¶
Mobile screens are narrow; prioritize single-column (col-sm-12) for readability.
Example: Simple text layout
<div class="row">
<div class="col-sm-12 bg-light p-3">
<h5>Single Column on Mobile</h5>
<p>Content fits the full width of the mobile screen</p>
</div>
</div>
2. Tablet: Double-Column Layout¶
On medium screens (≥768px, md breakpoint), switch to a two-column layout (col-md-6).
Example: Tablet double-column layout
<div class="row">
<div class="col-md-6 bg-light p-3">
<h5>Left Column (Tablet)</h5>
</div>
<div class="col-md-6 bg-light p-3">
<h5>Right Column (Tablet)</h5>
</div>
</div>
3. Desktop: Multi-Column Layout¶
On large screens (≥992px, lg breakpoint), expand to a multi-column layout (e.g., three columns: col-lg-4).
4. Image Adaptation: img-fluid Class¶
Images tend to overflow on small screens. Use .img-fluid to ensure images always match the parent container’s width while scaling proportionally:
<img src="example.jpg" class="img-fluid" alt="Responsive image">
5. Navigation Bar: Mobile “Hamburger Menu”¶
Hide text and show a hamburger menu on mobile, while displaying the full navigation on large screens:
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="#">Logo</a>
<!-- Hamburger button (only visible on mobile) -->
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Navigation items (collapsed on mobile, expanded on large screens) -->
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" href="#">Home</a></li>
<li class="nav-item"><a class="nav-link" href="#">Products</a></li>
<li class="nav-item"><a class="nav-link" href="#">About Us</a></li>
</ul>
</div>
</div>
</nav>
5. Advanced: Custom Breakpoints (Optional)¶
If default breakpoints don’t meet needs (e.g., finer screen divisions), customize via CSS variables:
:root {
--bs-breakpoint-sm: 600px; /* Adjust small screen to 600px */
--bs-breakpoint-md: 800px; /* Adjust medium screen to 800px */
}
6. Summary¶
Bootstrap5 transforms responsive design from “complex CSS” to “simple combinations” using breakpoints and utility classes. Key steps:
1. Use .col-<breakpoint>-<columns> for grid layouts.
2. Use .d-<breakpoint>-<display-type> to control element visibility.
3. Use .img-fluid for image adaptation and navbar for mobile navigation collapse.
Practice more: Modify breakpoint prefixes and column counts to master layout logic across devices.
Tip: For in-depth learning, visit the Bootstrap5 Official Documentation for details.