Bootstrap 5 Tutorial

v5.3.0

Bootstrap 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
IssueProblemMobile ImpactSolution
Horizontal ScrollingTables wider than screenPoor user experienceResponsive wrapper
Content OverflowText gets cut offUnreadable contentText wrapping/ellipsis
Tiny TextFont size too smallStrain to readResponsive font sizes
Touch TargetsButtons/links too smallHard to tapLarger touch areas
Data DensityToo much informationOverwhelmingProgressive 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 NameLast NameUsernameEmailPhoneDepartmentRole
1JohnDoe@johndoejohn@example.com(555) 123-4567EngineeringDeveloper
2JaneSmith@janesmithjane@example.com(555) 987-6543MarketingManager
<!-- 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
#ProductPriceCategoryStock
1Laptop$999Electronics15

Horizontal scroll on screens <576px

.table-responsive-md
#ProductPriceCategoryStock
1Laptop$999Electronics15

Horizontal scroll on screens <768px

.table-responsive-lg
#ProductPriceCategoryStock
1Laptop$999Electronics15

Horizontal scroll on screens <992px

.table-responsive-xl
#ProductPriceCategoryStock
1Laptop$999Electronics15

Horizontal scroll on screens <1200px

ClassBreakpointWhen Horizontal Scroll AppearsUse Case
.table-responsiveAll screensAlwaysSimple tables, always need scroll
.table-responsive-sm<576pxMobile phonesMost mobile-first designs
.table-responsive-md<768pxTablets and phonesTables with moderate columns
.table-responsive-lg<992pxTablets and smallerWide tables on desktop
.table-responsive-xl<1200pxSmall desktops and belowVery wide tables
.table-responsive-xxl<1400pxStandard desktops and belowExtra wide data tables

Advanced Responsive Techniques

Hide Columns on Mobile
#ProductCategorySupplierPriceStockAction
1Laptop ProElectronicsTechCorp$1,29915
2Wireless MouseAccessoriesPeriPlus$4942
<!-- 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
HeaderAdaptive FontSizes
Small text on mobileMedium on tabletLarge on desktop
Consistent readabilityAcross all devicesWith 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)
ProductPriceCategoryStockActions
Laptop Pro$1,299Electronics15
Wireless Mouse$49Accessories42
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 #CustomerDateAmountStatus
ORD-1001John Smith2023-10-15$299.99Delivered
ORD-1002Jane Doe2023-10-16$149.50Shipped
ORD-1003Bob Johnson2023-10-17$499.99Processing
How Stacked Tables Work
  • Uses data-label attributes 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)
1John Smithjohn@example.com(555) 123-4567123 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
  1. Identify essential columns that must always be visible
  2. Categorize columns by importance (P1 to P5)
  3. Use responsive display classes to hide non-essential columns
  4. Test on different screen sizes to ensure readability
  5. Consider providing a way to show hidden columns if needed
  6. 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
ProductDescriptionActions
Laptop ProHigh-performance laptop with 16GB RAM
Wireless MouseErgonomic 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-responsive or 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-responsive with 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