表格

使用 Bootstrap 为表格提供可选启用样式的文档和示例(鉴于它们在 JavaScript 插件中的普遍使用)。

Layer: content
Container queries

概述

由于 <table> 元素在日历和日期选择器等第三方小部件中的广泛使用,Bootstrap 的表格是可选启用的。为任何 <table> 添加基础类 .table,然后使用我们的可选修饰类或自定义样式进行扩展。Bootstrap 中所有表格样式都不会被继承,这意味着任何嵌套表格都可以独立于父表格设置样式。

使用最基本的表格标记,以下是基于 .table 的表格在 Bootstrap 中的外观。

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<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>
</table>

变体

使用主题特定的颜色类为表格、表格行或单个单元格设置样式。这些也会适应颜色模式。

Class Heading Heading
Default Cell Cell
Primary Cell Cell
Accent Cell Cell
Success Cell Cell
Danger Cell Cell
Warning Cell Cell
Info Cell Cell
Inverse Cell Cell
Secondary Cell Cell
HTML
<!-- On tables -->
<table class="table theme-primary">...</table>
<table class="table theme-accent">...</table>
<table class="table theme-success">...</table>
<table class="table theme-danger">...</table>
<table class="table theme-warning">...</table>
<table class="table theme-info">...</table>
<table class="table theme-inverse">...</table>
<table class="table theme-secondary">...</table>


<!-- On rows -->
<tr class="theme-primary">...</tr>
<tr class="theme-accent">...</tr>
<tr class="theme-success">...</tr>
<tr class="theme-danger">...</tr>
<tr class="theme-warning">...</tr>
<tr class="theme-info">...</tr>
<tr class="theme-inverse">...</tr>
<tr class="theme-secondary">...</tr>




<!-- On cells (`td` or `th`) -->
<tr>
  <td class="theme-primary">...</td>
  <td class="theme-accent">...</td>
  <td class="theme-success">...</td>
  <td class="theme-danger">...</td>
  <td class="theme-warning">...</td>
  <td class="theme-info">...</td>
  <td class="theme-inverse">...</td>
  <td class="theme-secondary">...</td>
</tr>
无障碍提示:使用颜色传达含义

仅使用颜色来增添含义只能提供视觉上的提示,无法传达给屏幕阅读器等辅助技术的用户。请确保含义本身从内容中就能显而易见(例如,具有_足够_颜色对比度的可见文本),或通过其他方式包含,例如使用 .visually-hidden 类隐藏的附加文本。

强调表格

条纹行

使用 .table-striped 为 <tbody> 中的任何表格行添加斑马纹。

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table table-striped">
  ...
</table>

条纹列

使用 .table-striped-columns 为任何表格列添加斑马纹。

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table table-striped-columns">
  ...
</table>

这些类也可以添加到表格变体上:

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table table-striped" data-bs-theme="dark">
  ...
</table>
# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table table-striped-columns" data-bs-theme="dark">
  ...
</table>
# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table theme-success table-striped">
  ...
</table>
# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table theme-success table-striped-columns">
  ...
</table>

可悬停行

添加 .table-hover 以启用 <tbody> 中表格行的悬停状态。

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table table-hover">
  ...
</table>
# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table table-hover" data-bs-theme="dark">
  ...
</table>

这些可悬停行也可以与条纹行变体组合使用:

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table table-striped table-hover">
  ...
</table>

激活表格

通过添加 .table-active 类突出显示表格行或单元格。

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML

<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>
# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML

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

变体和强调表格如何工作?

对于强调表格(条纹行、条纹列、可悬停行和激活表格),我们使用了一些技术使这些效果适用于我们所有的表格变体:

  • 我们首先将表格单元格的背景设置为 var(--bs-theme-bg-subtle, var(--bs-table-bg))。表格变体即 .theme-* 类:每个类都会在其添加到的表格、行或单元格上设置 --bs-theme-bg-subtle 和 --bs-theme-fg,内部的单元格会接收它们。这样,即使使用半透明颜色作为表格背景,我们也无需担心。

  • 然后我们在表格单元格上添加内嵌盒阴影 box-shadow: inset 0 0 0 9999px var(--bs-table-bg-state, var(--bs-table-bg-type, var(--bs-theme-bg-subtle, var(--bs-table-accent-bg))));,以层叠在任何指定的 background-color 之上。它使用自定义级联来覆盖 box-shadow,而不管 CSS 特异性如何。因为我们使用了巨大的扩散范围且没有模糊,颜色将呈现单色调。由于 --bs-table-accent-bg 默认设置为 transparent,我们默认没有盒阴影。

  • 当添加 .table-striped、.table-striped-columns、.table-hover 或 .table-active 类中的任一个时,--bs-table-bg-type 或 --bs-table-bg-state(默认设置为 initial)其中之一会被设置为半透明颜色,以着色背景并覆盖默认的 --bs-table-accent-bg。当主题类设置时该颜色为 --bs-theme-fg,否则为 --bs-table-color,并通过 --bs-table-striped-bg-factor、--bs-table-active-bg-factor 或 --bs-table-hover-bg-factor 与 transparent 混合。混合在每个单元格上进行,因此行或单元格上的主题类会为其自身的条纹和悬停状态着色。

  • 文本颜色以相同方式生成,其颜色默认被继承。当主题类设置时,边框使用 --bs-theme-border,否则使用 --bs-table-border-color。

