您好,登錄后才能下訂單哦!
這篇文章主要介紹了html中如何實(shí)現(xiàn)表頭不動(dòng),具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
html實(shí)現(xiàn)表頭不動(dòng)的方法:首先將內(nèi)容要滾動(dòng)的區(qū)域控制在tbody標(biāo)簽中,并添加“overflow-y: auto;”樣式;然后給tr標(biāo)簽添加“table-layout:fixed;”即可固定表頭。
本文操作環(huán)境:windows7系統(tǒng)、HTML5&&CSS3版、Dell G3電腦。
HTML table表格 固定表頭 tbody加滾動(dòng)條
純CSS table表格 thead固定 tbody滾動(dòng)效果
由于項(xiàng)目需要,在表格中,當(dāng)數(shù)據(jù)量越來越多時(shí),就會(huì)出現(xiàn)滾動(dòng)條,而在滾動(dòng)的過程中,默認(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)條就沒有了。
下面是效果圖,具體完整實(shí)例代碼也在下面:
完整實(shí)例代碼:
<!DOCTYPE html> <html> <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> <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>
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“html中如何實(shí)現(xiàn)表頭不動(dòng)”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。