Bootstrap Tables
Learn how Bootstrap makes HTML tables clean, responsive and easy to style.
What is a Table?
An HTML table is used to display information in rows and columns.
Tables are commonly used for displaying student records, employee details, course information, product lists and other structured data.
A table is like a register where information is arranged in rows and columns so that it becomes easy to read and compare.
Basic HTML Table
Before using Bootstrap, we can create a basic
HTML table using <table>,
<tr>,
<th> and
<td>.
<table>
<tr>
<th>Name</th>
<th>Course</th>
</tr>
<tr>
<td>Rahul</td>
<td>Java Full Stack</td>
</tr>
</table>
Output:
| Name | Course |
|---|---|
| Rahul | Java Full Stack |
Bootstrap Table
Bootstrap provides the
.table class to style an HTML table.
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Course</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
<tr>
<td>Rahul</td>
<td>Java Full Stack</td>
<td>6 Months</td>
</tr>
</tbody>
</table>
Output:
| Name | Course | Duration |
|---|---|---|
| Rahul | Java Full Stack | 6 Months |
| Priya | .NET Full Stack | 6 Months |
Table Borders
The .table-bordered class adds borders
around all table cells.
<table class="table table-bordered">
...
</table>
Output:
| Student | Technology | Status |
|---|---|---|
| Amit | Python | Active |
| Sneha | Data Analytics | Active |
Striped Rows
The .table-striped class adds
alternating background shades to table rows.
<table class="table table-striped">
...
</table>
Output:
| ID | Name | Course |
|---|---|---|
| 101 | Rahul | Java |
| 102 | Priya | Python |
| 103 | Amit | .NET |
Hoverable Rows
The .table-hover class adds a hover
effect when the user moves the mouse over a row.
<table class="table table-hover">
...
</table>
Output:
| Employee | Department | Experience |
|---|---|---|
| Rahul | Development | 2 Years |
| Sneha | Testing | 3 Years |
| Amit | Data Analytics | 1 Year |
Dark Table
Bootstrap provides table color classes that can be used to create different table styles.
<table class="table table-dark">
...
</table>
Output:
| ID | Course | Mode |
|---|---|---|
| 1 | Java Full Stack | Online |
| 2 | .NET Full Stack | Offline |
Table Header Colors
Bootstrap contextual classes can be applied to table headers to highlight important information.
<thead class="table-dark">
<tr>
<th>Name</th>
<th>Course</th>
</tr>
</thead>
Output:
| Name | Course | Status |
|---|---|---|
| Rahul | Java | Active |
| Priya | Python | Active |
Contextual Table Rows
Bootstrap provides contextual classes such as
table-success,
table-warning and
table-danger.
<tr class="table-success">
...
</tr>
<tr class="table-warning">
...
</tr>
<tr class="table-danger">
...
</tr>
Output:
| Student | Result | Status |
|---|---|---|
| Rahul | Excellent | Passed |
| Amit | Average | Review |
| Neha | Low | Needs Improvement |
Responsive Table
Tables can become difficult to read on small screens when there are many columns.
Bootstrap provides
.table-responsive to make tables
horizontally scrollable when required.
<div class="table-responsive">
<table class="table table-bordered">
...
</table>
</div>
Try resizing the browser window. On smaller screens, the table can scroll horizontally instead of breaking the page layout.
Example:
| Student ID | Student Name | Course | Duration | Mode | Location |
|---|---|---|---|---|---|
| 101 | Rahul | Java Full Stack | 6 Months | Offline | Pune |
| 102 | Priya | Python Full Stack | 6 Months | Online | Pune |
Table Sizing
Bootstrap provides the
.table-sm class to create a more
compact table.
<table class="table table-sm">
...
</table>
Output:
| ID | Name | Course |
|---|---|---|
| 1 | Rahul | Java |
| 2 | Priya | Python |
Real-Life Example – CIIT Training Institute
Suppose CIIT Training Institute wants to display student course information.
A Bootstrap table can be used to display:
- Student Name
- Course
- Duration
- Training Mode
- Status
<div class="table-responsive">
<table class="table table-bordered table-hover">
<thead class="table-dark">
<tr>
<th>Student</th>
<th>Course</th>
<th>Duration</th>
<th>Mode</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>Rahul</td>
<td>Java Full Stack</td>
<td>6 Months</td>
<td>Offline</td>
<td>Active</td>
</tr>
</tbody>
</table>
</div>
Output:
| Student | Course | Duration | Mode | Status |
|---|---|---|---|---|
| Rahul | Java Full Stack | 6 Months | Offline | Active |
| Priya | Python Full Stack | 6 Months | Online | Active |
| Amit | .NET Full Stack | 6 Months | Offline | Active |
Important Table Classes
| Class | Purpose |
|---|---|
.table
|
Basic Bootstrap table styling |
.table-bordered
|
Adds borders to table cells |
.table-striped
|
Adds alternating row colors |
.table-hover
|
Adds hover effect to rows |
.table-dark
|
Creates a dark table |
.table-sm
|
Creates a compact table |
.table-responsive
|
Makes tables responsive |
Common Mistakes
-
Forgetting to use the
.tableclass. - Creating tables without proper headings.
-
Not using
.table-responsivefor wide tables. - Adding too many columns on small screens.
- Using unnecessary table styling that makes the information difficult to read.
Summary
In this lesson, we learned how Bootstrap provides predefined classes to create clean and professional HTML tables.
We learned about basic tables, borders, striped rows, hover effects, dark tables, contextual rows, compact tables and responsive tables.
We also learned how .table-responsive
helps tables work better on smaller screens.