Tables·续
Nesting
Border styles, active styles, and table variants are not inherited by nested tables.
<Example code={<table class="table table-striped table-bordered">
<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>
<td colspan="4">
<table class="table mb-0">
<thead>
<tr>
<th scope="col">Header</th>
<th scope="col">Header</th>
<th scope="col">Header</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">A</th>
<td>First</td>
<td>Last</td>
</tr>
<tr>
<th scope="row">B</th>
<td>First</td>
<td>Last</td>
</tr>
<tr>
<th scope="row">C</th>
<td>First</td>
<td>Last</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<th scope="row">3</th>
<td>John</td>
<td>Doe</td>
<td>@social</td>
</tr>
</tbody>
</table>} customMarkup={<table class="table table-striped table-bordered">
<thead>
...
</thead>
<tbody>
...
<tr>
<td colspan="4">
<table class="table mb-0">
...
</table>
</td>
</tr>
...
</tbody>
</table>} />
How nesting works
To prevent any styles from leaking to nested tables, we use the child combinator (>) selector in our CSS. Since we need to target all the tds and ths in the thead, tbody, and tfoot, our selector would look pretty long without it. As such, we use the rather odd looking .table > :not(caption) > * > * selector to target all tds and ths of the .table, but none of any potential nested tables.
Note that if you add <tr>s as direct children of a table, those <tr> will be wrapped in a <tbody> by default, thus making our selectors work as intended.
Footer
<Example showMarkup={false} 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>
<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>
<tfoot>
<tr>
<td>Footer</td>
<td>Footer</td>
<td>Footer</td>
<td>Footer</td>
</tr>
</tfoot>
</table>} />
<table class="table">
<thead>
...
</thead>
<tbody>
...
</tbody>
<tfoot>
...
</tfoot>
</table>
Caption
A <caption> functions like a heading for a table. It helps users with screen readers to find a table and understand what it’s about and decide if they want to read it.
<div class="bd-example"> <table class="table"> <caption>List of users</caption> </table> </div>
<table class="table table-sm">
<caption>List of users</caption>
<thead>
...
</thead>
<tbody>
...
</tbody>
</table>
You can also put the <caption> on the top of the table with .caption-top.
<caption>List of users</caption> <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>`} />
Stacked tables
Add .table-stacked to convert a table’s rows into stacked blocks when space is limited. Each row becomes a vertical block with the first cell acting as a bold title and the remaining cells displayed as label–value pairs. Labels are generated from data-cell attributes on each <td>.
Both .table-stacked and .table-responsive use container queries, so the .table-responsive wrapper is required as the query container. Use .md:table-stacked, .lg:table-stacked, etc. to only stack below a specific container width. The default .table-stacked always stacks.
<ResizableExample code={<div class="table-responsive">
<table class="table md:table-stacked">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Type</th>
<th scope="col">Default</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td data-cell="Name">backdrop</td>
<td data-cell="Type">boolean or 'static'</td>
<td data-cell="Default">true</td>
<td data-cell="Description">Includes a backdrop element. Specify static for a backdrop which doesn’t close on click.</td>
</tr>
<tr>
<td data-cell="Name">keyboard</td>
<td data-cell="Type">boolean</td>
<td data-cell="Default">true</td>
<td data-cell="Description">Closes the component when escape key is pressed.</td>
</tr>
<tr>
<td data-cell="Name">focus</td>
<td data-cell="Type">boolean</td>
<td data-cell="Default">true</td>
<td data-cell="Description">Puts the focus on the component when initialized.</td>
</tr>
</tbody>
</table>
</div>} customMarkup={<div class="table-responsive">
<table class="table md:table-stacked">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Type</th>
...
</tr>
</thead>
<tbody>
<tr>
<td data-cell="Name">backdrop</td>
<td data-cell="Type">boolean or 'static'</td>
...
</tr>
</tbody>
</table>
</div>} />
<!-- Always stacked -->
<div class="table-responsive">
<table class="table table-stacked">...</table>
</div>
<!-- Stacked below md container width -->
<div class="table-responsive">
<table class="table md:table-stacked">...</table>
</div>
Responsive tables
Responsive tables allow tables to be scrolled horizontally with ease. Make any table responsive across all viewports by wrapping a .table with .table-responsive. Or, pick a maximum breakpoint with which to have a responsive table up to by using .{sm|md|lg|xl|2xl}:table-responsive.
The .table-responsive wrapper also acts as the container query context for stacked tables.
Responsive tables make use of overflow-y: hidden, which clips off any content that goes beyond the bottom or top edges of the table. In particular, this can clip off menus and other third-party widgets.
Always responsive
Across every breakpoint, use .table-responsive for horizontally scrolling tables.
<Example code={<div class="table-responsive">
<table class="table">
<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>
<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>
<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>
<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>
<td>Cell</td>
</tr>
</tbody>
</table>
</div>} customMarkup={<div class="table-responsive">
<table class="table">
...
</table>
</div>} />
Breakpoint specific
Use .{sm|md|lg|xl|2xl}:table-responsive as needed to create responsive tables up to a particular breakpoint. From that breakpoint and up, the table will behave normally and not scroll horizontally.
These tables may appear broken until their responsive styles apply at specific viewport widths.
{getData('breakpoints').map((breakpoint) => {
return (
<div class="bd-example">
<div class={${breakpoint.abbr}table-responsive}>
<table class="table">
<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>
</div>
)
})}
<Code code={getData('breakpoints').map((breakpoint) => <div class="${breakpoint.abbr}table-responsive">
<table class="table">
...
</table>
</div>)} lang="html" />
CSS
Variables
Customizing
- The factor variables (
--table-striped-bg-factor,--table-active-bg-factor&--table-hover-bg-factor) are used to determine the contrast in table variants. - Apart from the light & dark table variants, theme colors are lightened by the
--table-bg-scalevariable.
