果
.pure-table-wrapper-div {
overflow-x: scroll;
}
table.pure-table th:first-child,
table.pure-table td:first-child {
position: sticky;
left: 0;
z-index: 1;
background-color: aliceblue;
}
.pure-table-wrapper-div:
这个类名应用于包裹表格的 div 元素。它的样式规则如下:
overflow-x: scroll;: 当内容宽度超出容器宽度时,显示水平滚动条。这允许用户在需要时水平滚动查看表格的所有列。
示例 HTML 结构:
<div class="pure-table-wrapper-div"> <table class="pure-table"> <!-- 表格内容 --> </table> </div>
table.pure-table th:first-child, table.pure-table td:first-child: 这两个选择器分别针对 .pure-table 类的表格中的第一个表头单元格(<th>)和第一个数据单元格(<td>)。
它们定义的样式规则如下:
position: sticky;: 使用黏性定位。黏性定位元素在滚动时会根据设置的 top、bottom、left 或 right 属性值在特定位置 "粘附"。在本例中,由于定义了 left: 0;,这些元素会在左侧边缘粘附。 left: 0;: 在元素滚动到视口左侧边缘时使其粘附。与 position: sticky; 结合使用。
z-index: 1;: 设置元素的堆叠顺序。值越大,元素越靠前。在这种情况下,将第一个单元格设置为 z-index: 1 可确保它在其他表格单元格之上。
background-color: aliceblue;: 为匹配的单元格设置背景颜色。这可以增强视觉效果,使粘附的单元格与其他单元格区分开。
这段 CSS 代码的主要目的是实现表格的水平滚动,并固定第一列,使其在水平滚动时保持可见。同时,为第一列的单元格设置了背景颜色以增强视觉效果。
总的来说是:
使用 position: sticky; 来固定第一列。在 table.pure-table th:first-child 中设置了 position: sticky; 和 left: 0;,并将 z-index 设为 1,以确保第一列在滚动时会固定在屏幕上方。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="./purecss@3.0.0.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文档</title>
<style>
.pure-table-wrapper-div {
overflow-x: scroll;
}
table.pure-table th:first-child,
table.pure-table td:first-child {
position: sticky;
left: 0;
z-index: 1;
background-color: aliceblue;
}
</style>
</head>
<body>
<div class="pure-table-wrapper-div">
<table class="pure-table">
<thead>
<tr>
<th>#</th>
<th>品牌</th>
<th>型号</th>
<th>随机字符串</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>本田</td>
<td>雅阁</td>
<td>2009-QWERTYUIOPASDFGHJKLZXCVBNM</td>
<td>这是一段中文说明文字。</td>
</tr>
<tr>
<td>2</td>
<td>丰田</td>
<td>凯美瑞</td>
<td>2012-QWERTYUIOPASDFGHJKLZXCVBNM</td>
<td>这是一段中文说明文字。</td>
</tr>
<tr>
<td>3</td>
<td>现代</td>
<td>领动</td>
<td>2010-QWERTYUIOPASDFGHJKLZXCVBNM</td>
<td>这是一段中文说明文字。</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
要去除表格中最后一列中文的换行效果,可以使用 CSS 的 white-space 属性,将其设置为 nowrap,这样文字就不会自动换行了。
添加了以下 CSS 样式:
table.pure-table td:last-child,
table.pure-table th:last-child {
white-space: nowrap;
}
这样,最后一列中文就不会自动换行了。其中 table.pure-table td:last-child 和 table.pure-table th:last-child 选择器用于指定表格中的最后一列单元格,white-space: nowrap; 则是将 white-space 属性设置为 nowrap,禁止其自动换行。
position: sticky 是 CSS 中的一种定位方式,可以实现元素在滚动时固定在屏幕上,直到其滚动到指定位置。相对于 position: fixed,position: sticky 有以下优点和缺点:
优点:
缺点:
综上所述,position: sticky 是一种灵活且易用的定位方式,具有很多优点,但在兼容性和性能方面有一些缺点需要注意。如果要在项目中使用 position: sticky,需要在兼容性和性能方面进行综合考虑,以确保其正常运行和良好的用户体验。
.pure-table-wrapper-div {
overflow-x: scroll;
margin-left: 5em;
}
table.pure-table tr td:first-child,
table.pure-table tr th:first-child {
position: absolute;
width: 5em;
left: 0;
}
这段 CSS 代码主要是为了实现固定表格首列和横向滚动效果,并且在表格首列添加了固定的宽度。
首先,.pure-table-wrapper-div 是一个包裹表格的 div 元素,通过设置 overflow-x: scroll,实现了横向滚动的效果。同时,通过设置 margin-left: 5em,在左侧添加了 5em 的空白,使得表格不会紧贴在页面最左侧,美观性更好。
接着,table.pure-table tr td:first-child 和 table.pure-table tr th:first-child 选择器用于选中表格中的第一列单元格,使用 position: absolute 将其从文档流中脱离,并使用 width: 5em 指定其宽度为 5em,然后通过 left: 0 将其固定在表格最左侧。
这样,表格的首列就被固定在了左侧,不会随着表格的滚动而移动,同时也添加了固定的宽度,使得表格整体更加美观和易读。
总的来说,这段 CSS 代码实现了表格的固定首列和横向滚动效果,同时也为表格首列添加了固定的宽度,提高了表格的可读性和美观性。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="./purecss@3.0.0.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<style>
.pure-table-wrapper-div {
overflow-x: scroll;
margin-left: 5em;
}
table.pure-table tr td:first-child,
table.pure-table tr th:first-child {
position: absolute;
width: 5em;
left: 0;
}
</style>
<div class="pure-table-wrapper-div">
<table class="pure-table">
<thead>
<tr>
<th>#</th>
<th>Make</th>
<th>Model</th>
<th>随机数</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Honda</td>
<td>Accord</td>
<td>2009-QWERTYUIOPASDFGHJKLZXCVBNM</td>
</tr>
<tr>
<td>2</td>
<td>Toyota</td>
<td>Camry</td>
<td>2012-QWERTYUIOPASDFGHJKLZXCVBNM</td>
</tr>
<tr>
<td>3</td>
<td>Hyundai</td>
<td>Elantra</td>
<td>2010-QWERTYUIOPASDFGHJKLZXCVBNM</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
CSS 中的 position: absolute 定位方式可以让元素脱离文档流,并相对于它的最近的非 static 定位祖先元素进行定位。相对于其他定位方式,position: absolute 有以下优点和缺点:
优点:
缺点:
综上所述,position: absolute 是一种灵活且强大的定位方式,可以实现很多独特的布局效果,但需要注意它可能会对页面布局和元素位置产生影响。在使用时,需要根据具体情况进行综合考虑,并在保证页面布局和元素位置正确的前提下,尽可能地利用其优点实现更好的页面效果。
CSS table表格 thead固定 tbody滚动效果
由于项目需要,在表格中,当数据量越来越多时,就会出现滚动条,而在滚动的过程中,默认情况下表格头部会跟着表格内容一起滚动,导致看不到头部对应的字段名,影响体验效果!
实现思路:
将内容要滚动的区域控制在 tbody 标签中添加 overflow-y: auto; 样式,给 tr 标签添加 table-layout:fixed; (这是核心)样式,由于 tbody 有了滚动条后,滚动条也要占位,又会导致 tbody 和 thead 不对齐,所以在设置 tbody 的宽度时要把滚动条的宽度也加上【如果不想显示滚动条的话,可以把滚动条的宽度设置为0px,滚动条就没有了。
下面是效果图,具体完整实例代码也在下面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>纯CSS table表格 thead固定 tbody滚动</title>
<style>
.table-box {
margin: 100px auto;
width: 1024px;
}
/* 滚动条宽度 */
::-webkit-scrollbar {
width: 8px;
background-color: transparent;
}
/* 滚动条颜色 */
::-webkit-scrollbar-thumb {
background-color: #27314d;
}
table {
width: 100%;
border-spacing: 0px;
border-collapse: collapse;
}
table caption{
font-weight: bold;
font-size: 24px;
line-height: 50px;
}
table th, table td {
height: 50px;
text-align: center;
border: 1px solid gray;
}
table thead {
color: white;
background-color: #38F;
}
table tbody {
display: block;
width: calc(100% + 8px); /*这里的8px是滚动条的宽度*/
height: 300px;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
}
table tfoot {
background-color: #71ea71;
}
table thead tr, table tbody tr, table tfoot tr {
box-sizing: border-box;
table-layout: fixed;
display: table;
width: 100%;
}
table tbody tr:nth-of-type(odd) {
background: #EEE;
}
table tbody tr:nth-of-type(even) {
background: #FFF;
}
table tbody tr td{
border-bottom: none;
}
</style>
</head>
<body>
<section class="table-box">
<table cellpadding="0" cellspacing="0">
<caption>纯CSS table表格 thead固定 tbody滚动</caption>
<thead>
<tr>
<th>序 号</th>
<th>姓 名</th>
<th>年 龄</th>
<th>性 别</th>
<th>手 机</th>
</tr>
</thead>
<tbody>
<tr>
<td>001</td>
<td>Name</td>
<td>28</td>
<td>女</td>
<td>Mobile</td>
</tr>
<tr>
<td>002</td>
<td>Name</td>
<td>28</td>
<td>男</td>
<td>Mobile</td>
</tr>
<tr>
<td>003</td>
<td>Name</td>
<td>28</td>
<td>女</td>
<td>Mobile</td>
</tr>
<tr>
<td>004</td>
<td>Name</td>
<td>28</td>
<td>男</td>
<td>Mobile</td>
</tr>
<tr>
<td>005</td>
<td>Name</td>
<td>28</td>
<td>女</td>
<td>Mobile</td>
</tr>
<tr>
<td>006</td>
<td>Name</td>
<td>28</td>
<td>男</td>
<td>Mobile</td>
</tr>
<tr>
<td>007</td>
<td>Name</td>
<td>28</td>
<td>女</td>
<td>Mobile</td>
</tr>
<tr>
<td>008</td>
<td>Name</td>
<td>28</td>
<td>男</td>
<td>Mobile</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5">【table,thead,tbody,tfoot】 colspan:合并行, rowspan:合并列 </td>
</tr>
</tfoot>
</table>
</section>
</body>
</html>
我自己是一名从事了多年开发的web前端老程序员,目前辞职在做自己的web前端私人定制课程,今年年初我花了一个月整理了一份最适合2019年学习的web前端学习干货,各种框架都有整理,送给每一位前端小伙伴,想要获取的可以关注我的头条号并在后台私信我:前端,即可免费获取。
原文链接:https://blog.csdn.net/muguli2008/article/details/103787152
* {
margin: 0;
padding: 0;
}
.virtual-scroll-viewport {
position: relative;
width: 240px;
height: 300px;
margin: 150px auto 0;
overflow: auto;
will-change: scroll-position;
border: 1px solid #aaaaaa;
}
/* 撑开高度 */
.virtual-scroll-spacer {
position: absolute;
top: 0;
left: 0;
right: 0;
}
.virtual-scroll-list {
position: absolute;
top: 0;
left: 0;
right: 0;
}
.virtual-scroll-item {
height: 50px;
padding-left: 15px;
line-height: 50px;
}
.virtual-scroll-item:nth-child(2n) {
background: #f6f8fa;
}
<div class="virtual-scroll-viewport">
<div class="virtual-scroll-spacer"></div>
<div class="virtual-scroll-list"></div>
</div>
*请认真填写需求信息,我们会在24小时内与您取得联系。