gpt4 book ai didi

html - 在为 tbody 使用 block 显示时保留表格布局

转载 作者:可可西里 更新时间:2023-11-01 14:47:35 25 4
gpt4 key购买 nike

我有一个带有可滚动 tbody 的表格。问题是滚动无法正常工作我将 tbody 的显示设置为阻塞,这反过来又弄乱了表格布局

<div align="center">
<table class="sampleTable">
<thead>
<tr>
<th class="centerAlign" colspan="3">
<b>HEADER</b>
</th>
</tr>
<tr>
<th class="centerAlign">COLUMN 1</th>
<th class="centerAlign">COLUMN 2</th>
<th class="centerAlign">COLUMN 3</th>
</tr>
</thead>
<tbody class="scroll">
<tr>
<td class="centerAlign">
<a href="google.com">DATA</a>
</td>
<td class="centerAlign">
DATA
</td>
<td class="centerAlign">
DATA
</td>
</tr>
<tr>
<td class="centerAlign">
<a href="google.com">DATA</a>
</td>
<td class="centerAlign">
DATA
</td>
<td class="centerAlign">
DATA
</td>
</tr>
<tr>
<td class="centerAlign">
<a href="google.com">DATA</a>
</td>
<td class="centerAlign">
DATA
</td>
<td class="centerAlign">
DATA
</td>
</tr>
<tr>
<td class="centerAlign">
<a href="google.com">DATA</a>
</td>
<td class="centerAlign">
DATA
</td>
<td class="centerAlign">
DATA
</td>
</tr>
<tr>
<td class="centerAlign">
<a href="google.com">DATA</a>
</td>
<td class="centerAlign">
DATA
</td>
<td class="centerAlign">
DATA
</td>
</tr>
<tr>
<td class="centerAlign">
<a href="google.com">DATA</a>
</td>
<td class="centerAlign">
DATA
</td>
<td class="centerAlign">
DATA
</td>
</tr>
<tr>
<td class="centerAlign">
<a href="google.com">DATA</a>
</td>
<td class="centerAlign">
DATA
</td>
<td class="centerAlign">
DATA
</td>
</tr>
<tr>
<td class="centerAlign">
<a href="google.com">DATA</a>
</td>
<td class="centerAlign">
DATA
</td>
<td class="centerAlign">
DATA
</td>
</tr>
<tr>
<td class="centerAlign">
<a href="google.com">DATA</a>
</td>
<td class="centerAlign">
DATA
</td>
<td class="centerAlign">
DATA
</td>
</tr>
<tr>
<td class="centerAlign">
<a href="google.com">DATA</a>
</td>
<td class="centerAlign">
DATA
</td>
<td class="centerAlign">
DATA
</td>
</tr>
<tr>
<td class="centerAlign">
<a href="google.com">DATA</a>
</td>
<td class="centerAlign">
DATA
</td>
<td class="centerAlign">
DATA
</td>
</tr>
<tr>
<td class="centerAlign">
<a href="google.com">DATA</a>
</td>
<td class="centerAlign">
DATA
</td>
<td class="centerAlign">
DATA
</td>
</tr>
</tbody>
</table>
</div>

这是CSS:

table {
border-collapse: collapse;
clear: both;
}

table.sampleTable {
border: 0;
width: 60%;
height: 90%;
}

td {
font-weight: normal;
font-family: arial;
font-size: 13px;
color: #222222;
line-height: 18px;
border-top: 1px solid #ebebeb;
border-right: 1px solid #ebebeb;
border-bottom: 1px solid #ebebeb;
}

td.centerAlign {
text-align: center;
}

th.centerAlign {
text-align: center;
}

/* Scrollable tbody. */

tbody.scroll {
overflow: scroll;
height: 100px;
width: 100%;
}

表格看起来像这样并且不滚动: Without block display

现在,如果我将 'display: block 添加到 tbody.scroll 类和 thead,它就会开始滚动(如我所愿),但布局会变得一团糟。

tbody.scroll {
overflow: scroll;
height: 100px;
width: 100%;
display: block;
}

thead {
display:block;
}

现在是这样的。 enter image description here

是否可以在不影响表格外观的情况下让 tbody 滚动?

最佳答案

我找到了解决方案。在视口(viewport)中添加宽度修复了它。

td.centerAlign {
text-align: center;
width: 100vw;
}

th.centerAlign {
width: 100vw;
text-align: center;
}

关于html - 在为 tbody 使用 block 显示时保留表格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27104899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com