表格

表格

使用Bootstrap使用表格的文档和示例(表格样式及响应式优先于BootStrap全局的其它JavaScript事件)。

示例

在第三方部件例如日历和日期选择器中广泛使用表格,我们设计了视情况需要加入的表格类。只需要向某个<​table>添加一个基类.table,然后通过自定义样式或系统提供的class来起作用。

使用最基本的表格标记,下面是Bootstrap中 .table表格的样式(基本样式), Bootstrap 4继承了所有的表格样式,这意味着任何嵌套表格都将以与父类型相同的方式进行样式化。

<​table class="table">  <​thead>    <​tr>      <​th scope="col">#<​/th>      <​th scope="col">First Name<​/th>      <​th scope="col">Last Name<​/th>      <​th scope="col">Username<​/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>Larry<​/td>      <​td>the Bird<​/td>      <​td>@twitter<​/td>    <​/tr>  <​/tbody><​/table>

你可使用.table-darkclass选择器来产生颜色反转对比效果,即深色背景和浅色文本。

<​table class="table table-dark">  <​thead>    <​tr>      <​th scope="col">#<​/th>      <​th scope="col">First Name<​/th>      <​th scope="col">Last Name<​/th>      <​th scope="col">Username<​/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>Larry<​/td>      <​td>the Bird<​/td>      <​td>@twitter<​/td>    <​/tr>  <​/tbody><​/table>

与预设的反转样式相似,使用.thead-light 或.thead-dark 中的一个样式,就能使 <​thead>区显示出浅黑或深灰。

<​table class="table">  <​thead class="thead-dark">    <​tr>      <​th scope="col">#<​/th>      <​th scope="col">First Name<​/th>      <​th scope="col">Last Name<​/th>      <​th scope="col">Username<​/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>Larry<​/td>      <​td>the Bird<​/td>      <​td>@twitter<​/td>    <​/tr>  <​/tbody><​/table><​table class="table">  <​thead class="thead-light">    <​tr>      <​th scope="col">#<​/th>      <​th scope="col">First Name<​/th>      <​th scope="col">Last Name<​/th>      <​th scope="col">Username<​/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>Larry<​/td>      <​td>the Bird<​/td>      <​td>@twitter<​/td>    <​/tr>  <​/tbody><​/table>

条纹状表格

使用 .table-striped 样式定义 <​tbody>,可以产生逐行颜色强烈对比的表格样式(以及增加反转)。

<​table class="table table-striped">  <​thead>    <​tr>      <​th scope="col">#<​/th>      <​th scope="col">First Name<​/th>      <​th scope="col">Last Name<​/th>      <​th scope="col">Username<​/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>Larry<​/td>      <​td>the Bird<​/td>      <​td>@twitter<​/td>    <​/tr>  <​/tbody><​/table>
<​table class="table table-striped table-dark">  <​thead>    <​tr>      <​th scope="col">#<​/th>      <​th scope="col">First Name<​/th>      <​th scope="col">Last Name<​/th>      <​th scope="col">Username<​/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>Larry<​/td>      <​td>the Bird<​/td>      <​td>@twitter<​/td>    <​/tr>  <​/tbody><​/table>

表格边框处理

添加 .table-bordered 类可以产生表格边框与间隙系统。

<​table class="table table-bordered">  <​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 colspan="2">Larry the Bird<​/td>      <​td>@twitter<​/td>    <​/tr>  <​/tbody><​/table>
<​table class="table table-bordered table-dark">  <​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 colspan="2">Larry the Bird<​/td>      <​td>@twitter<​/td>    <​/tr>  <​/tbody><​/table>

无边框

添加.table-borderless无边界表格

<​table class="table table-borderless">  <​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 colspan="2">Larry the Bird<​/td>      <​td>@twitter<​/td>    <​/tr>  <​/tbody><​/table>

.table-borderless 也可以用在黑色表格上.

<​table class="table table-borderless table-dark">  <​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 colspan="2">Larry the Bird<​/td>      <​td>@twitter<​/td>    <​/tr>  <​/tbody><​/table>

行悬停效果

将 .table-hover 定义到 <​tbody>上,可以产生行悬停效果(鼠标移到行上会出现状态提示)。

<​table class="table table-hover">  <​thead>    <​tr>      <​th scope="col">#<​/th>      <​th scope="col">First Name<​/th>      <​th scope="col">Last Name<​/th>      <​th scope="col">Username<​/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 colspan="2">Larry the Bird<​/td>      <​td>@twitter<​/td>    <​/tr>  <​/tbody><​/table>
<​table class="table table-hover table-dark">  <​thead>    <​tr>      <​th scope="col">#<​/th>      <​th scope="col">First Name<​/th>      <​th scope="col">Last Name<​/th>      <​th scope="col">Username<​/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 colspan="2">Larry the Bird<​/td>      <​td>@twitter<​/td>    <​/tr>  <​/tbody><​/table>

紧缩表格

添加 .table-sm 可以将表格的padding值缩减一半,使表格更加紧凑。

<​table class="table table-sm">  <​thead>    <​tr>      <​th scope="col">#<​/th>      <​th scope="col">First Name<​/th>      <​th scope="col">Last Name<​/th>      <​th scope="col">Username<​/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 colspan="2">Larry the Bird<​/td>      <​td>@twitter<​/td>    <​/tr>  <​/tbody><​/table>