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

说明

实现原理和表头固定的可纵向滚动表格一样,可见链接:【css常用布局】表头固定的可纵向滚动的表格布局(小白向)

效果展示

在这里插入图片描述

实现原理

布局分析:
在这里插入图片描述
不要以为表格横过来了tr标签就为纵向了,每一行还是一个tr。只是把tr标签中的第一个td标签固定左侧了。

HTML部分:

<div class="wrap">
	<div class="table_wrap">
		<table class="table">
			<thead> <!-- thead标签在这里已经不代表表头了-->
				<tr>  <!-- 每个tr标签的第一个td标签为表头-->
					<td class="table_title">
						<p>只要你足够努力</p>
					</td>
					<td class="table_content">
						<p>你的老板就会过上他想要的生活</p>
					</td>
                   					
                    <!-- ... 省略 -->
                          
					<td class="table_content">
						<p>你的老板就会过上他想要的生活</p>
					</td>
				</tr>
			</thead>
			<tbody>
				<tr class="table_tr">
					<td class="table_title">
						<p>只要你足够努力</p>
					</td>
					<td class="table_content">
	                    <p>你的老板就会过上他想要的生活</p>
					</td>
					
					<!-- ... 省略 -->
				</tr>
				<tr class="table_tr">
					<td class="table_title">
						<p>只要你足够努力</p>
					</td>
					<td class="table_content">
	                    <p>你的老板就会过上他想要的生活</p>
					</td>
					
					<!-- ... 省略 -->
				</tr>
				
				<!-- ... 省略 -->
			</tbody>
		</table>
	</div>
</div>

记得每一行的td数量要一致

CSS部分:

.wrap {
	/* 这个只是用来居中内容 */
	align-items: center;
	display: flex;
	height: 100%;
	justify-content: center;
	padding: 8px;
}

table thead th{
	font-size: 400;
}

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

.table {
	border-collapse: collapse;
	width: 100%;
}

.table_title {
	padding: 10px;
	background-color: rgb(221, 221, 221);
	left: 0px;
	position: sticky; // 表头还是靠粘性固定
	font-weight: bold;
	text-align: center;
}

.table_title p,  .table_content p{
	width: 200px;
	font-size: 12px;
}

.table_content {
	padding: 10px;
}

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

完~

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个 HTML 表格,可以实现表头竖向固定横向滚动的效果: ```html <style> table { table-layout: fixed; width: 100%; border-collapse: collapse; } th, td { border: 1px solid #ccc; padding: 10px; text-align: center; } th { background-color: #f2f2f2; white-space: nowrap; writing-mode: vertical-lr; transform: rotate(-180deg); height: 100px; width: 50px; } tbody { display: block; height: 300px; overflow-y: auto; } tr { display: table; width: 100%; table-layout: fixed; } </style> <table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> <th>Header 4</th> <th>Header 5</th> <th>Header 6</th> <th>Header 7</th> <th>Header 8</th> <th>Header 9</th> <th>Header 10</th> </tr> </thead> <tbody> <tr> <td>Row 1, Cell 1</td> <td>Row 1, Cell 2</td> <td>Row 1, Cell 3</td> <td>Row 1, Cell 4</td> <td>Row 1, Cell 5</td> <td>Row 1, Cell 6</td> <td>Row 1, Cell 7</td> <td>Row 1, Cell 8</td> <td>Row 1, Cell 9</td> <td>Row 1, Cell 10</td> </tr> <tr> <td>Row 2, Cell 1</td> <td>Row 2, Cell 2</td> <td>Row 2, Cell 3</td> <td>Row 2, Cell 4</td> <td>Row 2, Cell 5</td> <td>Row 2, Cell 6</td> <td>Row 2, Cell 7</td> <td>Row 2, Cell 8</td> <td>Row 2, Cell 9</td> <td>Row 2, Cell 10</td> </tr> <tr> <td>Row 3, Cell 1</td> <td>Row 3, Cell 2</td> <td>Row 3, Cell 3</td> <td>Row 3, Cell 4</td> <td>Row 3, Cell 5</td> <td>Row 3, Cell 6</td> <td>Row 3, Cell 7</td> <td>Row 3, Cell 8</td> <td>Row 3, Cell 9</td> <td>Row 3, Cell 10</td> </tr> <tr> <td>Row 4, Cell 1</td> <td>Row 4, Cell 2</td> <td>Row 4, Cell 3</td> <td>Row 4, Cell 4</td> <td>Row 4, Cell 5</td> <td>Row 4, Cell 6</td> <td>Row 4, Cell 7</td> <td>Row 4, Cell 8</td> <td>Row 4, Cell 9</td> <td>Row 4, Cell 10</td> </tr> <tr> <td>Row 5, Cell 1</td> <td>Row 5, Cell 2</td> <td>Row 5, Cell 3</td> <td>Row 5, Cell 4</td> <td>Row 5, Cell 5</td> <td>Row 5, Cell 6</td> <td>Row 5, Cell 7</td> <td>Row 5, Cell 8</td> <td>Row 5, Cell 9</td> <td>Row 5, Cell 10</td> </tr> </tbody> </table> ``` 这个表格中,表头使用了 CSS 的 `writing-mode` 属性,将文字竖向排列,并使用 `transform` 属性将文字翻转180度,使其正常显示。同时,表头的高度设置为100px,保证表头表格内容有一定的距离。 表格内容使用了 `display: block;` 和 `height: 300px;` 属性,将其设置为块级元素,并且设置了固定的高度,以便实现垂直滚动条。 最后,使用了 `table-layout: fixed;` 属性,使表格布局固定,以便实现横向滚动条。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值