Bootstrap 5 is known for its responsive grid system, which allows developers to create layouts that adapt to different screen sizes. One essential feature of this system is the extra small breakpoint (xs
), designed for screens smaller than 576px.
In this post, we’ll explore how to use the extra small grid in Bootstrap 5 to create layouts that look great on small devices like smartphones.
What is the Extra Small Breakpoint?
The extra small breakpoint (xs
) in Bootstrap 5 is automatically applied to all elements unless a larger breakpoint is explicitly defined. It’s perfect for building layouts tailored to smaller screens, such as mobile devices.
Key Characteristics:
- Targets devices with screen widths less than 576px.
- Does not require a specific class (e.g.,
.col-xs-*
is unnecessary). - Default for all
.col-*
classes.
Using the Extra Small Grid in Bootstrap 5
The extra small grid behaves like a fallback. If you don’t define breakpoints for larger screens, the layout will automatically stack on extra small devices. Here’s how it works:
Basic Example: Extra Small Columns
<div class="container">
<div class="row">
<div class="col">Column 1</div>
<div class="col">Column 2</div>
<div class="col">Column 3</div>
</div>
</div>
Output:
- On devices smaller than 576px, the columns stack vertically.
- On larger devices, the columns remain horizontal unless you specify other breakpoints.
Explicitly Targeting the Extra Small Grid
Although you don’t need a specific xs
class in Bootstrap 5, you can create custom layouts for extra small screens by using the .col-*
class and avoiding other breakpoint-specific classes (sm
, md
, etc.).
Example: Full-Width Columns on Extra Small Screens
<div class="container">
<div class="row">
<div class="col-12 bg-primary text-white p-3">Column 1</div>
<div class="col-12 bg-secondary text-white p-3">Column 2</div>
<div class="col-12 bg-success text-white p-3">Column 3</div>
</div>
</div>
Output:
- Columns occupy the full width on all screen sizes unless larger breakpoints are added.
Combining Extra Small with Other Breakpoints
For truly responsive designs, you can combine extra small classes (col-*
) with other breakpoints like sm
, md
, etc., to define how columns behave across different screen sizes.
Example: Extra Small Stacked, Horizontal on Larger Screens
<div class="container">
<div class="row">
<div class="col-12 col-md-6 bg-info text-white p-3">Column 1</div>
<div class="col-12 col-md-6 bg-warning text-white p-3">Column 2</div>
</div>
</div>
Explanation:
- Extra Small (
col-12
): Columns stack vertically on screens smaller than 576px. - Medium (
col-md-6
): Columns become horizontal (side-by-side) on screens 768px and above.
Hiding and Showing Content on Extra Small Screens
Bootstrap’s utility classes allow you to control visibility for extra small devices. Use .d-*
classes to hide or show content.
Example: Hiding Content on Extra Small Screens
<div class="container">
<div class="row">
<div class="col d-none d-md-block bg-light p-3">Visible on Medium and Larger Screens</div>
<div class="col bg-dark text-white p-3">Visible on All Screens</div>
</div>
</div>
Key Classes:
.d-none
: Hides the column by default..d-md-block
: Displays the column on medium screens and above.
Adding Spacing in Extra Small Grids
Bootstrap 5 provides responsive utility classes for margins and paddings that adapt to screen sizes.
Example: Adding Spacing to Extra Small Columns
<div class="container">
<div class="row g-2">
<div class="col bg-primary text-white p-3">Column 1</div>
<div class="col bg-secondary text-white p-3">Column 2</div>
</div>
</div>
Explanation:
g-2
: Adds a small gutter (spacing) between the columns.
Use Cases for Extra Small Grids
The extra small grid system is perfect for mobile-first design. Here are some common use cases:
- Mobile Navigation: Stack menu items vertically for easy navigation on small screens.
- Product Listings: Display items in a single column on mobile devices.
- Forms: Make form inputs stack for better usability on smartphones.
- Hero Sections: Show full-width hero sections or banners on smaller screens.
FAQs About Bootstrap 5 Extra Small Grid
1. Do I need a specific xs
class for extra small grids?
No, the .col-*
classes automatically apply to extra small screens unless overridden by larger breakpoints.
2. How do I ensure columns stack only on extra small devices?
Use .col-12
for extra small devices and larger breakpoint classes (col-sm-*
, col-md-*
, etc.) to customize layouts for larger screens.
3. Can I mix extra small columns with larger breakpoints?
Yes! Combining .col-*
with other breakpoint-specific classes like .col-md-*
creates responsive designs.
Conclusion
The extra small breakpoint in Bootstrap 5 is the foundation of responsive, mobile-first design. By using the default .col-*
classes, you can create layouts that adapt seamlessly to small devices without any extra effort.