Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
果
.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:
這個(gè)類名應(yīng)用于包裹表格的 div 元素。它的樣式規(guī)則如下:
overflow-x: scroll;: 當(dāng)內(nèi)容寬度超出容器寬度時(shí),顯示水平滾動(dòng)條。這允許用戶在需要時(shí)水平滾動(dòng)查看表格的所有列。
示例 HTML 結(jié)構(gòu):
<div class="pure-table-wrapper-div"> <table class="pure-table"> <!-- 表格內(nèi)容 --> </table> </div>
table.pure-table th:first-child, table.pure-table td:first-child: 這兩個(gè)選擇器分別針對(duì) .pure-table 類的表格中的第一個(gè)表頭單元格(<th>)和第一個(gè)數(shù)據(jù)單元格(<td>)。
它們定義的樣式規(guī)則如下:
position: sticky;: 使用黏性定位。黏性定位元素在滾動(dòng)時(shí)會(huì)根據(jù)設(shè)置的 top、bottom、left 或 right 屬性值在特定位置 "粘附"。在本例中,由于定義了 left: 0;,這些元素會(huì)在左側(cè)邊緣粘附。 left: 0;: 在元素滾動(dòng)到視口左側(cè)邊緣時(shí)使其粘附。與 position: sticky; 結(jié)合使用。
z-index: 1;: 設(shè)置元素的堆疊順序。值越大,元素越靠前。在這種情況下,將第一個(gè)單元格設(shè)置為 z-index: 1 可確保它在其他表格單元格之上。
background-color: aliceblue;: 為匹配的單元格設(shè)置背景顏色。這可以增強(qiáng)視覺(jué)效果,使粘附的單元格與其他單元格區(qū)分開(kāi)。
這段 CSS 代碼的主要目的是實(shí)現(xiàn)表格的水平滾動(dòng),并固定第一列,使其在水平滾動(dòng)時(shí)保持可見(jiàn)。同時(shí),為第一列的單元格設(shè)置了背景顏色以增強(qiáng)視覺(jué)效果。
總的來(lái)說(shuō)是:
使用 position: sticky; 來(lái)固定第一列。在 table.pure-table th:first-child 中設(shè)置了 position: sticky; 和 left: 0;,并將 z-index 設(shè)為 1,以確保第一列在滾動(dòng)時(shí)會(huì)固定在屏幕上方。
<!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>型號(hào)</th>
<th>隨機(jī)字符串</th>
<th>說(shuō)明</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>本田</td>
<td>雅閣</td>
<td>2009-QWERTYUIOPASDFGHJKLZXCVBNM</td>
<td>這是一段中文說(shuō)明文字。</td>
</tr>
<tr>
<td>2</td>
<td>豐田</td>
<td>凱美瑞</td>
<td>2012-QWERTYUIOPASDFGHJKLZXCVBNM</td>
<td>這是一段中文說(shuō)明文字。</td>
</tr>
<tr>
<td>3</td>
<td>現(xiàn)代</td>
<td>領(lǐng)動(dòng)</td>
<td>2010-QWERTYUIOPASDFGHJKLZXCVBNM</td>
<td>這是一段中文說(shuō)明文字。</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
要去除表格中最后一列中文的換行效果,可以使用 CSS 的 white-space 屬性,將其設(shè)置為 nowrap,這樣文字就不會(huì)自動(dòng)換行了。
添加了以下 CSS 樣式:
table.pure-table td:last-child,
table.pure-table th:last-child {
white-space: nowrap;
}
這樣,最后一列中文就不會(huì)自動(dòng)換行了。其中 table.pure-table td:last-child 和 table.pure-table th:last-child 選擇器用于指定表格中的最后一列單元格,white-space: nowrap; 則是將 white-space 屬性設(shè)置為 nowrap,禁止其自動(dòng)換行。
position: sticky 是 CSS 中的一種定位方式,可以實(shí)現(xiàn)元素在滾動(dòng)時(shí)固定在屏幕上,直到其滾動(dòng)到指定位置。相對(duì)于 position: fixed,position: sticky 有以下優(yōu)點(diǎn)和缺點(diǎn):
優(yōu)點(diǎn):
缺點(diǎn):
綜上所述,position: sticky 是一種靈活且易用的定位方式,具有很多優(yōu)點(diǎn),但在兼容性和性能方面有一些缺點(diǎn)需要注意。如果要在項(xiàng)目中使用 position: sticky,需要在兼容性和性能方面進(jìn)行綜合考慮,以確保其正常運(yùn)行和良好的用戶體驗(yàn)。
.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 代碼主要是為了實(shí)現(xiàn)固定表格首列和橫向滾動(dòng)效果,并且在表格首列添加了固定的寬度。
首先,.pure-table-wrapper-div 是一個(gè)包裹表格的 div 元素,通過(guò)設(shè)置 overflow-x: scroll,實(shí)現(xiàn)了橫向滾動(dòng)的效果。同時(shí),通過(guò)設(shè)置 margin-left: 5em,在左側(cè)添加了 5em 的空白,使得表格不會(huì)緊貼在頁(yè)面最左側(cè),美觀性更好。
接著,table.pure-table tr td:first-child 和 table.pure-table tr th:first-child 選擇器用于選中表格中的第一列單元格,使用 position: absolute 將其從文檔流中脫離,并使用 width: 5em 指定其寬度為 5em,然后通過(guò) left: 0 將其固定在表格最左側(cè)。
這樣,表格的首列就被固定在了左側(cè),不會(huì)隨著表格的滾動(dòng)而移動(dòng),同時(shí)也添加了固定的寬度,使得表格整體更加美觀和易讀。
總的來(lái)說(shuō),這段 CSS 代碼實(shí)現(xiàn)了表格的固定首列和橫向滾動(dòng)效果,同時(shí)也為表格首列添加了固定的寬度,提高了表格的可讀性和美觀性。
<!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>隨機(jī)數(shù)</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 定位方式可以讓元素脫離文檔流,并相對(duì)于它的最近的非 static 定位祖先元素進(jìn)行定位。相對(duì)于其他定位方式,position: absolute 有以下優(yōu)點(diǎn)和缺點(diǎn):
優(yōu)點(diǎn):
缺點(diǎn):
綜上所述,position: absolute 是一種靈活且強(qiáng)大的定位方式,可以實(shí)現(xiàn)很多獨(dú)特的布局效果,但需要注意它可能會(huì)對(duì)頁(yè)面布局和元素位置產(chǎn)生影響。在使用時(shí),需要根據(jù)具體情況進(jìn)行綜合考慮,并在保證頁(yè)面布局和元素位置正確的前提下,盡可能地利用其優(yōu)點(diǎn)實(shí)現(xiàn)更好的頁(yè)面效果。
CSS table表格 thead固定 tbody滾動(dòng)效果
由于項(xiàng)目需要,在表格中,當(dāng)數(shù)據(jù)量越來(lái)越多時(shí),就會(huì)出現(xiàn)滾動(dòng)條,而在滾動(dòng)的過(guò)程中,默認(rèn)情況下表格頭部會(huì)跟著表格內(nèi)容一起滾動(dòng),導(dǎo)致看不到頭部對(duì)應(yīng)的字段名,影響體驗(yàn)效果!
實(shí)現(xiàn)思路:
將內(nèi)容要滾動(dòng)的區(qū)域控制在 tbody 標(biāo)簽中添加 overflow-y: auto; 樣式,給 tr 標(biāo)簽添加 table-layout:fixed; (這是核心)樣式,由于 tbody 有了滾動(dòng)條后,滾動(dòng)條也要占位,又會(huì)導(dǎo)致 tbody 和 thead 不對(duì)齊,所以在設(shè)置 tbody 的寬度時(shí)要把滾動(dòng)條的寬度也加上【如果不想顯示滾動(dòng)條的話,可以把滾動(dòng)條的寬度設(shè)置為0px,滾動(dòng)條就沒(méi)有了。
下面是效果圖,具體完整實(shí)例代碼也在下面:
<!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滾動(dòng)</title>
<style>
.table-box {
margin: 100px auto;
width: 1024px;
}
/* 滾動(dòng)條寬度 */
::-webkit-scrollbar {
width: 8px;
background-color: transparent;
}
/* 滾動(dòng)條顏色 */
::-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是滾動(dòng)條的寬度*/
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滾動(dòng)</caption>
<thead>
<tr>
<th>序 號(hào)</th>
<th>姓 名</th>
<th>年 齡</th>
<th>性 別</th>
<th>手 機(jī)</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>
我自己是一名從事了多年開(kāi)發(fā)的web前端老程序員,目前辭職在做自己的web前端私人定制課程,今年年初我花了一個(gè)月整理了一份最適合2019年學(xué)習(xí)的web前端學(xué)習(xí)干貨,各種框架都有整理,送給每一位前端小伙伴,想要獲取的可以關(guān)注我的頭條號(hào)并在后臺(tái)私信我:前端,即可免費(fèi)獲取。
原文鏈接: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;
}
/* 撐開(kāi)高度 */
.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>
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。