html - 放置在 CSS 网格中时需要表格滚动而不是主体滚动

标签 html css css-tables css-grid

我在一个 div 中放置了一个表格。表格超出了 div 的宽度。并且 div 将 overflow-x 设置为滚动。下面的代码工作正常

body {
  margin: 0;
}

.table-section {
  width: 100%;
  overflow-x: scroll;
  padding: 10px;
  background-color: lightblue;
}

.table-section table {
  background-color: royalblue;
}

.table-section table th {
  min-width: 150px;
}
<div class="table-section">
    <table>
      <thead>
        <tr>
          <th>
            Col 1
          </th>
          <th>
            Col 2
          </th>
          <th>
            Col 3
          </th>
          <th>
            Col 4
          </th>
          <th>
            Col 5
          </th>
          <th>
            Col 6
          </th>
          <th>
            Col 7
          </th>
          <th>
            Col 8
          </th>
      </thead>
    </table>
  </div>

但是,如果我将整个内容放在一个网格单元格中,overflow-x 似乎不起作用。并且文件正文有滚动条。代码如下:

body {
  margin: 0;
}

.grid {
  display: grid;
  grid-template-columns: 230px 1fr;
}

.table-section {
  grid-column: 2 / 3;
  width: 100%;
  overflow-x: scroll;
  padding: 10px;
  background-color: lightblue;
}

.table-section table {
  background-color: royalblue;
}

.table-section table th {
  min-width: 150px;
}
<div class="grid">
  <div class="table-section">
    <table>
      <thead>
        <tr>
          <th>
            Col 1
          </th>
          <th>
            Col 2
          </th>
          <th>
            Col 3
          </th>
          <th>
            Col 4
          </th>
          <th>
            Col 5
          </th>
          <th>
            Col 6
          </th>
          <th>
            Col 7
          </th>
          <th>
            Col 8
          </th>
      </thead>
    </table>
  </div>
</div>

为什么它不能在网格单元内工作?我该如何解决?谢谢。

最佳答案

我通常不会将表格网格混合使用。无论如何,请注意在 Firefox 66 中 overflow 工作正常,而在 Chrome 中则不然。从 table-section 中删除 width: 100% 并且在没有它的情况下它可以正常工作:

请看下面的演示:

body {
  margin: 0;
}

.grid {
  display: grid;
  grid-template-columns: 230px 1fr;
}

.table-section {
  grid-column: 2 / 3;
  /*width: 100%;*/
  overflow-x: scroll;
  padding: 10px;
  background-color: lightblue;
}

.table-section table {
  background-color: royalblue;
}

.table-section table th {
  min-width: 150px;
}
<div class="grid">
  <div class="table-section">
    <table>
      <thead>
        <tr>
          <th>
            Col 1
          </th>
          <th>
            Col 2
          </th>
          <th>
            Col 3
          </th>
          <th>
            Col 4
          </th>
          <th>
            Col 5
          </th>
          <th>
            Col 6
          </th>
          <th>
            Col 7
          </th>
          <th>
            Col 8
          </th>
      </thead>
    </table>
  </div>
</div>

如果您的表格小于网格单元格 - 修复,您可以考虑 min-width: 100%box-sizing: border-box width 计算中考虑 padding:

body {
  margin: 0;
}

.grid {
  display: grid;
  grid-template-columns: 230px 1fr;
}

.table-section {
  grid-column: 2 / 3;
  min-width: 100%; /* changed */
  overflow-x: scroll;
  padding: 10px;
  background-color: lightblue;
  box-sizing: border-box; /* added */
}

.table-section table {
  background-color: royalblue;
}

.table-section table th {
  min-width: 150px;
}
<div class="grid">
  <div class="table-section">
    <table>
      <thead>
        <tr>
          <th>
            Col 1
          </th>
          <th>
            Col 2
          </th>
          <th>
            Col 3
          </th>
          <th>
            Col 4
          </th>
          <th>
            Col 5
          </th>
          <th>
            Col 6
          </th>
          <th>
            Col 7
          </th>
          <th>
            Col 8
          </th>
      </thead>
    </table>
  </div>
</div>

关于html - 放置在 CSS 网格中时需要表格滚动而不是主体滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55469176/

相关文章:

html - 对齐 2 :after contents below each other

html - Bootstrap : Textarea height shrinked when I apply Bootstrap style to it

html - 表格边框不直

html - 如何在 CSS/HTML 中引用计算机目录中的图像作为背景图像

使用表格的 CSS Sprite 按钮......无法在 Safari 中删除表格后的换行符

jquery - 使用 Div 的表结构

javascript - 使用 forEach 比较两个数组并根据条件添加不同的 html 元素

javascript - 在三星智能电视上运行基于 Cordova/Phonegap 的应用程序

javascript - 使用 src 链接获取 Img 元素

html - 如何设置 border-radius 来制作带圆圈的字母?