当放置在CSS网格中时需要表滚动而不是正文滚动

时间:2019-04-02 07:30:03

标签: html css css3 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>

为什么它在网格单元中不起作用?我该如何解决?谢谢。

3 个答案:

答案 0 :(得分:1)

我通常不会将网格混合使用。无论如何,请注意,在Firefox 66中,溢出可以正常工作,而在Chrome中则不能。从width: 100%中删除table-section,并且两者都没有的话,它可以正常工作:

请参见下面的演示

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来说明padding > width 计算:

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>

答案 1 :(得分:0)

使用min-width代替width:100%

body {
  margin: 0;
}

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

.table-section {
  grid-column: 2 / 3;
  min-width: 600px;
  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>

答案 2 :(得分:0)

删除某种代码并创建新代码。

   .table-section 类上的

padding:10px,然后将表包装到另一个 div

在这里查看我的小提琴 https://jsfiddle.net/c0mx9Lzv/4/