html css 可滚动表格不会与上面的表格对齐

标签 html css

第一个表格是固定的,当第二个表格向下滚动时,它会留在 View 中。如何使这两个表对齐?

#index_table,
#index_table_header {
  text-align: left;
  margin: 20px;
  margin: 0 auto;
  table-layout: fixed;
}

#index_table,
#index_table_header {
  width: 800px;
}

#index_table {
  display: block;
  height: 390px;
  overflow-y: scroll;
}

#index_table_header td {
  padding: 10px 8px;
  width: 100px;
  border: 1px solid black;
}

#index_table td {
  padding: 10px 8px;
  width: 100px;
  border: 1px solid black;
}

#index_table td:nth-child(1),
#index_table_header td:nth-child(1) {
  width: 80px;
}

#index_table td:nth-child(3),
#index_table_header td:nth-child(3) {
  width: 70px;
}

#index_table td:nth-child(5),
#index_table_header td:nth-child(5) {
  width: 200px;
}

#index_table td:nth-child(2),
#index_table_header td:nth-child(2) {
  width: 250px;
}
<table id="index_table_header">
  <thead>
    <tr>
      <td>ID</td>
      <td>Item</td>
      <td>Amount</td>
      <td>Added</td>
      <td>Nutritional Value ID</td>
      <td>Actions</td>
    </tr>
  </thead>
</table>
<table id="index_table">
  <tbody>
    <tr>
      <td>395</td>
      <td>chicken liver</td>
      <td>0.37</td>
      <td>2019-10-14</td>
      <td>67</td>
      <td>
        <a href="/delete/395">Delete</a>
        <br>
        <a href="/update/395">Update</a>
      </td>
    </tr>
</table>

一些需要考虑的事情

  1. 我需要表格和表格单元格(列)完全对齐
  2. 第二个表格实际上有数百行,我希望它可以滚动并且我不希望标题从 View 中消失,这就是为什么我有两个表格并且我不想合并为一个表格的原因。

最佳答案

您可以使用此代码

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <title>Hello, world!</title>
  <style type="text/css">
    body {
      margin: 0;
    }
    
    .table-scroll {
      position: relative;
      max-width: 600px;
      margin: auto;
      overflow: hidden;
      border: 1px solid #000;
    }
    
    .table-wrap {
      width: 100%;
      overflow: auto;
    }
    
    .table-scroll table {
      width: 100%;
      margin: auto;
      border-collapse: separate;
      border-spacing: 0;
    }
    
    .table-scroll th,
    .table-scroll td {
      padding: 5px 10px;
      border: 1px solid #000;
      background: #fff;
      white-space: nowrap;
      vertical-align: top;
    }
    
    .table-scroll thead,
    .table-scroll tfoot {
      background: #f9f9f9;
    }
    
    .clone {
      position: absolute;
      top: 0;
      left: 0;
      pointer-events: none;
    }
    
    .clone th,
    .clone td {
      visibility: hidden
    }
    
    .clone td,
    .clone th {
      border-color: transparent
    }
    
    .clone tbody th {
      visibility: visible;
      color: black;
    }
    
    .clone .fixed-side {
      border: 1px solid #000;
      background: #eee;
      visibility: visible;
    }
    
    .clone thead,
    .clone tfoot {
      background: transparent;
    }
    
    .red {
      background-color: red;
      border-radius: 10px;
      color: white;
      padding: 2px 10px;
      text-decoration: none;
      outline: none;
    }
    
    .green {
      background-color: green;
      border-radius: 10px;
      color: white;
      padding: 3px 10px;
      text-decoration: none;
      outline: none;
    }
  </style>
</head>

<body>
  <div id="table-scroll" class="table-scroll">
    <div class="table-wrap">
      <table class="main-table">
        <thead>
          <tr>
            <th class="fixed-side" scope="col">ID</th>
            <th scope="col">Item</th>
            <th scope="col">Amount</th>
            <th scope="col">Added</th>
            <th scope="col">Nutritional Value ID</th>
            <th scope="col">Actions</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <th class="fixed-side">395</th>
            <td>chicken liver</td>
            <td>0.37</td>
            <td>2019-10-14</td>
            <td>67</td>
            <td>
              <a class="red" href="/delete/395">Delete</a>
              <a class="green" href="/update/395">Update</a>
            </td>
          </tr>
          <tr>
            <th class="fixed-side">395</th>
            <td>chicken liver</td>
            <td>0.37</td>
            <td>2019-10-14</td>
            <td>67</td>
            <td>
              <a class="red" href="/delete/395">Delete</a>
              <a class="green" href="/update/395">Update</a>
            </td>
          </tr>
          <tr>
            <th class="fixed-side">395</th>
            <td>chicken liver</td>
            <td>0.37</td>
            <td>2019-10-14</td>
            <td>67</td>
            <td>
              <a class="red" href="/delete/395">Delete</a>
              <a class="green" href="/update/395">Update</a>
            </td>
          </tr>
          <tr>
            <th class="fixed-side">395</th>
            <td>chicken liver</td>
            <td>0.37</td>
            <td>2019-10-14</td>
            <td>67</td>
            <td>
              <a class="red" href="/delete/395">Delete</a>
              <a class="green" href="/update/395">Update</a>
            </td>
          </tr>
          <tr>
            <th class="fixed-side">395</th>
            <td>chicken liver</td>
            <td>0.37</td>
            <td>2019-10-14</td>
            <td>67</td>
            <td>
              <a class="red" href="/delete/395">Delete</a>
              <a class="green" href="/update/395">Update</a>
            </td>
          </tr>
          <tr>
            <th class="fixed-side">395</th>
            <td>chicken liver</td>
            <td>0.37</td>
            <td>2019-10-14</td>
            <td>67</td>
            <td>
              <a class="red" href="/delete/395">Delete</a>
              <a class="green" href="/update/395">Update</a>
            </td>
          </tr>
        </tbody>
        <tfoot>
          <tr>
            <th class="fixed-side">&nbsp;</th>
            <td>Footer 2</td>
            <td>Footer 3</td>
            <td>Footer 4</td>
            <td>Footer 5</td>
            <td>Footer 6</td>
          </tr>
        </tfoot>
      </table>
    </div>
  </div>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  <script type="text/javascript">
    // requires jquery library
    jQuery(document).ready(function() {
      jQuery(".main-table").clone(true).appendTo('#table-scroll').addClass('clone');
    });
  </script>
</body>

</html>

关于html css 可滚动表格不会与上面的表格对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58446610/

相关文章:

javascript - jquery无法获取ajax数据?

html - Bootstrap : responsive button in grid system?

html - 如何在 Bootstrap 中使用网格系统对齐 Bootstrap 输入和标签?

javascript - 基于基于列表的菜单隐藏或显示 div 内容的最佳方法

html - z-index 无法正常运行

php - html textarea mysql insert 为 HTML 和 android 保留新行

html - 使用 CSS Floats 和边距问题

html - 链接到html中的根目录文件夹

css - 自定义字体无法在 Chrome 上正确显示

css - 代码标签不自动包装内容,宽度已超过父级的宽度