Bootstrap responsive tables
Responsive Tables in Bootstrap
Responsive tables in Bootstrap are designed to adapt to different screen sizes, ensuring that tables remain usable and visually appealing on smaller devices. Bootstrap provides classes that help tables handle overflow and maintain readability on various screen sizes.
1. Basic Responsive Table
To make a table responsive, you wrap it in a container with the .table-responsive
class. This container enables horizontal scrolling on smaller screens, allowing users to scroll through the table horizontally if its content overflows the viewport.
Class: .table-responsive
Usage:
Wrap your table with a div
element and apply the .table-responsive
class to this div
.
Example:
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>John</td>
<td>Doe</td>
<td>@johndoe</td>
<td>john.doe@example.com</td>
<td>(555) 123-4567</td>
<td>123 Main St</td>
<td>Springfield</td>
<td>IL</td>
<td>62701</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jane</td>
<td>Smith</td>
<td>@janesmith</td>
<td>jane.smith@example.com</td>
<td>(555) 987-6543</td>
<td>456 Oak St</td>
<td>Springfield</td>
<td>IL</td>
<td>62702</td>
</tr>
<!-- More rows -->
</tbody>
</table>
</div>
.table-responsive
: Applies horizontal scrolling to the table, ensuring that it fits within the viewport and allowing users to scroll horizontally if needed.
2. Responsive Breakpoints
Bootstrap also provides breakpoint-specific responsive classes that apply different styles based on the screen size. This allows for more granular control over table responsiveness.
Class Variants:
.table-responsive-sm
: Adds horizontal scrolling for small screens and up (≥576px)..table-responsive-md
: Adds horizontal scrolling for medium screens and up (≥768px)..table-responsive-lg
: Adds horizontal scrolling for large screens and up (≥992px)..table-responsive-xl
: Adds horizontal scrolling for extra-large screens and up (≥1200px)..table-responsive-xxl
: Adds horizontal scrolling for extra-extra-large screens and up (≥1400px).
Example:
<div class="table-responsive-md">
<table class="table">
<!-- Table content -->
</table>
</div>
.table-responsive-md
: Makes the table responsive starting from medium screens (≥768px) and adds horizontal scrolling if necessary.
3. Customizing Table Styles
While making a table responsive, you may also want to customize its appearance or behavior based on specific requirements.
Custom Scrollbars:
You can customize the scrollbar style using CSS if needed, to better fit your design.
Example:
.table-responsive {
max-height: 400px; /* Adjust as needed */
overflow-y: auto;
}
max-height
: Limits the height of the table container, enabling vertical scrolling if the content exceeds the height.
4. Practical Considerations
- Content Management: For tables with a lot of columns or data, ensure that the content is still readable and usable on smaller screens.
- Design Adjustments: Consider simplifying or summarizing the table data for mobile users to improve readability and user experience.
Summary of Responsive Tables in Bootstrap
- Basic Responsive Table: Use
.table-responsive
to enable horizontal scrolling for tables on smaller screens. - Responsive Breakpoints: Use
.table-responsive-sm
,.table-responsive-md
,.table-responsive-lg
,.table-responsive-xl
, or.table-responsive-xxl
for breakpoint-specific responsiveness. - Customizing Table Styles: Customize scrollbars and table container height using CSS as needed.