小码哥的IT人生

Bootstrap 5 表格

Bootstrap 2023-08-10 09:26:52小码哥的IT人生shichen

Bootstrap 5 表格

基础表格

一个基本的 Bootstrap 5 表格有一点内边距,以及水平分隔线。

.table 类为表格添加了基本样式:

示例代码:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h1>基础表格</h1>
  <p>.table 类为表格添加了基本样式(浅色内边距和水平分隔线):</p>
  <table class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Bill</td>
        <td>Gates</td>
        <td>bill@example.com</td>
      </tr>
      <tr>
        <td>Steve</td>
        <td>Jobs</td>
        <td>steve@example.com</td>
      </tr>
      <tr>
        <td>Elon</td>
        <td>Musk</td>
        <td>elon@example.com</td>
      </tr>
    </tbody>
  </table>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

条纹行

.table-striped 类将斑马条纹添加到表中:

示例代码:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h1>条纹行</h1>
  <p>.table-striped 类将斑马条纹添加到表中:</p>
  <table class="table table-striped">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Bill</td>
        <td>Gates</td>
        <td>bill@example.com</td>
      </tr>
      <tr>
        <td>Steve</td>
        <td>Jobs</td>
        <td>steve@example.com</td>
      </tr>
      <tr>
        <td>Elon</td>
        <td>Musk</td>
        <td>elon@example.com</td>
      </tr>
    </tbody>
  </table>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

带边框的表格

.table-bordered 类为表格和单元格的所有边添加边框:

示例代码:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h1>带边框的表格</h1>
  <p>.table-bordered 类在表格和单元格的所有边添加边框:</p>
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Bill</td>
        <td>Gates</td>
        <td>bill@example.com</td>
      </tr>
      <tr>
        <td>Steve</td>
        <td>Jobs</td>
        <td>steve@example.com</td>
      </tr>
      <tr>
        <td>Elon</td>
        <td>Musk</td>
        <td>elon@example.com</td>
      </tr>
    </tbody>
  </table>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

有悬停效果的行

.table-hover 类在表格行上添加悬停效果(灰色背景色):

示例代码:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h1>有悬停效果的行</h1>
  <p>.table-hover 类在表格行上启用悬停状态(鼠标悬停时的灰色背景):</p>
  <table class="table table-hover">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Bill</td>
        <td>Gates</td>
        <td>bill@example.com</td>
      </tr>
      <tr>
        <td>Steve</td>
        <td>Jobs</td>
        <td>steve@example.com</td>
      </tr>
      <tr>
        <td>Elon</td>
        <td>Musk</td>
        <td>elon@example.com</td>
      </tr>
    </tbody>
  </table>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

黑色/深色表格

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

示例代码:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h1>黑色/深色表格</h1>
  <p>.table-dark 类为表格添加黑色背景:</p>
  <table class="table table-dark">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Bill</td>
        <td>Gates</td>
        <td>bill@example.com</td>
      </tr>
      <tr>
        <td>Steve</td>
        <td>Jobs</td>
        <td>steve@example.com</td>
      </tr>
      <tr>
        <td>Elon</td>
        <td>Musk</td>
        <td>elon@example.com</td>
      </tr>
    </tbody>
  </table>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

深色条纹表格

结合 .table-dark.table-striped 来创建深色的条纹表格:

示例代码:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h1>深色条纹表格</h1>
  <p>结合 .table-dark 和 .table-striped 来创建深色的条纹表格:</p>
  <table class="table table-dark table-striped">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Bill</td>
        <td>Gates</td>
        <td>bill@example.com</td>
      </tr>
      <tr>
        <td>Steve</td>
        <td>Jobs</td>
        <td>steve@example.com</td>
      </tr>
      <tr>
        <td>Elon</td>
        <td>Musk</td>
        <td>elon@example.com</td>
      </tr>
    </tbody>
  </table>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

可悬停的深色表格

.table-hover 类在表格行上添加悬停效果(灰色背景色):

示例代码:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h1>可悬停的深色表格</h1>
  <p>.table-hover 类在表格行上添加悬停效果(灰色背景色):</p>
  <table class="table table-dark table-hover">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Bill</td>
        <td>Gates</td>
        <td>bill@example.com</td>
      </tr>
      <tr>
        <td>Steve</td>
        <td>Jobs</td>
        <td>steve@example.com</td>
      </tr>
      <tr>
        <td>Elon</td>
        <td>Musk</td>
        <td>elon@example.com</td>
      </tr>
    </tbody>
  </table>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

无边框表格

.table-borderless 类从表格中删除边框:

示例代码:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h1>无边框表格</h1>
  <p>.table-borderless 类从表格中删除边框:</p>
  <table class="table table-borderless">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Bill</td>
        <td>Gates</td>
        <td>bill@example.com</td>
      </tr>
      <tr>
        <td>Steve</td>
        <td>Jobs</td>
        <td>steve@example.com</td>
      </tr>
      <tr>
        <td>Elon</td>
        <td>Musk</td>
        <td>elon@example.com</td>
      </tr>
    </tbody>
  </table>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

上下文类

上下文类可用于为整个表格 (<table>)、表格行 (<tr>) 或表格单元格 (<td>) 着色。