表格边框

带边框表格

添加 .table-bordered 为表格及其单元格的所有边添加边框。

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table table-bordered">
  ...
</table>

可以添加边框颜色工具类以更改颜色:

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table table-bordered border-primary">
  ...
</table>

无边框表格

添加 .table-borderless 获得无边框表格。

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table table-borderless">
  ...
</table>
# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table table-borderless" data-bs-theme="dark">
  ...
</table>

卡片式表格

需要一个带边框、圆角的表格,而无需将其嵌套在卡片中?包裹表格并裁剪角落。

使用工具类

组合边框、圆角和溢出工具类。使用 .mb-0 去除表格的底部外边距。

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<div class="border rounded overflow-hidden">
  <table class="table table-striped mb-0">
    ...
  </table>
</div>

使用 .table-card

使用 .table-card 获得相同的外观。它会像卡片一样圆角、裁剪单元格背景,并移除最后一行的底部边框,以免与包裹元素叠加。

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<div class="table-card">
  <table class="table table-hover">
    ...
  </table>
</div>

当表格可能溢出时,将其与响应式包裹元素组合使用:

# Heading Heading Heading Heading Heading Heading Heading Heading
1 Cell Cell Cell Cell Cell Cell Cell Cell
2 Cell Cell Cell Cell Cell Cell Cell Cell
3 Cell Cell Cell Cell Cell Cell Cell Cell
HTML
<div class="table-responsive table-card">
  <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>

小表格

添加 .table-sm 通过将所有单元格 padding 减半,使任何 .table 更紧凑。

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table table-sm">
  ...
</table>
# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table table-sm" data-bs-theme="dark">
  ...
</table>

表格组分隔线

使用 .table-group-divider 在表格组——<thead>、<tbody> 和 <tfoot>——之间添加更粗、更深的边框。通过更改 border-top-color 来自定义颜色(目前我们暂不为此提供工具类)。

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<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 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>

垂直对齐

<thead> 的表格单元格始终垂直对齐到底部。<tbody> 中的表格单元格从 <table> 继承对齐方式,默认对齐到顶部。使用垂直对齐类在需要的地方重新对齐。

Heading 1 Heading 2 Heading 3 Heading 4
This cell inherits vertical-align: middle; from the table This cell inherits vertical-align: middle; from the table This cell inherits vertical-align: middle; from the table 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.
This cell inherits vertical-align: bottom; from the table row This cell inherits vertical-align: bottom; from the table row This cell inherits vertical-align: bottom; from the table row 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.
This cell inherits vertical-align: middle; from the table This cell inherits vertical-align: middle; from the table This cell is aligned to the top. 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.
HTML

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

嵌套

边框样式、激活样式和表格变体不会被嵌套表格继承。

# First Last Handle
1 Mark Otto @mdo
Header Header Header
A First Last
B First Last
C First Last
3 John Doe @social
HTML

<table class="table table-striped table-bordered">
<thead>
  ...
</thead>
<tbody>
  ...
  <tr>
    <td colspan="4">
      <table class="table mb-0">
        ...
      </table>
    </td>
  </tr>
  ...
</tbody>
</table>

嵌套如何工作

为防止任何样式泄漏到嵌套表格,我们在 CSS 中使用子代组合器(>)选择器。由于我们需要定位 thead、tbody 和 tfoot 中的所有 td 和 th,没有它我们的选择器会相当长。因此,我们使用看起来有些奇怪的 .table > :not(caption) > * > * 选择器来定位 .table 的所有 td 和 th,但不包括任何潜在的嵌套表格。

请注意,如果你将 <tr> 添加为表格的直接子元素,这些 <tr> 默认会被包裹在 <tbody> 中,从而使我们的选择器按预期工作。

页脚

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
Footer Footer Footer Footer
HTML
<table class="table">
  <thead>
    ...
  </thead>
  <tbody>
    ...
  </tbody>
  <tfoot>
    ...
  </tfoot>
</table>

标题

<caption> 的作用类似于表格的标题。它帮助屏幕阅读器用户找到表格、了解其内容并决定是否要阅读。

List of users
# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
          <table class="table table-sm">
  <caption>List of users</caption>
  <thead>
    ...
  </thead>
  <tbody>
    ...
  </tbody>
</table>
        

你还可以使用 .caption-top 将 <caption> 放在表格顶部。

List of users
# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 John Doe @social
HTML
<table class="table 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>

堆叠表格

