View on GitHub

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.

Tables - 图1

  1. <table class="table">
  2. <thead>
  3. <tr>
  4. <th scope="col">#</th>
  5. <th scope="col">First</th>
  6. <th scope="col">Last</th>
  7. <th scope="col">Handle</th>
  8. </tr>
  9. </thead>
  10. <tbody>
  11. <tr>
  12. <th scope="row">1</th>
  13. <td>Mark</td>
  14. <td>Otto</td>
  15. <td>@mdo</td>
  16. </tr>
  17. <tr>
  18. <th scope="row">2</th>
  19. <td>Jacob</td>
  20. <td>Thornton</td>
  21. <td>@fat</td>
  22. </tr>
  23. <tr>
  24. <th scope="row">3</th>
  25. <td colspan="2">Larry the Bird</td>
  26. <td>@twitter</td>
  27. </tr>
  28. </tbody>
  29. </table>

Variants

Use contextual classes to color tables, table rows or individual cells.

Tables - 图2

  1. <!-- On tables -->
  2. <table class="table-primary">...</table>
  3. <table class="table-secondary">...</table>
  4. <table class="table-success">...</table>
  5. <table class="table-danger">...</table>
  6. <table class="table-warning">...</table>
  7. <table class="table-info">...</table>
  8. <table class="table-light">...</table>
  9. <table class="table-dark">...</table>
  10. <!-- On rows -->
  11. <tr class="table-primary">...</tr>
  12. <tr class="table-secondary">...</tr>
  13. <tr class="table-success">...</tr>
  14. <tr class="table-danger">...</tr>
  15. <tr class="table-warning">...</tr>
  16. <tr class="table-info">...</tr>
  17. <tr class="table-light">...</tr>
  18. <tr class="table-dark">...</tr>
  19. <!-- On cells (`td` or `th`) -->
  20. <tr>
  21. <td class="table-primary">...</td>
  22. <td class="table-secondary">...</td>
  23. <td class="table-success">...</td>
  24. <td class="table-danger">...</td>
  25. <td class="table-warning">...</td>
  26. <td class="table-info">...</td>
  27. <td class="table-light">...</td>
  28. <td class="table-dark">...</td>
  29. </tr>
Conveying meaning to assistive technologies

Using color to add meaning only provides a visual indication, which will not be conveyed to users of assistive technologies – such as screen readers. Ensure that information denoted by the color is either obvious from the content itself (e.g. the visible text), or is included through alternative means, such as additional text hidden with the .visually-hidden class.

Accented tables

Striped rows

Use .table-striped to add zebra-striping to any table row within the <tbody>.

Tables - 图3

  1. <table class="table table-striped">
  2. ...
  3. </table>

Striped columns

Use .table-striped-columns to add zebra-striping to any table column.

Tables - 图4

  1. <table class="table table-striped-columns">
  2. ...
  3. </table>

These classes can also be added to table variants:

Tables - 图5

  1. <table class="table table-dark table-striped">
  2. ...
  3. </table>

Tables - 图6

  1. <table class="table table-dark table-striped-columns">
  2. ...
  3. </table>

Tables - 图7

  1. <table class="table table-success table-striped">
  2. ...
  3. </table>

Tables - 图8

  1. <table class="table table-success table-striped-columns">
  2. ...
  3. </table>

Hoverable rows

Add .table-hover to enable a hover state on table rows within a <tbody>.

Tables - 图9

  1. <table class="table table-hover">
  2. ...
  3. </table>

Tables - 图10

  1. <table class="table table-dark table-hover">
  2. ...
  3. </table>

These hoverable rows can also be combined with the striped rows variant:

Tables - 图11

  1. <table class="table table-striped table-hover">
  2. ...
  3. </table>

Active tables

Highlight a table row or cell by adding a .table-active class.

Tables - 图12

  1. <table class="table">
  2. <thead>
  3. ...
  4. </thead>
  5. <tbody>
  6. <tr class="table-active">
  7. ...
  8. </tr>
  9. <tr>
  10. ...
  11. </tr>
  12. <tr>
  13. <th scope="row">3</th>
  14. <td colspan="2" class="table-active">Larry the Bird</td>
  15. <td>@twitter</td>
  16. </tr>
  17. </tbody>
  18. </table>

Tables - 图13

  1. <table class="table table-dark">
  2. <thead>
  3. ...
  4. </thead>
  5. <tbody>
  6. <tr class="table-active">
  7. ...
  8. </tr>
  9. <tr>
  10. ...
  11. </tr>
  12. <tr>
  13. <th scope="row">3</th>
  14. <td colspan="2" class="table-active">Larry the Bird</td>
  15. <td>@twitter</td>
  16. </tr>
  17. </tbody>
  18. </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-bg custom 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-accent-bg); to layer on top of any specified background-color. Because we use a huge spread and no blur, the color will be monotone. Since --bs-table-accent-bg is unset by default, we don’t have a default box shadow.
  • When either .table-striped, .table-striped-columns, .table-hover or .table-active classes are added, the --bs-table-accent-bg is set to a semitransparent color to colorize the background.
  • For each table variant, we generate a --bs-table-accent-bg color with the highest contrast depending on that color. For example, the accent color for .table-primary is darker while .table-dark has a lighter accent color.
  • Text and border colors are generated the same way, and their colors are inherited by default.

