0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-10 22:24:19 +01:00
Bootstrap/docs/4.0/content/tables.md

793 lines
18 KiB
Markdown
Raw Normal View History

2014-07-12 07:34:47 +02:00
---
layout: docs
2014-07-12 07:34:47 +02:00
title: Tables
2017-05-28 07:16:44 +02:00
description: Documentation and examples for opt-in styling of tables (given their prevelant use in JavaScript plugins) with Bootstrap.
2015-08-10 08:38:16 +02:00
group: content
2017-05-28 07:16:44 +02:00
toc: true
2014-07-12 07:34:47 +02:00
---
## Examples
2017-05-28 07:16:44 +02:00
Due to the widespread use of tables across third-party widgets like calendars and date pickers, we've designed our tables to be **opt-in**. Just add the base class `.table` to any `<table>`, then extend with custom styles or our various included modifier classes.
Using the most basic table markup, here's how `.table`-based tables look in Bootstrap. **All table styles are inherited in Bootstrap 4**, meaning any nested tables will be styled in the same manner as the parent.
2014-07-12 07:34:47 +02:00
{% example html %}
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Username</th>
2014-07-12 07:34:47 +02:00
</tr>
</thead>
<tbody>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">1</th>
2014-07-12 07:34:47 +02:00
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">2</th>
2014-07-12 07:34:47 +02:00
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">3</th>
2014-07-12 07:34:47 +02:00
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
{% endexample %}
You can also invert the colors—with light text on dark backgrounds—with `.table-dark`.
2014-07-14 09:23:46 +02:00
{% example html %}
<table class="table table-dark">
2014-07-14 09:23:46 +02:00
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Username</th>
2014-07-14 09:23:46 +02:00
</tr>
</thead>
<tbody>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">1</th>
2014-07-14 09:23:46 +02:00
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">2</th>
2014-07-14 09:23:46 +02:00
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">3</th>
2014-07-14 09:23:46 +02:00
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
{% endexample %}
2014-07-14 08:45:34 +02:00
## Table head options
Similar to tables and dark tables, use the modifier classes `.thead-light` or `.thead-dark` to make `<thead>`s appear light or dark gray.
2014-07-14 08:45:34 +02:00
{% example html %}
<table class="table">
<thead class="thead-dark">
2014-07-14 08:45:34 +02:00
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Username</th>
2014-07-14 08:45:34 +02:00
</tr>
</thead>
<tbody>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">1</th>
2014-07-14 08:45:34 +02:00
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">2</th>
2014-07-14 08:45:34 +02:00
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">3</th>
2014-07-14 08:45:34 +02:00
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
<table class="table">
<thead class="thead-light">
2014-07-14 08:45:34 +02:00
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Username</th>
2014-07-14 08:45:34 +02:00
</tr>
</thead>
<tbody>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">1</th>
2014-07-14 08:45:34 +02:00
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">2</th>
2014-07-14 08:45:34 +02:00
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">3</th>
2014-07-14 08:45:34 +02:00
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
{% endexample %}
2014-07-12 07:34:47 +02:00
## Striped rows
Use `.table-striped` to add zebra-striping to any table row within the `<tbody>`.
{% example html %}
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Username</th>
2014-07-12 07:34:47 +02:00
</tr>
</thead>
<tbody>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">1</th>
2014-07-12 07:34:47 +02:00
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">2</th>
2014-07-12 07:34:47 +02:00
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">3</th>
2014-07-12 07:34:47 +02:00
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
{% endexample %}
{% example html %}
<table class="table table-striped table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Username</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>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
{% endexample %}
2014-07-12 07:34:47 +02:00
## Bordered table
Add `.table-bordered` for borders on all sides of the table and cells.
{% example html %}
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Username</th>
2014-07-12 07:34:47 +02:00
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
2014-07-12 07:34:47 +02:00
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
2014-07-12 07:34:47 +02:00
<td>Mark</td>
<td>Otto</td>
<td>@TwBootstrap</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">3</th>
2014-07-12 07:34:47 +02:00
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">4</th>
2014-07-12 07:34:47 +02:00
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
{% endexample %}
{% example html %}
<table class="table table-bordered table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Username</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>Mark</td>
<td>Otto</td>
<td>@TwBootstrap</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">4</th>
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
{% endexample %}
2014-07-12 07:34:47 +02:00
## Hoverable rows
Add `.table-hover` to enable a hover state on table rows within a `<tbody>`.
{% example html %}
<table class="table table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Username</th>
2014-07-12 07:34:47 +02:00
</tr>
</thead>
<tbody>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">1</th>
2014-07-12 07:34:47 +02:00
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">2</th>
2014-07-12 07:34:47 +02:00
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">3</th>
2014-07-12 07:34:47 +02:00
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
{% endexample %}
{% example html %}
<table class="table table-hover table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Username</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 colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
{% endexample %}
2014-09-18 07:14:27 +02:00
## Small table
2014-07-12 07:34:47 +02:00
2014-09-18 07:14:27 +02:00
Add `.table-sm` to make tables more compact by cutting cell padding in half.
2014-07-12 07:34:47 +02:00
{% example html %}
2014-09-18 07:14:27 +02:00
<table class="table table-sm">
2014-07-12 07:34:47 +02:00
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Username</th>
2014-07-12 07:34:47 +02:00
</tr>
</thead>
<tbody>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">1</th>
2014-07-12 07:34:47 +02:00
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">2</th>
2014-07-12 07:34:47 +02:00
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
{% endexample %}
{% example html %}
<table class="table table-sm table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Username</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>
2014-07-12 07:34:47 +02:00
</tr>
<tr>
2014-12-01 06:01:50 +01:00
<th scope="row">3</th>
2014-07-12 07:34:47 +02:00
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
{% endexample %}
## Contextual classes
Use contextual classes to color table rows or individual cells.
<div class="bd-example">
2014-07-12 07:34:47 +02:00
<table class="table">
<thead>
<tr>
<th scope="col">Type</th>
<th scope="col">Column heading</th>
<th scope="col">Column heading</th>
<th scope="col">Column heading</th>
2014-07-12 07:34:47 +02:00
</tr>
</thead>
<tbody>
<tr class="table-active">
2017-06-28 18:36:27 +02:00
<th scope="row">Active</th>
2014-07-12 07:34:47 +02:00
<td>Column content</td>
<td>Column content</td>
<td>Column content</td>
</tr>
<tr>
2017-06-28 18:36:27 +02:00
<th scope="row">Default</th>
2014-07-12 07:34:47 +02:00
<td>Column content</td>
<td>Column content</td>
<td>Column content</td>
</tr>
2017-06-28 18:36:27 +02:00
{% for color in site.data.theme-colors %}
<tr class="table-{{ color.name }}">
<th scope="row">{{ color.name | capitalize }}</th>
2014-07-12 07:34:47 +02:00
<td>Column content</td>
<td>Column content</td>
<td>Column content</td>
2017-06-28 18:36:27 +02:00
</tr>{% endfor %}
2014-07-12 07:34:47 +02:00
</tbody>
</table>
</div>
{% highlight html %}
<!-- On rows -->
<tr class="table-active">...</tr>
2017-06-28 18:36:27 +02:00
{% for color in site.data.theme-colors %}
<tr class="table-{{ color.name }}">...</tr>{% endfor %}
2014-07-12 07:34:47 +02:00
<!-- On cells (`td` or `th`) -->
<tr>
<td class="table-active">...</td>
2017-06-28 18:36:27 +02:00
{% for color in site.data.theme-colors %}
<td class="table-{{ color.name }}">...</td>{% endfor %}
2014-07-12 07:34:47 +02:00
</tr>
{% endhighlight %}
Regular table background variants are not available with the dark table, however, you may use [text or background utilities]({{ site.baseurl }}/docs/{{ site.docs_version }}/utilities/colors/) to achieve similar styles.
<div class="bd-example">
<table class="table table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Column heading</th>
<th scope="col">Column heading</th>
<th scope="col">Column heading</th>
</tr>
</thead>
<tbody>
<tr class="bg-primary">
<th scope="row">1</th>
<td>Column content</td>
<td>Column content</td>
<td>Column content</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Column content</td>
<td>Column content</td>
<td>Column content</td>
</tr>
<tr class="bg-success">
<th scope="row">3</th>
<td>Column content</td>
<td>Column content</td>
<td>Column content</td>
</tr>
<tr>
<th scope="row">4</th>
<td>Column content</td>
<td>Column content</td>
<td>Column content</td>
</tr>
<tr class="bg-info">
<th scope="row">5</th>
<td>Column content</td>
<td>Column content</td>
<td>Column content</td>
</tr>
<tr>
<th scope="row">6</th>
<td>Column content</td>
<td>Column content</td>
<td>Column content</td>
</tr>
<tr class="bg-warning">
<th scope="row">7</th>
<td>Column content</td>
<td>Column content</td>
<td>Column content</td>
</tr>
<tr>
<th scope="row">8</th>
<td>Column content</td>
<td>Column content</td>
<td>Column content</td>
</tr>
<tr class="bg-danger">
<th scope="row">9</th>
<td>Column content</td>
<td>Column content</td>
<td>Column content</td>
</tr>
</tbody>
</table>
</div>
2016-05-08 23:16:23 +02:00
{% highlight html %}
<!-- On rows -->
<tr class="bg-primary">...</tr>
<tr class="bg-success">...</tr>
<tr class="bg-warning">...</tr>
<tr class="bg-danger">...</tr>
<tr class="bg-info">...</tr>
<!-- On cells (`td` or `th`) -->
<tr>
<td class="bg-primary">...</td>
<td class="bg-success">...</td>
<td class="bg-warning">...</td>
<td class="bg-danger">...</td>
<td class="bg-info">...</td>
</tr>
{% endhighlight %}
{% capture callout-include %}{% include callout-warning-color-assistive-technologies.md %}{% endcapture %}
{{ callout-include | markdownify }}
2014-07-12 07:34:47 +02:00
## Captions
A `<caption>` functions like a heading for a table. It helps users with screen readers to find a table and understand what its about and decide if they want to read it.
{% example html %}
<table class="table">
<caption>List of users</caption>
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Username</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>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
{% endexample %}
## Responsive tables
Responsive tables allow tables to be scrolled horizontally with ease. Make any table responsive across all viewports by adding `.table-responsive` class on `.table`. Or, pick a maximum breakpoint with which to have a responsive table up to by adding `.table-responsive{-sm|-md|-lg|-xl}`.
2014-07-12 07:34:47 +02:00
{% callout warning %}
#### Vertical clipping/truncation
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 dropdown menus and other third-party widgets.
{% endcallout %}
### Always responsive
<div class="bd-example">
<table class="table table-responsive">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
</tr>
</tbody>
</table>
2014-07-12 07:34:47 +02:00
<table class="table table-bordered table-responsive-lg">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
</tr>
</tbody>
2014-07-12 07:34:47 +02:00
</table>
</div>
{% highlight html %}
<table class="table table-responsive">
...
</table>
2014-07-12 07:34:47 +02:00
{% endhighlight %}
### Breakpoint specific
Use `.table-responsive{-sm|-md|-lg|-xl}` 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.
<div class="bd-example">
{% for bp in site.data.breakpoints %}{% unless bp.breakpoint == "xs" %}
<table class="table table-responsive{{ bp.abbr }}">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
<th scope="col">Table heading</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
<td>Table cell</td>
</tr>
</tbody>
</table>
{% endunless %}{% endfor %}
</div>
{% highlight html %}
{% for bp in site.data.breakpoints %}{% unless bp.breakpoint == "xs" %}
<table class="table table-responsive{{ bp.abbr }}">
...
</table>
{% endunless %}{% endfor %}
{% endhighlight %}