示例代码:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h1>上下文类</h1>
  <p>上下文类可用于为表格、表格行或表格单元格着色。可以使用的类有:.table-primary、.table-success、.table-info、.table-warning、.table-danger、.table-active、.table-secondary、.table-light 和 .table-dark:</p>
  <table class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Default</td>
        <td>Defaultson</td>
        <td>def@somemail.com</td>
      </tr>
      <tr class="table-primary">
        <td>Primary</td>
        <td>Joe</td>
        <td>joe@example.com</td>
      </tr>
      <tr class="table-success">
        <td>Success</td>
        <td>Gates</td>
        <td>bill@example.com</td>
      </tr>
      <tr class="table-danger">
        <td>Danger</td>
        <td>Jobs</td>
        <td>steve@example.com</td>
      </tr>
      <tr class="table-info">
        <td>Info</td>
        <td>Musk</td>
        <td>elon@example.com</td>
      </tr>
      <tr class="table-warning">
        <td>Warning</td>
        <td>Refs</td>
        <td>bo@example.com</td>
      </tr>
      <tr class="table-active">
        <td>Active</td>
        <td>Activeson</td>
        <td>act@example.com</td>
      </tr>
      <tr class="table-secondary">
        <td>Secondary</td>
        <td>Secondson</td>
        <td>sec@example.com</td>
      </tr>
      <tr class="table-light">
        <td>Light</td>
        <td>Angie</td>
        <td>angie@example.com</td>
      </tr>
      <tr class="table-dark">
        <td>Dark</td>
        <td>Bo</td>
        <td>bo@example.com</td>
      </tr>
    </tbody>
  </table>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

可用的上下文类:

描述
.table-primary 蓝色:表示重要动作。
.table-success 绿色:表示成功或积极的动作。
.table-danger 红色:表示危险或潜在的负面行为。
.table-info 浅蓝色:表示中性的信息更改或操作。
.table-warning 橙色:表示可能需要注意的警告。
.table-active 灰色:将悬停颜色应用于表格行或表格单元格。
.table-secondary 灰色:表示不太重要的动作。
.table-light 浅灰色表格或表格行背景。
.table-dark 深灰色表格或表格行背景。

表头颜色

您还可以使用任何上下文类只向表格标题添加背景颜色:

示例代码:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h1>表头颜色</h1>
  <p>您可以使用任何上下文类只向表格标题添加背景颜色:</p>
  <table class="table">
    <thead class="table-dark">
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Bill</td>
        <td>Gates</td>
        <td>bill@example.com</td>
      </tr>
      <tr>
        <td>Steve</td>
        <td>Jobs</td>
        <td>steve@example.com</td>
      </tr>
      <tr>
        <td>Elon</td>
        <td>Musk</td>
        <td>elon@example.com</td>
      </tr>
    </tbody>
  </table>
  <table class="table">
    <thead class="table-success">
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Bill</td>
        <td>Gates</td>
        <td>bill@example.com</td>
      </tr>
      <tr>
        <td>Steve</td>
        <td>Jobs</td>
        <td>steve@example.com</td>
      </tr>
      <tr>
        <td>Elon</td>
        <td>Musk</td>
        <td>elon@example.com</td>
      </tr>
    </tbody>
  </table>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

小型表格

.table-sm 类通过将单元格填充减半来使表格变小:

示例代码:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h1>小型表格</h1>
  <p>.table-sm 类通过将单元格填充减半来使表格变小:</p>
  <table class="table table-bordered table-sm">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Bill</td>
        <td>Gates</td>
        <td>bill@example.com</td>
      </tr>
      <tr>
        <td>Steve</td>
        <td>Jobs</td>
        <td>steve@example.com</td>
      </tr>
      <tr>
        <td>Elon</td>
        <td>Musk</td>
        <td>elon@example.com</td>
      </tr>
    </tbody>
  </table>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

响应式表格

.table-responsive 类在需要时向表格添加滚动条(当它在水平方向上太大时):

示例代码:

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

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h1>响应式表格</h1>
  <p>.table-responsive 类在需要时向表格添加滚动条(当它在水平方向上太大时):</p>
  <div class="table-responsive">
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>#</th>
          <th>Firstname</th>
          <th>Lastname</th>
          <th>Age</th>
          <th>City</th>
          <th>Country</th>
          <th>Sex</th>
          <th>Example</th>
          <th>Example</th>
          <th>Example</th>
          <th>Example</th>
          <th>Example</th>
          <th>Example</th>
          <th>Example</th>
          <th>Example</th>
          <th>Example</th>
          <th>Example</th>
          <th>Example</th>
          <th>Example</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>Bill</td>
          <td>Gates</td>
          <td>19</td>
          <td>Seatle</td>
          <td>USA</td>
          <td>Male</td>
          <td>Yes</td>
          <td>Yes</td>
          <td>Yes</td>
          <td>Yes</td>
          <td>Yes</td>
          <td>Yes</td>
          <td>Yes</td>
          <td>Yes</td>
          <td>Yes</td>
          <td>Yes</td>
          <td>Yes</td>
          <td>Yes</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

您还可以决定表格何时应该获得滚动条,具体取决于屏幕宽度:

屏幕宽度
.table-responsive-sm < 576px
.table-responsive-md < 768px
.table-responsive-lg < 992px
.table-responsive-xl < 1200px
.table-responsive-xxl < 1400px

示例代码:

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

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h1>响应式表格</h1>
  <p>.table-responsive-sm 类创建了一个响应式表格,它将在宽度小于 576px 的屏幕上水平滚动。</p>
  <p>请调整浏览器窗口大小来查看效果。</p>
  <div class="table-responsive-sm">
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>#</th>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Age</th>
        <th>City</th>
        <th>Country</th>
        <th>Sex</th>
        <th>Example</th>
        <th>Example</th>
        <th>Example</th>
        <th>Example</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>Bill</td>
        <td>Gates</td>
        <td>19</td>
        <td>Seatle</td>
        <td>USA</td>
        <td>Male</td>
        <td>Yes</td>
        <td>Yes</td>
        <td>Yes</td>
        <td>Yes</td>
      </tr>
    </tbody>
  </table>
  </div>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024