Behind the scenes it looks like this:

  1. @mixin table-variant($state, $background) {
  2. .table-#{$state} {
  3. $color: color-contrast(opaque($body-bg, $background));
  4. $hover-bg: mix($color, $background, percentage($table-hover-bg-factor));
  5. $striped-bg: mix($color, $background, percentage($table-striped-bg-factor));
  6. $active-bg: mix($color, $background, percentage($table-active-bg-factor));
  7. $border-color: mix($color, $background, percentage($table-border-factor));
  8. --#{$prefix}table-color: #{$color};
  9. --#{$prefix}table-bg: #{$background};
  10. --#{$prefix}table-border-color: #{$border-color};
  11. --#{$prefix}table-striped-bg: #{$striped-bg};
  12. --#{$prefix}table-striped-color: #{color-contrast($striped-bg)};
  13. --#{$prefix}table-active-bg: #{$active-bg};
  14. --#{$prefix}table-active-color: #{color-contrast($active-bg)};
  15. --#{$prefix}table-hover-bg: #{$hover-bg};
  16. --#{$prefix}table-hover-color: #{color-contrast($hover-bg)};
  17. color: var(--#{$prefix}table-color);
  18. border-color: var(--#{$prefix}table-border-color);
  19. }
  20. }

Table borders

Bordered tables

Add .table-bordered for borders on all sides of the table and cells.

Tables - 图14

  1. <table class="table table-bordered">
  2. ...
  3. </table>

Border color utilities can be added to change colors:

Tables - 图15

  1. <table class="table table-bordered border-primary">
  2. ...
  3. </table>

Tables without borders

Add .table-borderless for a table without borders.

Tables - 图16

  1. <table class="table table-borderless">
  2. ...
  3. </table>

Tables - 图17

  1. <table class="table table-dark table-borderless">
  2. ...
  3. </table>

Small tables

Add .table-sm to make any .table more compact by cutting all cell padding in half.

Tables - 图18

  1. <table class="table table-sm">
  2. ...
  3. </table>

Tables - 图19

  1. <table class="table table-dark table-sm">
  2. ...
  3. </table>

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).

Tables - 图20

html

  1. <table class="table">
  2. <thead>
  3. <tr>
  4. <th scope="col">#</th>
  5. <th scope="col">First</th>
  6. <th scope="col">Last</th>
  7. <th scope="col">Handle</th>
  8. </tr>
  9. </thead>
  10. <tbody class="table-group-divider">
  11. <tr>
  12. <th scope="row">1</th>
  13. <td>Mark</td>
  14. <td>Otto</td>
  15. <td>@mdo</td>
  16. </tr>
  17. <tr>
  18. <th scope="row">2</th>
  19. <td>Jacob</td>
  20. <td>Thornton</td>
  21. <td>@fat</td>
  22. </tr>
  23. <tr>
  24. <th scope="row">3</th>
  25. <td colspan="2">Larry the Bird</td>
  26. <td>@twitter</td>
  27. </tr>
  28. </tbody>
  29. </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.

Tables - 图21

  1. <div class="table-responsive">
  2. <table class="table align-middle">
  3. <thead>
  4. <tr>
  5. ...
  6. </tr>
  7. </thead>
  8. <tbody>
  9. <tr>
  10. ...
  11. </tr>
  12. <tr class="align-bottom">
  13. ...
  14. </tr>
  15. <tr>
  16. <td>...</td>
  17. <td>...</td>
  18. <td class="align-top">This cell is aligned to the top.</td>
  19. <td>...</td>
  20. </tr>
  21. </tbody>
  22. </table>
  23. </div>

Nesting

Border styles, active styles, and table variants are not inherited by nested tables.

Tables - 图22

  1. <table class="table table-striped">
  2. <thead>
  3. ...
  4. </thead>
  5. <tbody>
  6. ...
  7. <tr>
  8. <td colspan="4">
  9. <table class="table mb-0">
  10. ...
  11. </table>
  12. </td>
  13. </tr>
  14. ...
  15. </tbody>
  16. </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.

Anatomy

Table head

Similar to tables and dark tables, use the modifier classes .table-light or .table-dark to make <thead>s appear light or dark gray.

Tables - 图23

  1. <table class="table">
  2. <thead class="table-light">
  3. ...
  4. </thead>
  5. <tbody>
  6. ...
  7. </tbody>
  8. </table>

