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>

These classes can also be added to table variants:

Tables - 图4

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

Tables - 图5

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

Hoverable rows

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

Tables - 图6

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

Tables - 图7

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

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

Tables - 图8

  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 - 图9

  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 - 图10

  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, 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 a gradient on the table cells with background-image: linear-gradient(var(--bs-table-accent-bg), var(--bs-table-accent-bg)); to layer on top of any specified background-color. Since --bs-table-accent-bg is transparent by default, we have an invisible transparent linear gradient by default.
  • When either .table-striped, .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. --#{$variable-prefix}table-bg: #{$background};
  8. --#{$variable-prefix}table-striped-bg: #{$striped-bg};
  9. --#{$variable-prefix}table-striped-color: #{color-contrast($striped-bg)};
  10. --#{$variable-prefix}table-active-bg: #{$active-bg};
  11. --#{$variable-prefix}table-active-color: #{color-contrast($active-bg)};
  12. --#{$variable-prefix}table-hover-bg: #{$hover-bg};
  13. --#{$variable-prefix}table-hover-color: #{color-contrast($hover-bg)};
  14. color: $color;
  15. border-color: mix($color, $background, percentage($table-border-factor));
  16. }
  17. }

Table borders

Bordered tables

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

Tables - 图11

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

Border color utilities can be added to change colors:

Tables - 图12

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

Tables without borders

Add .table-borderless for a table without borders.

Tables - 图13

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

Tables - 图14

  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 - 图15

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

Tables - 图16

  1. <table class="table table-dark table-sm">
  2. ...
  3. </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 the top by default. Use the vertical align classes to re-align where needed.

Tables - 图17

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

Nesting

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

Tables - 图18

  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 - 图19

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

Tables - 图20

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

Table foot

Tables - 图21

  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 - 图22

  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 - 图23

  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 - 图24

  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 - 图25

Tables - 图26

Tables - 图27

Tables - 图28

Tables - 图29

Tables - 图30

  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>

Customizing in Sass

  • 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-level variable.
  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: $body-color;
  7. $table-bg: transparent;
  8. $table-th-font-weight: null;
  9. $table-striped-color: $table-color;
  10. $table-striped-bg-factor: .05;
  11. $table-striped-bg: rgba($black, $table-striped-bg-factor);
  12. $table-active-color: $table-color;
  13. $table-active-bg-factor: .1;
  14. $table-active-bg: rgba($black, $table-active-bg-factor);
  15. $table-hover-color: $table-color;
  16. $table-hover-bg-factor: .075;
  17. $table-hover-bg: rgba($black, $table-hover-bg-factor);
  18. $table-border-factor: .1;
  19. $table-border-width: $border-width;
  20. $table-border-color: $border-color;
  21. $table-striped-order: odd;
  22. $table-group-separator-color: currentColor;
  23. $table-caption-color: $text-muted;
  24. $table-bg-scale: -80%;
  25. $table-variants: (
  26. "primary": shift-color($primary, $table-bg-scale),
  27. "secondary": shift-color($secondary, $table-bg-scale),
  28. "success": shift-color($success, $table-bg-scale),
  29. "info": shift-color($info, $table-bg-scale),
  30. "warning": shift-color($warning, $table-bg-scale),
  31. "danger": shift-color($danger, $table-bg-scale),
  32. "light": $light,
  33. "dark": $dark,
  34. );