整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          HTML table表格 固定表頭 tbody加滾動條

          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

          果圖

          實現頁面滾動,背景圖穿過的效果

          注意:內部圖片地址,自己替換成自己的圖片


          示例代碼

          css樣式代碼

          * {
          	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;
          }

          html主要結構代碼

          求:當頁面很長,有滾動條時,需要固定頁面上的部分元素,便于下方元素和上方固定元素的對照。

          起始頁面布局


          固定效果


          主站蜘蛛池模板: 波多野结衣一区在线| 亚洲一区无码中文字幕| 中文字幕AV一区二区三区人妻少妇| 色婷婷一区二区三区四区成人网 | 在线|一区二区三区| 波多野结衣电影区一区二区三区 | 国产一区二区在线观看麻豆 | 日本一区二三区好的精华液| av在线亚洲欧洲日产一区二区| 人成精品视频三区二区一区 | 日韩高清一区二区| 一区二区三区日韩精品| 国产激情精品一区二区三区| 国产对白精品刺激一区二区| 中文字幕乱码一区二区免费| 国产福利日本一区二区三区| 久久精品免费一区二区喷潮| 乱中年女人伦av一区二区| 国产免费一区二区三区VR| 多人伦精品一区二区三区视频| 精品久久久久久中文字幕一区| 国产精品一区二区久久国产| 精品一区二区三区免费毛片| 亚洲AV无码一区二区三区鸳鸯影院 | 国产精品被窝福利一区 | 日本不卡一区二区视频a| 国产在线一区二区杨幂| 美女视频一区二区三区| 午夜无码一区二区三区在线观看| 国产午夜精品一区理论片| 日本免费一区尤物| 一区二区在线视频免费观看| 日韩免费一区二区三区在线 | 精品国产一区二区三区麻豆| 综合一区自拍亚洲综合图区| 国产日本亚洲一区二区三区| 国产一区二区三区在线看片| 国产高清一区二区三区四区| 中文字幕一区二区区免| 亚洲日本va午夜中文字幕一区| 亚洲视频一区网站|