Tables - 图24

  1. <table class="table">
  2. <thead class="table-dark">
  3. ...
  4. </thead>
  5. <tbody>
  6. ...
  7. </tbody>
  8. </table>

Table foot

Tables - 图25

  1. <table class="table">
  2. <thead>
  3. ...
  4. </thead>
  5. <tbody>
  6. ...
  7. </tbody>
  8. <tfoot>
  9. ...
  10. </tfoot>
  11. </table>

Captions

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.

Tables - 图26

  1. <table class="table table-sm">
  2. <caption>List of users</caption>
  3. <thead>
  4. ...
  5. </thead>
  6. <tbody>
  7. ...
  8. </tbody>
  9. </table>

You can also put the <caption> on the top of the table with .caption-top.

Tables - 图27

html

  1. <table class="table caption-top">
  2. <caption>List of users</caption>
  3. <thead>
  4. <tr>
  5. <th scope="col">#</th>
  6. <th scope="col">First</th>
  7. <th scope="col">Last</th>
  8. <th scope="col">Handle</th>
  9. </tr>
  10. </thead>
  11. <tbody>
  12. <tr>
  13. <th scope="row">1</th>
  14. <td>Mark</td>
  15. <td>Otto</td>
  16. <td>@mdo</td>
  17. </tr>
  18. <tr>
  19. <th scope="row">2</th>
  20. <td>Jacob</td>
  21. <td>Thornton</td>
  22. <td>@fat</td>
  23. </tr>
  24. <tr>
  25. <th scope="row">3</th>
  26. <td>Larry</td>
  27. <td>the Bird</td>
  28. <td>@twitter</td>
  29. </tr>
  30. </tbody>
  31. </table>

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 .table-responsive{-sm|-md|-lg|-xl|-xxl}.

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.

Always responsive

Across every breakpoint, use .table-responsive for horizontally scrolling tables.

Tables - 图28

  1. <div class="table-responsive">
  2. <table class="table">
  3. ...
  4. </table>
  5. </div>

Breakpoint specific

Use .table-responsive{-sm|-md|-lg|-xl|-xxl} 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.

Tables - 图29 Tables - 图30 Tables - 图31 Tables - 图32 Tables - 图33 Tables - 图34

  1. <div class="table-responsive">
  2. <table class="table">
  3. ...
  4. </table>
  5. </div>
  6. <div class="table-responsive-sm">
  7. <table class="table">
  8. ...
  9. </table>
  10. </div>
  11. <div class="table-responsive-md">
  12. <table class="table">
  13. ...
  14. </table>
  15. </div>
  16. <div class="table-responsive-lg">
  17. <table class="table">
  18. ...
  19. </table>
  20. </div>
  21. <div class="table-responsive-xl">
  22. <table class="table">
  23. ...
  24. </table>
  25. </div>
  26. <div class="table-responsive-xxl">
  27. <table class="table">
  28. ...
  29. </table>
  30. </div>

Sass

Variables

  1. $table-cell-padding-y: .5rem;
  2. $table-cell-padding-x: .5rem;
  3. $table-cell-padding-y-sm: .25rem;
  4. $table-cell-padding-x-sm: .25rem;
  5. $table-cell-vertical-align: top;
  6. $table-color: var(--#{$prefix}body-color);
  7. $table-bg: transparent;
  8. $table-accent-bg: transparent;
  9. $table-th-font-weight: null;
  10. $table-striped-color: $table-color;
  11. $table-striped-bg-factor: .05;
  12. $table-striped-bg: rgba($black, $table-striped-bg-factor);
  13. $table-active-color: $table-color;
  14. $table-active-bg-factor: .1;
  15. $table-active-bg: rgba($black, $table-active-bg-factor);
  16. $table-hover-color: $table-color;
  17. $table-hover-bg-factor: .075;
  18. $table-hover-bg: rgba($black, $table-hover-bg-factor);
  19. $table-border-factor: .1;
  20. $table-border-width: $border-width;
  21. $table-border-color: var(--#{$prefix}border-color);
  22. $table-striped-order: odd;
  23. $table-striped-columns-order: even;
  24. $table-group-separator-color: currentcolor;
  25. $table-caption-color: $text-muted;
  26. $table-bg-scale: -80%;

Loop

  1. $table-variants: (
  2. "primary": shift-color($primary, $table-bg-scale),
  3. "secondary": shift-color($secondary, $table-bg-scale),
  4. "success": shift-color($success, $table-bg-scale),
  5. "info": shift-color($info, $table-bg-scale),
  6. "warning": shift-color($warning, $table-bg-scale),
  7. "danger": shift-color($danger, $table-bg-scale),
  8. "light": $light,
  9. "dark": $dark,
  10. );

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-scale variable.