放置在 CSS 网格中时需要 table 滚动而不是主体滚动
Need table scroll instead of body scroll when placed within CSS grid
我有一个 table 放在 div 中。 table 超出了 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>
为什么它在网格单元中不起作用?我该如何解决?谢谢。
使用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>
删除某种代码并创建新代码。
padding:10px
on the .table-section class then wrap the table to another div.
在这里查看我的 fiddle https://jsfiddle.net/c0mx9Lzv/4/
我通常不会将 tables 与 grids 混合使用。无论如何,请注意在 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>
如果你的table小于网格单元格-要修复,你可以考虑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>
我有一个 table 放在 div 中。 table 超出了 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>
为什么它在网格单元中不起作用?我该如何解决?谢谢。
使用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>
删除某种代码并创建新代码。
padding:10px
on the .table-section class then wrap the table to another div.
在这里查看我的 fiddle https://jsfiddle.net/c0mx9Lzv/4/
我通常不会将 tables 与 grids 混合使用。无论如何,请注意在 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>
如果你的table小于网格单元格-要修复,你可以考虑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>