Bootstrap5 表格

Bootstrap 5 的表格(Table)组件提供了一系列功能和样式,使得表格更加易于构建、响应式和美观。

我们可以轻松地创建带有不同样式、排序功能、分页和响应式布局的表格。

Bootstrap5 基础表格

在 Bootstrap 5 中,创建表格的基础结构很简单,只需要使用标准的 HTML

元素,并加上 table 类。

Bootstrap5 通过 .table 类来设置基础表格的样式,实例如下:

实例

Firstname Lastname Email
John Doe [email protected]
Mary Moe [email protected]
July Dooley [email protected]

尝试一下 »

条纹表格

通过添加 .table-striped 类,您将在 内的行上看到条纹,如下面的实例所示:

实例

Firstname Lastname Email
John Doe [email protected]
Mary Moe [email protected]
July Dooley [email protected]

尝试一下 »

带边框表格

.table-bordered 类可以为表格添加边框

实例

Firstname Lastname Email
John Doe [email protected]
Mary Moe [email protected]
July Dooley [email protected]

尝试一下 »

鼠标悬停状态表格

.table-hover 类可以为表格的每一行添加鼠标悬停效果(灰色背景):

实例

Firstname Lastname Email
John Doe [email protected]
Mary Moe [email protected]
July Dooley [email protected]

尝试一下 »

黑色背景表格

.table-dark 类可以为表格添加黑色背景:

实例

Firstname Lastname Email
John Doe [email protected]
Mary Moe [email protected]
July Dooley [email protected]

尝试一下 »

黑色条纹表格

联合使用 .table-dark 和 .table-striped 类可以创建黑色的条纹表格:

实例

Firstname Lastname Email
John Doe [email protected]
Mary Moe [email protected]
July Dooley [email protected]

尝试一下 »

鼠标悬停效果 - 黑色背景表格

联合使用 .table-dark 和 .table-hover 类可以设置黑色背景表格的鼠标悬停效果:

实例

Firstname Lastname Email
John Doe [email protected]
Mary Moe [email protected]
July Dooley [email protected]

尝试一下 »

无边框表格

.table-borderless 类可以设置一个无边框的表格:

实例

Firstname Lastname Email
John Doe [email protected]
Mary Moe [email protected]
July Dooley [email protected]

尝试一下 »

指定意义的颜色类

通过指定意义的颜色类可以为表格的行或者单元格设置颜色:

实例

Firstname Lastname Email
Default Defaultson [email protected]
Primary Joe [email protected]
Success Doe [email protected]
Danger Moe [email protected]
Info Dooley [email protected]
Warning Refs [email protected]
Active Activeson [email protected]
Secondary Secondson [email protected]
Light Angie [email protected]
Dark Bo [email protected]

尝试一下 »

下表列出了表格颜色类的说明:

类名

描述

.table-primary

蓝色: 指定这是一个重要的操作

.table-success

绿色: 指定这是一个允许执行的操作

.table-danger

红色: 指定这是可以危险的操作

.table-info

浅蓝色: 表示内容已变更

.table-warning

橘色: 表示需要注意的操作

.table-active

灰色: 用于鼠标悬停效果

.table-secondary

灰色: 表示内容不怎么重要

.table-light

浅灰色,可以是表格行的背景

.table-dark

深灰色,可以是表格行的背景

表头颜色

我们也可以设置表头的颜色,例如 .table-dark 类用于给表头添加黑色背景, .table-light 类用于给表头添加灰色背景:

实例

Firstname Lastname Email
John Doe [email protected]
Mary Moe [email protected]
July Dooley [email protected]

Firstname Lastname Email
John Doe [email protected]
Mary Moe [email protected]
July Dooley [email protected]

尝试一下 »

较小的表格

.table-sm 类用于通过减少内边距来设置较小的表格:

实例

Firstname Lastname Email
John Doe [email protected]
Mary Moe [email protected]
July Dooley [email protected]

尝试一下 »

响应式表格

.table-responsive 类用于创建响应式表格:在屏幕宽度小于 992px 时会创建水平滚动条,如果可视区域宽度大于 992px 则显示不同效果(没有滚动条):

实例

# Firstname Lastname Age City Country Sex Example Example Example Example
1 Anna Pitt 35 New York USA Female Yes Yes Yes Yes

尝试一下 »

你可以通过以下类设定在指定屏幕宽度下显示滚动条:

类名

屏幕宽度

.table-responsive-sm

< 576px

.table-responsive-md

< 768px

.table-responsive-lg

< 992px

.table-responsive-xl

< 1200px

.table-responsive-xxl

< 1400px

实例

...

尝试一下 »