gpt4 book ai didi

html - 使用 html div 修复和滚动表格结构

转载 作者:太空狗 更新时间:2023-10-29 14:15:41 24 4
gpt4 key购买 nike

我想创建一个表格网格,其中前几列是固定的,其余列可滚动,如图所示。

enter image description here

其余列是动态的,用户可以选择和取消选择列。我正在努力使用 div 或表格制作该 html。需要指导或示例结构才能继续。

最佳答案

自定义实现。就这么简单:

table {
table-layout: fixed;
width: 100%;
*margin-left: -100px;/*ie7*/
}
td, th {
vertical-align: top;
border-top: 1px solid #ccc;
padding:10px;
width:100px;
}
.col1{
position:absolute;
*position: relative; /*ie7*/
left:0;
width:100px;
}
.col2{
position:absolute;
*position: relative; /*ie7*/
left:100px;
width:100px;
}
.col3{
position:absolute;
*position: relative; /*ie7*/
left:200px;
width:100px;
}
.col4{
position:absolute;
*position: relative; /*ie7*/
left:300px;
width:100px;
}
.outer {position:relative}
.inner {
overflow-x:scroll;
overflow-y:visible;
width:500px;
margin-left:400px;
}
<div class="outer">
<div class="inner">
<table>
<tr>
<th class="col1">Header A</th>
<th class="col2">Header A</th>
<th class="col3">Header A</th>
<th class="col4">Header A</th>
<td>col 2 - A (WITH LONGER CONTENT)</td>
<td>col 3 - A</td>
<td>col 4 - A</td>
<td>col 5 - A</td>
<td>col 6 - B</td>
<td>col 7 - B</td>
</tr>
<tr>
<th class="col1">Header B</th>
<th class="col2">Header B</th>
<th class="col3">Header B</th>
<th class="col4">Header B</th>
<td>col 2 - B</td>
<td>col 3 - B</td>
<td>col 4 - B</td>
<td>col 5 - B</td>
<td>col 6 - B</td>
<td>col 7 - B</td>
</tr>
<tr>
<th class="col1">Header C</th>
<th class="col2">Header C</th>
<th class="col3">Header C</th>
<th class="col4">Header C</th>
<td>col 2 - C</td>
<td>col 3 - C</td>
<td>col 4 - C</td>
<td>col 5 - C</td>
<td>col 6 - B</td>
<td>col 7 - B</td>
</tr>
</table>
</div>
</div>

或者 jsfiddle:

https://jsfiddle.net/h75zn59o/21/

注意:

position:absolute; 是导致第一个标题列被修复的原因。

在原始 CSS 中,它只是应用于“th”,但使用了类(在本例中为 col1、col2 等)

我们可以给不同的列分配不同的固定位置。

因为列的宽度为 100 像素,所以后面的每一列都再向左放置 100 像素。因此,第一列为 0 像素,然后 col2 为 100 像素,依此类推)以避免与前一列重叠。

关于html - 使用 html div 修复和滚动表格结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42996635/

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