Bootstrap 5 Tutorial
v5.3.0Bootstrap 5 Tutorial
Responsive Tables in Bootstrap 5
Responsive Tables: Make tables work on all screen sizes using Bootstrap's responsive table classes and techniques.
The Problem with Tables on Mobile
Traditional HTML tables don't work well on small screens because they force horizontal scrolling or content wrapping.
Non-Responsive Table Issues
| Issue | Problem | Mobile Impact | Solution |
|---|---|---|---|
| Horizontal Scrolling | Tables wider than screen | Poor user experience | Responsive wrapper |
| Content Overflow | Text gets cut off | Unreadable content | Text wrapping/ellipsis |
| Tiny Text | Font size too small | Strain to read | Responsive font sizes |
| Touch Targets | Buttons/links too small | Hard to tap | Larger touch areas |
| Data Density | Too much information | Overwhelming | Progressive disclosure |
Key Insight: On mobile devices, tables need to adapt their layout, not just shrink in size.
Basic Responsive Tables
Using .table-responsive
The simplest way to make tables responsive is to wrap them in a .table-responsive div.
| # | First Name | Last Name | Username | Phone | Department | Role | |
|---|---|---|---|---|---|---|---|
| 1 | John | Doe | @johndoe | john@example.com | (555) 123-4567 | Engineering | Developer |
| 2 | Jane | Smith | @janesmith | jane@example.com | (555) 987-6543 | Marketing | Manager |
<!-- Basic responsive table -->
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<!-- More columns -->
</tr>
</thead>
<tbody>
<!-- Table rows -->
</tbody>
</table>
</div>How .table-responsive Works
CSS Properties Applied
.table-responsive {
display: block;
width: 100%;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
/* Creates horizontal scrollbar
when table exceeds container width */
/* -webkit-overflow-scrolling: touch;
enables smooth scrolling on iOS */Breakpoint-Specific Responsive
<!-- Responsive only on specific breakpoints --> <!-- Always responsive --> <div class="table-responsive"> <!-- Table content --> </div> <!-- Responsive on small screens and down --> <div class="table-responsive-sm"> <!-- Horizontal scroll on <576px --> </div> <!-- Responsive on medium screens and down --> <div class="table-responsive-md"> <!-- Horizontal scroll on <768px --> </div> <!-- Responsive on large screens and down --> <div class="table-responsive-lg"> <!-- Horizontal scroll on <992px --> </div> <!-- Responsive on extra large screens and down --> <div class="table-responsive-xl"> <!-- Horizontal scroll on <1200px --> </div> <!-- Responsive on extra extra large screens --> <div class="table-responsive-xxl"> <!-- Horizontal scroll on <1400px --> </div>
Breakpoint-Specific Responsive Tables
Responsive at Different Breakpoints
Control when tables become responsive based on screen size.
.table-responsive-sm
| # | Product | Price | Category | Stock |
|---|---|---|---|---|
| 1 | Laptop | $999 | Electronics | 15 |
Horizontal scroll on screens <576px
.table-responsive-md
| # | Product | Price | Category | Stock |
|---|---|---|---|---|
| 1 | Laptop | $999 | Electronics | 15 |
Horizontal scroll on screens <768px
.table-responsive-lg
| # | Product | Price | Category | Stock |
|---|---|---|---|---|
| 1 | Laptop | $999 | Electronics | 15 |
Horizontal scroll on screens <992px
.table-responsive-xl
| # | Product | Price | Category | Stock |
|---|---|---|---|---|
| 1 | Laptop | $999 | Electronics | 15 |
Horizontal scroll on screens <1200px
| Class | Breakpoint | When Horizontal Scroll Appears | Use Case |
|---|---|---|---|
.table-responsive | All screens | Always | Simple tables, always need scroll |
.table-responsive-sm | <576px | Mobile phones | Most mobile-first designs |
.table-responsive-md | <768px | Tablets and phones | Tables with moderate columns |
.table-responsive-lg | <992px | Tablets and smaller | Wide tables on desktop |
.table-responsive-xl | <1200px | Small desktops and below | Very wide tables |
.table-responsive-xxl | <1400px | Standard desktops and below | Extra wide data tables |
Advanced Responsive Techniques
Hide Columns on Mobile
| # | Product | Category | Supplier | Price | Stock | Action |
|---|---|---|---|---|---|---|
| 1 | Laptop Pro | Electronics | TechCorp | $1,299 | 15 | |
| 2 | Wireless Mouse | Accessories | PeriPlus | $49 | 42 |
<!-- Hide columns on specific breakpoints -->
<table class="table">
<thead>
<tr>
<th>Always visible</th>
<th class="d-none d-sm-table-cell">
<!-- Hidden on xs, visible on sm and up -->
</th>
<th class="d-none d-md-table-cell">
<!-- Hidden on xs and sm, visible on md and up -->
</th>
<th class="d-none d-lg-table-cell">
<!-- Hidden on xs, sm, md, visible on lg and up -->
</th>
<th class="d-none d-xl-table-cell">
<!-- Hidden until xl -->
</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data</td>
<td class="d-none d-sm-table-cell">Visible on sm+</td>
<td class="d-none d-md-table-cell">Visible on md+</td>
<td class="d-none d-lg-table-cell">Visible on lg+</td>
<td class="d-none d-xl-table-cell">Visible on xl+</td>
</tr>
</tbody>
</table>Responsive Font Sizes
| Header | Adaptive Font | Sizes |
|---|---|---|
| Small text on mobile | Medium on tablet | Large on desktop |
| Consistent readability | Across all devices | With responsive fonts |
Font Size Breakpoints
<!-- Responsive font sizes in tables -->
<th class="fs-6 fs-md-5 fs-lg-4">
<!--
fs-6: 1rem on all screens (default)
fs-md-5: 1.25rem on medium and up
fs-lg-4: 1.5rem on large and up
-->
</th>
<td class="fs-6 fs-lg-5">
<!--
fs-6: 1rem on all screens
fs-lg-5: 1.25rem on large and up
-->
</td>
<!-- Bootstrap font size classes -->
.fs-1 { font-size: 2.5rem !important; } /* Largest */
.fs-2 { font-size: 2rem !important; }
.fs-3 { font-size: 1.75rem !important; }
.fs-4 { font-size: 1.5rem !important; }
.fs-5 { font-size: 1.25rem !important; }
.fs-6 { font-size: 1rem !important; } /* Smallest */Card-Based Tables for Mobile
Transform Tables to Cards on Mobile
For complex tables, consider transforming them into card layouts on mobile devices.
Desktop View (Table)
| Product | Price | Category | Stock | Actions |
|---|---|---|---|---|
| Laptop Pro | $1,299 | Electronics | 15 | |
| Wireless Mouse | $49 | Accessories | 42 |
Mobile View (Cards)
Laptop Pro
Price: $1,299
Category: Electronics
Stock: 15
Wireless Mouse
Price: $49
Category: Accessories
Stock: 42
<!-- Table on desktop, cards on mobile -->
<div class="d-none d-md-block">
<!-- Desktop table view -->
<div class="table-responsive">
<table class="table">
<!-- Table content for desktop -->
</table>
</div>
</div>
<div class="d-md-none">
<!-- Mobile card view -->
<div class="card mb-3">
<div class="card-body">
<h5 class="card-title">Product Name</h5>
<div class="row">
<div class="col-6">
<strong>Price:</strong> $99
</div>
<div class="col-6">
<strong>Category:</strong> Electronics
</div>
<!-- More fields -->
</div>
<div class="mt-2">
<button class="btn btn-sm btn-primary">View</button>
<button class="btn btn-sm btn-success">Buy</button>
</div>
</div>
</div>
<!-- More cards -->
</div>Stacked Tables (Vertical Layout)
Stacked Table Layout
Transform table rows into stacked blocks on small screens.
| Order # | Customer | Date | Amount | Status |
|---|---|---|---|---|
| ORD-1001 | John Smith | 2023-10-15 | $299.99 | Delivered |
| ORD-1002 | Jane Doe | 2023-10-16 | $149.50 | Shipped |
| ORD-1003 | Bob Johnson | 2023-10-17 | $499.99 | Processing |
How Stacked Tables Work
- Uses
data-labelattributes for mobile labels - Hides table header on mobile
- Displays each cell as a block element
- Shows label before each data point
- Maintains semantic table structure
CSS for Stacked Tables
/* Stacked table styles */
@media (max-width: 767.98px) {
.table-stacked {
border: 0;
}
.table-stacked thead {
display: none;
}
.table-stacked tr {
display: block;
margin-bottom: 1rem;
border: 1px solid #dee2e6;
border-radius: 0.25rem;
}
.table-stacked td {
display: block;
text-align: right;
border: none;
border-bottom: 1px solid #dee2e6;
position: relative;
padding-left: 50%;
}
.table-stacked td:last-child {
border-bottom: 0;
}
.table-stacked td::before {
content: attr(data-label);
position: absolute;
left: 0.75rem;
width: calc(50% - 1.5rem);
padding-right: 0.75rem;
text-align: left;
font-weight: bold;
white-space: nowrap;
}
}Priority-Based Column Hiding
Column Priority System
| # (P1) | Name (P1) | Email (P2) | Phone (P3) | Address (P4) | Action (P1) |
|---|---|---|---|---|---|
| 1 | John Smith | john@example.com | (555) 123-4567 | 123 Main St |
Priority Levels
- Priority 1: Always visible (ID, Name, Actions)
- Priority 2: Hidden on xs screens (Email)
- Priority 3: Hidden on sm screens (Phone)
- Priority 4: Hidden on md screens (Address)
- Priority 5: Hidden on lg screens (Extra details)
Implementation Strategy
Step-by-Step Approach
- Identify essential columns that must always be visible
- Categorize columns by importance (P1 to P5)
- Use responsive display classes to hide non-essential columns
- Test on different screen sizes to ensure readability
- Consider providing a way to show hidden columns if needed
- Document column priorities for maintenance
Code Example
<!-- Priority-based column hiding -->
<table class="table">
<thead>
<tr>
<!-- Priority 1: Always visible -->
<th class="priority-1">ID</th>
<th class="priority-1">Name</th>
<!-- Priority 2: Hide on xs -->
<th class="priority-2 d-none d-sm-table-cell">Email</th>
<!-- Priority 3: Hide on sm -->
<th class="priority-3 d-none d-md-table-cell">Phone</th>
<!-- Priority 4: Hide on md -->
<th class="priority-4 d-none d-lg-table-cell">Address</th>
<!-- Priority 1: Always visible -->
<th class="priority-1">Actions</th>
</tr>
</thead>
</table>Touch-Friendly Tables
Mobile Touch Optimization
| Product | Description | Actions |
|---|---|---|
| Laptop Pro | High-performance laptop with 16GB RAM | |
| Wireless Mouse | Ergonomic mouse with long battery life |
Touch-Friendly Guidelines
- Touch targets: Minimum 44×44 pixels
- Spacing: Adequate space between interactive elements
- Scroll behavior: Smooth, predictable scrolling
- Tap feedback: Visual feedback on touch
- Gesture support: Consider swipe actions
- Zoom prevention: Prevent accidental zoom on double-tap
CSS for Touch Optimization
/* Touch-friendly table styles */
.btn-touch {
min-height: 44px;
min-width: 44px;
padding: 0.75rem 1.5rem;
}
.touch-friendly td {
padding: 1rem 0.75rem;
}
.touch-friendly tr {
border-bottom: 2px solid #dee2e6;
}
/* Prevent text selection on touch */
.touch-friendly {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Better scrolling on iOS */
.table-responsive {
-webkit-overflow-scrolling: touch;
}Performance Considerations
Performance Tips
- Virtual scrolling: For tables with 100+ rows
- Lazy loading: Load data as user scrolls
- Pagination: Split data across multiple pages
- Minimize DOM elements: Avoid deeply nested tables
- Optimize images: Compress and use appropriate sizes
- CSS vs JavaScript: Prefer CSS solutions for responsiveness
- Browser testing: Test performance on different browsers
- Mobile-first: Design for mobile performance first
Testing Checklist
- ✅ Test on actual mobile devices
- ✅ Check horizontal scrolling behavior
- ✅ Verify touch targets are adequate
- ✅ Test with screen readers
- ✅ Check loading performance
- ✅ Test with slow network connections
- ✅ Verify responsive breakpoints work
- ✅ Test printing functionality
- ✅ Check browser compatibility
- ✅ Test with different orientations
Best Practices Summary
Responsive Table Guidelines
- ✅ Always wrap tables in
.table-responsiveor breakpoint-specific variants - ✅ Use priority-based column hiding for complex tables
- ✅ Consider card layouts for mobile when appropriate
- ✅ Ensure touch targets are at least 44×44 pixels
- ✅ Test on actual mobile devices, not just emulators
- ✅ Use semantic HTML and proper accessibility attributes
- ✅ Implement pagination or virtual scrolling for large datasets
Choosing the Right Approach
- Simple tables: Use
.table-responsivewith horizontal scroll - Moderate complexity: Hide non-essential columns on mobile
- Complex tables: Consider card layout or stacked design
- Data-heavy applications: Implement virtual scrolling or pagination
- E-commerce/Product lists: Card layouts work well on mobile
- Admin dashboards: Priority-based column hiding with horizontal scroll