Tables
Source: https://getbootstrap.com/docs/6.0/content/tables
Documentation and examples for opt-in styling of tables (given their prevalent use in JavaScript plugins) with Bootstrap.
Overview
Due to the widespread use of <table> elements across third-party widgets like calendars and date pickers, Bootstrap’s tables are opt-in. Add the base class .table to any <table>, then extend with our optional modifier classes or custom styles. All table styles are not inherited in Bootstrap, meaning any nested tables can be styled independent from the parent.
Using the most basic table markup, here’s how .table-based tables look in Bootstrap.
Variants
Use theme-specific color classes to style tables, table rows, or individual cells. These adapt to color modes as well.
<Example code={[
<table class="table">
<thead>
<tr>
<th scope="col">Class</th>
<th scope="col">Heading</th>
<th scope="col">Heading</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Default</th>
<td>Cell</td>
<td>Cell</td>
</tr>,
...getData('theme-colors').map((themeColor) => <tr class="theme-${themeColor.name}">
<th scope="row">${themeColor.title}</th>
<td>Cell</td>
<td>Cell</td>
</tr>),
</tbody>
</table>
]} customMarkup={[
<!-- On tables -->,
...getData('theme-colors').map((themeColor) => <table class="table theme-${themeColor.name}">...</table>),
`
<!-- On rows -->,
...getData('theme-colors').map((themeColor) =>...),
\n\n
<!-- On cells (`td` or `th`) -->
<tr>,
...getData('theme-colors').map((themeColor) => ...),`
]} />
Accented tables
Striped rows
Use .table-striped to add zebra-striping to any table row within the <tbody>.
Striped columns
Use .table-striped-columns to add zebra-striping to any table column.
These classes can also be added to table variants:
Hoverable rows
Add .table-hover to enable a hover state on table rows within a <tbody>.
These hoverable rows can also be combined with the striped rows variant:
Active tables
Highlight a table row or cell by adding a .table-active class.
<Example code={<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr class="table-active">
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>John</td>
<td>Doe</td>
<td class="table-active">@social</td>
</tr>
</tbody>
</table>} customMarkup={<table class="table">
<thead>
...
</thead>
<tbody>
<tr class="table-active">
...
</tr>
<tr>
...
</tr>
<tr>
<th scope="row">3</th>
<td>John</td>
<td>Doe</td>
<td class="table-active">@social</td>
</tr>
</tbody>
</table>} />
<Example code={<table class="table" data-bs-theme="dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr class="table-active">
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>John</td>
<td>Doe</td>
<td class="table-active">@social</td>
</tr>
</tbody>
</table>} customMarkup={<table class="table" data-bs-theme="dark">
<thead>
...
</thead>
<tbody>
<tr class="table-active">
...
</tr>
<tr>
...
</tr>
<tr>
<th scope="row">3</th>
<td>John</td>
<td>Doe</td>
<td class="table-active">@social</td>
</tr>
</tbody>
</table>} />
How do the variants and accented tables work?
For the accented tables (striped rows, striped columns, hoverable rows, and active tables), we used some techniques to make these effects work for all our table variants:
-
We start by setting the background of a table cell with the
--bs-table-bgcustom property. All table variants then set that custom property to colorize the table cells. This way, we don’t get into trouble if semi-transparent colors are used as table backgrounds. -
Then we add an inset box shadow on the table cells with
box-shadow: inset 0 0 0 9999px var(--bs-table-bg-state, var(--bs-table-bg-type, var(--bs-table-accent-bg)));to layer on top of any specifiedbackground-color. It uses custom cascade to override thebox-shadow, regardless the CSS specificity. Because we use a huge spread and no blur, the color will be monotone. Since--bs-table-accent-bgis set totransparentby default, we don’t have a default box shadow. -
When either
.table-striped,.table-striped-columns,.table-hoveror.table-activeclasses are added, either--bs-table-bg-typeor--bs-table-bg-state(by default set toinitial) are set to a semitransparent color (--bs-table-striped-bg,--bs-table-active-bgor--bs-table-hover-bg) to colorize the background and override default--bs-table-accent-bg. -
For each table variant, we set
--bs-table-bg-typeor--bs-table-bg-stateto a semi-transparent color derived from the current theme color. Those values are layered in the inset box shadow, while--bs-table-accent-bgremains a transparent fallback. -
Text and border colors are generated the same way, and their colors are inherited by default.
Behind the scenes it looks like this:
Table borders
Bordered tables
Add .table-bordered for borders on all sides of the table and cells.
Border color utilities can be added to change colors:
Tables without borders
Add .table-borderless for a table without borders.
Card-like tables
Need a bordered, rounded table without nesting it in a card? Wrap the table and clip the corners.
With utilities
Compose border, border radius, and overflow utilities. Drop the table’s bottom margin with .mb-0.
<table class="table table-striped mb-0">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>John</td>
<td>Doe</td>
<td>@social</td>
</tr>
</tbody>
</table>
</div>} customMarkup={<div class="border rounded overflow-hidden">
<table class="table table-striped mb-0">
...
</table>
</div>`} />
With .table-card
Use .table-card for the same look. It rounds corners like a card, clips cell backgrounds, and removes the last row’s bottom border so it does not stack on the wrapper.
<table class="table table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>John</td>
<td>Doe</td>
<td>@social</td>
</tr>
</tbody>
</table>
</div>} customMarkup={<div class="table-card">
<table class="table table-hover">
...
</table>
</div>`} />
Combine it with a responsive wrapper when the table can overflow:
<table class="table table-striped"> <thead> <tr> <th scope="col">#</th> <th scope="col">Heading</th> <th scope="col">Heading</th> <th scope="col">Heading</th> <th scope="col">Heading</th> <th scope="col">Heading</th> <th scope="col">Heading</th> <th scope="col">Heading</th> <th scope="col">Heading</th> </tr> </thead> <tbody> <tr> <th scope="row">1</th> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <th scope="row">2</th> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <th scope="row">3</th> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> </tbody> </table> </div>`} />
Small tables
Add .table-sm to make any .table more compact by cutting all cell padding in half.
Table group dividers
Add a thicker border, darker between table groups—<thead>, <tbody>, and <tfoot>—with .table-group-divider. Customize the color by changing the border-top-color (which we don’t currently provide a utility class for at this time).
<thead> <tr> <th scope="col">#</th> <th scope="col">First</th> <th scope="col">Last</th> <th scope="col">Handle</th> </tr> </thead> <tbody class="table-group-divider"> <tr> <th scope="row">1</th> <td>Mark</td> <td>Otto</td> <td>@mdo</td> </tr> <tr> <th scope="row">2</th> <td>Jacob</td> <td>Thornton</td> <td>@fat</td> </tr> <tr> <th scope="row">3</th> <td>John</td> <td>Doe</td> <td>@social</td> </tr> </tbody> </table>`} />
Vertical alignment
Table cells of <thead> are always vertical aligned to the bottom. Table cells in <tbody> inherit their alignment from <table> and are aligned to the top by default. Use the vertical align classes to re-align where needed.
<Example code={<div class="table-responsive">
<table class="table align-middle">
<thead>
<tr>
<th scope="col" class="w-25">Heading 1</th>
<th scope="col" class="w-25">Heading 2</th>
<th scope="col" class="w-25">Heading 3</th>
<th scope="col" class="w-25">Heading 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>This cell inherits <code>vertical-align: middle;</code> from the table</td>
<td>This cell inherits <code>vertical-align: middle;</code> from the table</td>
<td>This cell inherits <code>vertical-align: middle;</code> from the table</td>
<td>This here is some placeholder text, intended to take up quite a bit of vertical space, to demonstrate how the vertical alignment works in the preceding cells.</td>
</tr>
<tr class="align-bottom">
<td>This cell inherits <code>vertical-align: bottom;</code> from the table row</td>
<td>This cell inherits <code>vertical-align: bottom;</code> from the table row</td>
<td>This cell inherits <code>vertical-align: bottom;</code> from the table row</td>
<td>This here is some placeholder text, intended to take up quite a bit of vertical space, to demonstrate how the vertical alignment works in the preceding cells.</td>
</tr>
<tr>
<td>This cell inherits <code>vertical-align: middle;</code> from the table</td>
<td>This cell inherits <code>vertical-align: middle;</code> from the table</td>
<td class="align-top">This cell is aligned to the top.</td>
<td>This here is some placeholder text, intended to take up quite a bit of vertical space, to demonstrate how the vertical alignment works in the preceding cells.</td>
</tr>
</tbody>
</table>
</div>} customMarkup={<div class="table-responsive">
<table class="table align-middle">
<thead>
<tr>
...
</tr>
</thead>
<tbody>
<tr>
...
</tr>
<tr class="align-bottom">
...
</tr>
<tr>
<td>...</td>
<td>...</td>
<td class="align-top">This cell is aligned to the top.</td>
<td>...</td>
</tr>
</tbody>
</table>
</div>} />
