【css常用布局】表头固定的可纵向滚动的表格布局(小白向)

说明

纵向滚动表格的时候,表头固定

效果展示

在这里插入图片描述

实现原理

html部分

<div class="container">
	<div class="table_wrap">
		<table class="table">
			<thead>
				<tr>
					<th class="table_th">我是标题一</th>
					<th class="table_th">我是标题二</th>
					<th class="table_th">我是标题三</th>
				</tr>
			</thead>
			<tbody>
				<tr class="table_tr">
					<td class="table_td">万事开头难</td>
					<td class="table_td">然后中间难</td>
					<td class="table_td">最后结尾难</td>
				</tr>
				
				... 中间就省略了
				
				<tr class="table_tr">
					<td class="table_td">万事开头难</td>
					<td class="table_td">然后中间难</td>
					<td class="table_td">最后结尾难</td>
				</tr>
			</tbody>
		</table>
	</div>
</div>

css部分

.container { // 内容居中
    display: flex;
	align-items: center;
	flex-direction: column;
	font-size: 12px;
}

.table_wrap {
	border: 1px solid rgba(0, 0, 0, 0.3);
	height: 400px;
	overflow: auto;
	width: 60%;
}

.table {
	border-collapse: collapse; // 表格边框合并
	width: 100%;
}

.table_th {
	background-color: rgb(221, 221, 221);
	padding: 16px;
	position: sticky; // 表格头靠这个属性固定
	top: 0px;
	z-index: 9999;
}

.table_tr {
	border-top: 1px solid rgba(0, 0, 0, 0.3);
}

.table_td {
	padding: 16px;
}

简单吧,给个赞再走吧!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值