BOOTSTRA.386 v5.3.1 表格完全指南:.table 变体、斑马纹与响应式表格的源码级解析
BOOTSTRA.386 v5.3.1 表格完全指南:.table 变体、斑马纹与响应式表格的源码级解析
本篇技术指南基于 BOOTSTRA.386 仓库内 v5.3.1/site/content/docs/5.3/content/tables.md 官方文档,系统讲解 Bootstrap 5.3.1 表格组件的全部能力:从 .table 基础类、上下文变体、斑马纹与悬停态,到嵌套隔离、响应式横向滚动与 Sass 定制。读者学完后,将能直接在自己的 BOOTSTRA.386 页面中写出可复制、可运行的表格代码,并理解其底层 CSS 自定义属性实现原理,做到既会"用"也懂"理"。
概述:为什么表格是"选择加入"(opt-in)的
日历、日期选择器等第三方小部件中 <table> 元素极为常见。如果 Bootstrap 默认给所有表格加样式,这些第三方插件的布局会瞬间"破相"。因此 Bootstrap 的表格样式是 opt-in 的:只有当你显式给 <table> 加上基础类 .table 时,表格样式才会生效。
另一个关键设计是 表格样式不会继承:.table 内部嵌套的任何 <table> 都可以完全独立于父表格进行样式设置。这一点由源码中的选择器设计保证,详见后文"嵌套表格"一节。
基础用法非常简单:
<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>
</tbody>
</table>
.table 的默认实现位于 v5.3.1/scss/_tables.scss:它设置 width: 100%、margin-bottom: $spacer、单元格内边距(padding)、底部边框,并声明了后续所有修饰类所依赖的 CSS 自定义属性(详见"工作原理"一节)。
上下文变体(Variants)
使用上下文类可以给整张表格、表格行或单个单元格着色。可用类由主题色决定,包括:
| 类名 | 适用对象 | 说明 |
|---|---|---|
.table-primary |
table / tr / td / th | 主色(primary)变体 |
.table-secondary |
table / tr / td / th | 次要色变体 |
.table-success |
table / tr / td / th | 成功色变体 |
.table-info |
table / tr / td / th | 信息色变体 |
.table-warning |
table / tr / td / th | 警告色变体 |
.table-danger |
table / tr / td / th | 危险色变体 |
.table-light |
table / tr / td / th | 浅色变体 |
.table-dark |
table / tr / td / th | 深色变体 |
在表格、行、单元格三个层级上的用法完全一致:
<!-- 用在整张表格上 -->
<table class="table table-dark">...</table>
<!-- 用在行上 -->
<tr class="table-active">...</tr>
<!-- 用在单元格(td 或 th)上 -->
<tr>
<td class="table-primary">...</td>
<td class="table-danger">...</td>
</tr>
这些变体类全部由 v5.3.1/scss/_variables.scss 中的 $table-variants 映射表循环生成:
$table-variants: (
"primary": shift-color($primary, $table-bg-scale),
"secondary": shift-color($secondary, $table-bg-scale),
"success": shift-color($success, $table-bg-scale),
"info": shift-color($info, $table-bg-scale),
"warning": shift-color($warning, $table-bg-scale),
"danger": shift-color($danger, $table-bg-scale),
"light": $light,
"dark": $dark,
) !default;
然后由 v5.3.1/scss/_tables.scss 的 @each 循环逐一调用 table-variant() mixin(定义见 v5.3.1/scss/mixins/_table-variants.scss)生成 .table-{state} 系列类。如果你需要自定义一套变体色,只需在重新编译 Sass 前覆盖 $table-variants 映射即可。
注意(来自官方文档的提示):由于生成表格变体所依赖的 CSS 相对复杂,这些变体在 v6 之前大概率不会获得自适应颜色模式(color mode adaptive styling)支持。
另外,官方文档还特别提醒:使用颜色辅助技术(assistive technologies)时,仅凭颜色传达的信息可能无法被屏幕阅读器用户感知,应配合图标、文本等非颜色手段补充语义(详见仓库内 v5.3.1/site/content/docs/5.3/content/tables.md 引用的 callouts/warning-color-assistive-technologies.md 警告内容)。
强调表格(Accented tables)
斑马纹行:.table-striped
给 <tbody> 内的奇数行添加灰色条纹背景:
<table class="table table-striped">
...
</table>
默认按 奇数行(odd)着色,对应源码中 $table-striped-order: odd !default(见 v5.3.1/scss/_variables.scss),实现选择器为 .table-striped > tbody > tr:nth-of-type(odd) > *。
斑马纹列:.table-striped-columns
Bootstrap 5 新增了按 列 着色的能力:
<table class="table table-striped-columns">
...
</table>
默认作用于第 偶数列,对应 $table-striped-columns-order: even !default(v5.3.1/scss/_variables.scss),实现选择器为 .table-striped-columns > :not(caption) > tr > :nth-child(even)。
悬停行:.table-hover
.table-hover 为 <tbody> 内的行添加悬停高亮:
<table class="table table-hover">
...
</table>
官方文档特意说明:.table-hover 在源码中被放在斑马纹规则之后(见 v5.3.1/scss/_tables.scss 及其注释 "Placed here since it has to come after the potential zebra striping"),以保证悬停态能覆盖斑马纹,因此三种强调效果可以自由组合:
<table class="table table-striped table-hover">
...
</table>
激活态:.table-active
.table-active 用于单独高亮某一行或某一单元格(典型场景是标记"当前选中"的记录):
<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 class="table-active">
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2" class="table-active">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
激活态同样可以叠加在深色表格上:
<table class="table table-dark">
<tbody>
<tr class="table-active">...</tr>
</tbody>
</table>
上述四种强调类(.table-striped、.table-striped-columns、.table-hover、.table-active)与所有变体类均可自由组合,例如 .table-dark.table-striped、.table-success.table-striped-columns、.table-dark.table-hover 等。
工作原理:CSS 自定义属性 + inset box-shadow
强调表格能兼容所有变体的秘诀,在于一套基于 CSS 自定义属性的级联方案。官方文档给出了完整机制,结合 v5.3.1/scss/_tables.scss 的源码可以梳理出四步:
- 用
--bs-table-bg作为底色锚点:所有变体通过设置--bs-table-bg自定义属性来给单元格着色。即使使用了半透明背景色,也能保证颜色叠加正确。 - 用超大 inset box-shadow 叠色:给单元格加
box-shadow: inset 0 0 0 9999px var(--bs-table-bg-state, var(--bs-table-bg-type, var(--bs-table-accent-bg)));。由于 spread 值极大且无模糊,形成均匀纯色覆盖层;由于 shadow 不参与背景混合,即使底层背景是半透明色也不会"污染"。--bs-table-accent-bg默认值为transparent,因此默认情况下没有任何 box-shadow 效果。 - 强调类只改两个属性:当加上
.table-striped/.table-striped-columns/.table-hover/.table-active时,分别把--bs-table-bg-type或--bs-table-bg-state(默认initial)设置为半透明色(--bs-table-striped-bg、--bs-table-active-bg、--bs-table-hover-bg),从而覆盖默认的--bs-table-accent-bg。从源码可见:- 斑马纹(行/列)写的是
--bs-table-bg-type(v5.3.1/scss/_tables.scss); - 激活态与悬停态写的是
--bs-table-bg-state(v5.3.1/scss/_tables.scss),且type优先于state,保证激活/悬停态能盖过斑马纹。
- 斑马纹(行/列)写的是
- 变体自动生成配套强调色:
table-variant()mixin 对每个变体色,用mix()按三个因子($table-hover-bg-factor、$table-striped-bg-factor、$table-active-bg-factor)混合出配套的悬停色、条纹色与激活色,并借助color-contrast()选出对比度最高的文字色(例如.table-primary的强调色更深,而.table-dark的强调色更浅),完整代码见 v5.3.1/scss/mixins/_table-variants.scss。
这套方案的核心价值是:用自定义属性级联替代了 CSS 优先级,无论选择器特异性如何,只要按 state → type → accent-bg 的默认值链条正确赋值,颜色叠加结果就始终正确。
表格边框
有边框:.table-bordered
.table-bordered 给表格和所有单元格四周都加上边框:
<table class="table table-bordered">
...
</table>
还可以叠加边框颜色工具类来改变边框色,例如:
<table class="table table-bordered border-primary">
...
</table>
边框颜色工具类的完整清单见仓库内 v5.3.1/site/content/docs/5.3/utilities/borders.md。源码实现(v5.3.1/scss/_tables.scss)特意将行与单元格的边框拆分:tr 只保留上下边框、td/th 只保留左右边框,以避免半透明或不同色边框在角部出现渲染瑕疵。
无边框:.table-borderless
.table-borderless 去掉所有边框,常与深色变体搭配使用:
<table class="table table-borderless">
...
</table>
<table class="table table-dark table-borderless">
...
</table>
紧凑表格:.table-sm
.table-sm 将单元格内边距减半,适合信息密度较高的场景:
<table class="table table-sm">
...
</table>
<table class="table table-dark table-sm">
...
</table>
其实现(v5.3.1/scss/_tables.scss)只是把 $table-cell-padding-x/y 换成 $table-cell-padding-x/y-sm。默认变量定义见 v5.3.1/scss/_variables.scss——值得注意的是,在本仓库的 386 主题中这些内边距变量默认值均为 0(见下文"关于 DOS 主题的说明")。
表格分组分隔线:.table-group-divider
在 <thead>、<tbody>、<tfoot> 之间添加一条更粗、颜色更深的边框,用于视觉上区分分组:
<table class="table">
<thead>...</thead>
<tbody class="table-group-divider">
<tr>...</tr>
<tr>...</tr>
</tbody>
</table>
该类的实现(v5.3.1/scss/_tables.scss)是 border-top: calc(#{$table-border-width} * 2) solid $table-group-separator-color,即两倍宽度的上边框。官方文档说明:目前 Bootstrap 未提供修改 border-top-color 的工具类,如需自定义颜色,需要自己写 CSS 覆盖。
垂直对齐(Vertical alignment)
默认规则:<thead> 单元格始终底部对齐;<tbody> 单元格继承 <table> 的设置,默认为顶部对齐。需要调整时,可使用垂直对齐工具类(.align-top、.align-middle、.align-bottom),完整清单见仓库内 v5.3.1/site/content/docs/5.3/utilities/vertical-align.md。
例如在表格上统一设为 align-middle,再对个别行/单元格单独覆盖:
<div class="table-responsive">
<table class="table align-middle">
<thead>
<tr>
<th scope="col" class="w-25">Heading 1</th>
<th scope="col" class="w-25">Heading 2</th>
<th scope="col" class="w-25">Heading 3</th>
<th scope="col" class="w-25">Heading 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>This cell inherits vertical-align: middle; from the table</td>
<td>This cell inherits vertical-align: middle; from the table</td>
<td>This cell inherits vertical-align: middle; from the table</td>
<td>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.</td>
</tr>
<tr class="align-bottom">
<td>This cell inherits vertical-align: bottom; from the table row</td>
<td>This cell inherits vertical-align: bottom; from the table row</td>
<td>This cell inherits vertical-align: bottom; from the table row</td>
<td>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.</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td class="align-top">This cell is aligned to the top.</td>
<td>...</td>
</tr>
</tbody>
</table>
</div>
对齐相关源码:.table > tbody { vertical-align: inherit; } 与 .table > thead { vertical-align: bottom; } 定义在 v5.3.1/scss/_tables.scss,基础 vertical-align: $table-cell-vertical-align(默认 top)定义在 v5.3.1/scss/_tables.scss。
嵌套表格(Nesting)与隔离原理
嵌套表格支持将一张独立表格放进某个单元格内,且不会继承父表格的边框样式、激活样式和变体样式:
<table class="table table-striped table-bordered">
<thead>...</thead>
<tbody>
<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>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
嵌套隔离的原理(见 v5.3.1/scss/_tables.scss):.table 的所有单元格样式都通过子代选择器(>)限定作用域。由于需要同时覆盖 thead、tbody、tfoot 内的所有 td 和 th,Bootstrap 使用了看起来有些"怪异"的选择器:
.table > :not(caption) > * > * { ... }
它精确命中 .table 的直接行容器(thead/tbody/tfoot,用 :not(caption) 排除标题)下的行(*),再命中行内的所有 td/th(*),从而完全跳过嵌套在单元格里的任何子表格。官方文档还提醒:如果你把 <tr> 直接写成 <table> 的子元素,浏览器会自动把它们包进一个 <tbody> 中,因此该选择器依然能正常工作。
表格结构(Anatomy)
表头:.table-light / .table-dark
与表格级变体不同,这里用 .table-light 或 .table-dark 修饰 <thead>,使其呈现浅灰或深灰:
<table class="table">
<thead class="table-light">
...
</thead>
<tbody>
...
</tbody>
</table>
<table class="table">
<thead class="table-dark">
...
</thead>
<tbody>
...
</tbody>
</table>
表尾:tfoot
<tfoot> 直接作为普通结构使用即可,样式与 thead/tbody 一致:
<table class="table">
<thead>...</thead>
<tbody>...</tbody>
<tfoot>
<tr>
<td>Footer</td>
<td>Footer</td>
<td>Footer</td>
<td>Footer</td>
</tr>
</tfoot>
</table>
标题:caption 与 .caption-top
<caption> 相当于表格的"标题",帮助屏幕阅读器用户快速定位表格并理解其内容:
<table class="table table-sm">
<caption>List of users</caption>
<thead>...</thead>
<tbody>...</tbody>
</table>
默认情况下 caption 显示在表格底部(由 v5.3.1/scss/_reboot.scss 中的 caption-side: bottom 决定,同时该处还定义了 caption 的内边距与 $table-caption-color 文字色)。需要放在顶部时,加上 .caption-top:
<table class="table caption-top">
<caption>List of users</caption>
<thead>...</thead>
<tbody>...</tbody>
</table>
.caption-top { caption-side: top; } 定义在 v5.3.1/scss/_tables.scss。
响应式表格(Responsive tables)
响应式表格通过横向滚动解决窄屏下的表格溢出问题。有两种用法:
- 始终响应式:用
.table-responsive包裹.table,在所有视口宽度下都启用横向滚动:
<div class="table-responsive">
<table class="table">
...
</table>
</div>
- 按断点响应式:用
.table-responsive{-sm|-md|-lg|-xl|-xxl}指定"在某个断点以下滚动、断点及以上恢复正常(不滚动)"。断点与视口宽度的对应关系(见仓库内 v5.3.1/site/data/breakpoints.yml):
| 类名 | 断点 | 最小视口宽度 |
|---|---|---|
.table-responsive-sm |
sm | 576px |
.table-responsive-md |
md | 768px |
.table-responsive-lg |
lg | 992px |
.table-responsive-xl |
xl | 1200px |
.table-responsive-xxl |
xxl | 1400px |
<div class="table-responsive-lg">
<table class="table">
...
</table>
</div>
注意:这些断点变体在小于对应断点的视口下,"看起来可能像是坏了"——直到滚动样式生效为止,这是正常现象。
实现层面,v5.3.1/scss/_tables.scss 遍历 $grid-breakpoints,对每个断点使用 media-breakpoint-down()(定义见 v5.3.1/scss/mixins/_breakpoints.scss,即 max-width: 断点宽度 - 0.02px,以规避 Safari 的分数像素舍入 bug)生成 .table-responsive{-infix},设置 overflow-x: auto 与 -webkit-overflow-scrolling: touch。
警告:垂直裁剪/截断。响应式表格使用
overflow-y: hidden,会裁剪掉超出表格上、下边缘的内容——尤其会裁掉下拉菜单等第三方小部件(例如用响应式表格包裹日历组件时,弹出的日期面板可能被截断)。官方文档明确标注了这一风险,使用时需要留意。
CSS:Sass 变量、循环与定制
所有表格 Sass 变量集中在 v5.3.1/scss/_variables.scss:
| 变量 | 默认值 | 作用 |
|---|---|---|
$table-cell-padding-y / $table-cell-padding-x |
0 |
单元格内边距(本主题下为 0,见下文说明) |
$table-cell-padding-y-sm / $table-cell-padding-x-sm |
0 |
.table-sm 单元格内边距 |
$table-cell-vertical-align |
top |
单元格默认垂直对齐 |
$table-color |
var(--bs-body-color) |
表格文字色 |
$table-bg |
var(--bs-body-bg) |
表格背景色 |
$table-accent-bg |
transparent |
默认强调色(透明) |
$table-striped-color / $table-striped-bg |
$table-color / rgba($black, .05) |
斑马纹文字色与背景色 |
$table-striped-bg-factor |
.05 |
斑马纹混合因子 |
$table-active-color / $table-active-bg |
$table-color / rgba($black, .1) |
激活态文字色与背景色 |
$table-active-bg-factor |
.1 |
激活态混合因子 |
$table-hover-color / $table-hover-bg |
$table-color / rgba($black, .075) |
悬停态文字色与背景色 |
$table-hover-bg-factor |
.075 |
悬停态混合因子 |
$table-border-factor |
.1 |
边框色混合因子 |
$table-border-width |
var(--bs-border-width) |
表格边框宽度 |
$table-border-color |
var(--bs-border-color) |
表格边框颜色 |
$table-striped-order |
odd |
斑马纹行序号 |
$table-striped-columns-order |
even |
斑马纹列序号 |
$table-group-separator-color |
currentcolor |
分组分隔线颜色 |
$table-caption-color |
var(--bs-secondary-color) |
caption 文字色 |
$table-bg-scale |
-80% |
变体背景色明暗偏移量 |
变体循环($table-variants)见前文"上下文变体"一节;编译入口在 v5.3.1/scss/bootstrap.scss(@import "tables";),mixin 汇总在 v5.3.1/scss/_mixins.scss(@import "mixins/table-variants";)。
定制要点(官方文档总结):
- 三个因子变量(
$table-striped-bg-factor、$table-active-bg-factor、$table-hover-bg-factor)决定各变体下强调效果的对比度——因子越大,强调色越深。 - 除
.table-light与.table-dark两个变体直接使用$light/$dark外,其余主题色变体都会按$table-bg-scale(默认-80%)做shift-color()变亮处理,使表格底色比对应按钮等组件的纯色更柔和。
关于 DOS 主题的说明
BOOTSTRA.386 是"1980 年代 DOS 风格"的 Bootstrap 主题,其 v5.3.1 分支的 386 风格主要通过 v5.3.1/scss/_dos.scss 注入(如 DOS 光标、滚动条皮肤等)。从源码结构看,_dos.scss 中没有针对表格的样式覆盖,表格的布局与强调机制直接沿用上述 Bootstrap 5.3.1 官方实现;而 $table-cell-padding-* 在 v5.3.1/scss/_variables.scss 中默认值为 0,配合整个主题将各处内边距置 0 以呈现紧凑的字符终端观感。因此本文所有类名、组合方式与定制手段在 BOOTSTRA.386 中均可直接使用,最终视觉呈现为更贴合主题的紧凑、无多余留白的 DOS 风格表格。
小结
- 表格样式是 opt-in 的:先加
.table,再用修饰类扩展;样式不会向嵌套表格泄漏。 - 变体(
.table-primary….table-dark)可作用于表格、行、单元格三个层级;斑马纹、悬停、激活四种强调效果可与任意变体自由组合。 - 底层依赖
--bs-table-bg+ inset box-shadow +--bs-table-bg-type/state的级联方案,见 v5.3.1/scss/_tables.scss 与 v5.3.1/scss/mixins/_table-variants.scss。 - 嵌套隔离由
.table > :not(caption) > * > *子代选择器保证;响应式滚动由.table-responsive{-sm|-md|-lg|-xl|-xxl}提供,注意overflow-y: hidden会裁剪下拉菜单等溢出内容。 - 深度定制入口集中在 v5.3.1/scss/_variables.scss:修改三个 factor 变量可调整强调色浓度,修改
$table-bg-scale可调整变体底色明暗,修改$table-variants映射可增删变体。