添加 .table-stacked,在空间有限时将表格的行转换为堆叠块。每行变为一个垂直块,第一个单元格作为加粗标题,其余单元格显示为标签–值对。标签由每个 <td> 上的 data-cell 属性生成。

.table-stacked 和 .table-responsive 都使用容器查询,因此需要 .table-responsive 包裹元素作为查询容器。使用 .md:table-stacked、.lg:table-stacked 等仅在低于特定容器宽度时堆叠。默认的 .table-stacked 始终堆叠。

Name Type Default Description
backdrop boolean or 'static' true Includes a backdrop element. Specify static for a backdrop which doesn’t close on click.
keyboard boolean true Closes the component when escape key is pressed.
focus boolean true Puts the focus on the component when initialized.
HTML

<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>
HTML
          <!-- 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>
        

响应式表格

响应式表格允许表格轻松地水平滚动。通过将 .table 包裹在 .table-responsive 中,使任何表格在所有视口中都具备响应式。或者,使用 .{sm|md|lg|xl|2xl}:table-responsive 选择响应式表格的最大断点。

.table-responsive 包裹元素还充当堆叠表格的容器查询上下文。

响应式表格使用 overflow-y: hidden,这会裁剪超出表格底部或顶部边缘的任何内容。特别是,这可能会裁剪菜单和其他第三方小部件。

始终响应式

在所有断点中,使用 .table-responsive 实现水平滚动的表格。

# Heading Heading Heading Heading Heading Heading Heading Heading Heading
1 Cell Cell Cell Cell Cell Cell Cell Cell Cell
2 Cell Cell Cell Cell Cell Cell Cell Cell Cell
3 Cell Cell Cell Cell Cell Cell Cell Cell Cell
HTML

<div class="table-responsive">
<table class="table">
  ...
</table>
</div>

特定断点

根据需要,使用 .{sm|md|lg|xl|2xl}:table-responsive 创建直到特定断点的响应式表格。从该断点及以上,表格将正常表现,不会水平滚动。

这些表格在特定视口宽度应用其响应式样式之前可能显示为损坏。

#HeadingHeadingHeadingHeadingHeadingHeadingHeadingHeading
1CellCellCellCellCellCellCellCell
2CellCellCellCellCellCellCellCell
3CellCellCellCellCellCellCellCell
#HeadingHeadingHeadingHeadingHeadingHeadingHeadingHeading
1CellCellCellCellCellCellCellCell
2CellCellCellCellCellCellCellCell
3CellCellCellCellCellCellCellCell
#HeadingHeadingHeadingHeadingHeadingHeadingHeadingHeading
1CellCellCellCellCellCellCellCell
2CellCellCellCellCellCellCellCell
3CellCellCellCellCellCellCellCell
#HeadingHeadingHeadingHeadingHeadingHeadingHeadingHeading
1CellCellCellCellCellCellCellCell
2CellCellCellCellCellCellCellCell
3CellCellCellCellCellCellCellCell
#HeadingHeadingHeadingHeadingHeadingHeadingHeadingHeading
1CellCellCellCellCellCellCellCell
2CellCellCellCellCellCellCellCell
3CellCellCellCellCellCellCellCell
#HeadingHeadingHeadingHeadingHeadingHeadingHeadingHeading
1CellCellCellCellCellCellCellCell
2CellCellCellCellCellCellCellCell
3CellCellCellCellCellCellCellCell
HTML
<div class="table-responsive">
  <table class="table">
    ...
  </table>
</div>

<div class="sm:table-responsive">
  <table class="table">
    ...
  </table>
</div>

<div class="md:table-responsive">
  <table class="table">
    ...
  </table>
</div>

<div class="lg:table-responsive">
  <table class="table">
    ...
  </table>
</div>

<div class="xl:table-responsive">
  <table class="table">
    ...
  </table>
</div>

<div class="2xl:table-responsive">
  <table class="table">
    ...
  </table>
</div>

CSS

变量

Tables use local CSS variables on .table for real-time customization. Values for the CSS variables are generated from Sass maps unique to each component and applied to the aforementioned class.

// stylelint-disable-next-line scss/dollar-variable-default
$table-tokens: defaults(
  (
    --table-cell-padding-y: .5rem,
    --table-cell-padding-x: .5rem,
    --table-cell-vertical-align: top,
    --table-color: var(--fg-body),
    --table-bg: var(--bg-body),
    --table-accent-bg: transparent,
    --table-border-width: var(--border-width),
    --table-border-color: var(--border-color),
    --table-group-separator-color: currentcolor,
    --table-striped-color: var(--table-color),
    --table-striped-bg-factor: 5%,
    --table-active-color: var(--table-color),
    --table-active-bg-factor: 10%,
    --table-hover-color: var(--table-color),
    --table-hover-bg-factor: 7.5%,
  ),
  $table-tokens
);

自定义

  • 因子变量(--table-striped-bg-factor、--table-active-bg-factor 和 --table-hover-bg-factor)用于确定表格变体中的对比度。