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;
}
body,
html {
height: 100%;
}
body {
font-size: 100%;
font-family: Roboto, sans-serif;
color: #4d4d4d;
background-color: #fff;
}
.cd-main-content {
height: 100%;
position: relative;
z-index: 1;
}
.cd-fixed-bg.cd-bg-1 {
background-image: url(https://www.csdn.net/company/img/cd-background-4.jpg);
}
@media only screen and (min-width: 1170px) {
.cd-fixed-bg {
background-attachment: fixed;
background-attachment: scroll\9;
}
}
.cd-fixed-bg {
position: relative;
min-height: 100%;
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
z-index: 1;
}
.picinfo {
position: absolute;
left: 50%;
bottom: 40px;
right: auto;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
width: 90%;
max-width: 900px;
font-size: 30px;
font-size: 1.875rem;
text-shadow: 0 1px 3px rgb(0 0 0 / 30%);
color: #fff;
}
.cd-fixed-bg h1,
.cd-fixed-bg h2 {
padding-top: 168px;
}
@media only screen and (min-width: 1170px) {
.cd-fixed-bg h1,
.cd-fixed-bg h2 {
font-size: 48px;
font-weight: 300;
}
}
.picinfo span {
display: inline-block;
line-height: 24px;
padding: 10px 0 0 0;
margin: 10px 0 0 0;
font-size: 16px;
border-top: .5px #ccc solid;
}
.cd-scrolling-bg.cd-color-2 {
background-color: #fff;
color: #4d4d4d;
}
@media only screen and (min-width: 768px) {
.cd-scrolling-bg {
padding: 1em 0;
font-size: 20px;
font-size: 1.25rem;
line-height: 2;
font-weight: 300;
}
}
.cd-scrolling-bg {
position: relative;
padding: 1em 0;
line-height: 1.6;
z-index: 2;
}
.cd-container {
width: 90%;
max-width: 900px;
margin: 0 auto;
font-size: 15px;
}
.cd-fixed-bg.cd-bg-4 {
background-image: url(https://www.csdn.net/company/img/cd-background-4.jpg);
}
@media only screen and (min-width: 1170px) {
.cd-fixed-bg {
background-attachment: fixed;
background-attachment: scroll\9;
}
}
.cd-fixed-bg {
position: relative;
min-height: 100%;
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
z-index: 1;
}
.picinfo {
position: absolute;
left: 50%;
bottom: 40px;
right: auto;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
width: 90%;
max-width: 900px;
font-size: 30px;
font-size: 1.875rem;
text-shadow: 0 1px 3px rgb(0 0 0 / 30%);
color: #fff;
}
.cd-scrolling-bg.cd-color-3 {
color: #3d3536;
}
@media only screen and (min-width: 768px) {
.cd-scrolling-bg {
padding: 1em 0;
font-size: 20px;
font-size: 1.25rem;
line-height: 2;
font-weight: 300;
}
}
.cd-scrolling-bg {
position: relative;
padding: 1em 0;
line-height: 1.6;
z-index: 2;
}
.cd-container {
width: 90%;
max-width: 900px;
margin: 0 auto;
font-size: 15px;
}
.chart-1 {
display: block;
width: 100%;
max-width: 800px;
margin: 2em auto 0;
}
.cd-fixed-bg.cd-bg-5 {
background-image: url(https://www.csdn.net/company/img/cd-background-5.jpg);
}
@media only screen and (min-width: 1170px) {
.cd-fixed-bg {
background-attachment: fixed;
background-attachment: scroll\9;
}
}
.cd-fixed-bg {
position: relative;
min-height: 100%;
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
z-index: 1;
}
求:當頁面很長,有滾動條時,需要固定頁面上的部分元素,便于下方元素和上方固定元素的對照。
起始頁面布局
固定效果
*請認真填寫需求信息,我們會在24小時內與您取得聯系。