html 表格head头部不动 body部分滚动,每格宽同内容增加

本文介绍了一种使用HTML和CSS实现类似Excel表格首行固定效果的方法。通过将表格头部设置为固定显示,并使表格主体部分滚动的方式,使得在滚动表格内容时,表头始终保持可见。此方法适用于网页中需要展示大量数据且希望保持表头始终可见的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如下图同Excel表格首行固定:

 

<style>
.table{    width: 100%;    border-collapse:collapse;    border-spacing:0;}

.table thead{display: block; width: 100%; background: #ddd;}
.table tbody
{display: block; height:70px; overflow: auto; width: 100%; border: 1px solid #ddd;}
.table td,.table th
{ width: 200px; border-bottom: none; border-left: none; border-right: 1px solid #CCC; border-top: 1px solid #DDD; padding: 2px 3px 3px 4px }
.table tr
{ border-bottom: 1px solid #B74; } </style>
<table class="table">
                <thead>
                <tr>
                    <th>header</th>
                    <th>header two</th>
                    <th>header two</th>
                    <th>header two</th>
                    <th>header two</th>
                    <th>header two</th>
                </tr>
                </thead>
                <tbody class="M_scrollBar">
                <tr>
                    <td>fuck 1</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                </tr>
                <tr>
                    <td>fuck 1</td>
                    <td>fuc<br>kfuc<br>kfuc<br>kfuc<br>kfuc<br>kfuc<br>kfuc<br>kfuc<br>kfuc<br>kfuc<br>kfuc<br>kfuc<br>kfuc<br>kfuc<br>k 2</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                </tr>
                <tr>
                    <td>fuck 1</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                </tr>
                <tr>
                    <td>fuck 1</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                    <td>fuck 2</td>
                </tr>
                </tbody>
            </table>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值