Нужна прокрутка таблицы вместо прокрутки тела при размещении в сетке CSS - PullRequest
1 голос
/ 02 апреля 2019

У меня есть стол, расположенный внутри 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 ]

1 голос
/ 02 апреля 2019

Я бы не смешивал таблицы с сетками .В любом случае, обратите внимание, что в 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>

Если ваша таблица меньше ячейки сетки - до fix , вы можете учитывать min-width: 100% и box-sizing: border-box для учета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>
0 голосов
/ 02 апреля 2019

удалите какой-то код и создайте новый.

padding:10px в классе .table-section , затем оберните таблицу в другой div .

см. Мою скрипку здесь https://jsfiddle.net/c0mx9Lzv/4/

0 голосов
/ 02 апреля 2019

Используйте 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>
...