CSS教程,CSS固定表头的HTML表格

时间:2021-05-08


  用CSS实现的固定表头的HTML表格。
曾经在项目中实现过一个固定表头的HTML表格,但使用了非常臃肿的代码,因为实际上是画了三个一样的表格。一个纯粹用HTML和CSS实现的固定表头的表格,其简化代码如下:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
<style type="text/css">
<!--
body {
background: #FFF;
color: #000;
font: normal normal 12px Verdana, Geneva, Arial, Helvetica, sans-serif;
margin: 10px;
padding: 0
}
table, td, a {
color: #000;
font: normal normal 12px Verdana, Geneva, Arial, Helvetica, sans-serif
}
.td
{
nowrap:'true';
}
div.tableContainer {
clear: both;
border: 1px solid #963;
height: 285px;
overflow: auto;
width: 100%;
}

\html div.tableContainer {
padding: 0 16px 0 0
}

html>body div.tableContainer {
height: auto;
padding: 0;
width: 740px
}


head:first-child body div[class].tableContainer {
height: 285px;
overflow: hidden;
width: 756px
}



div.tableContainer table {
float: left;
width: 100%
}

\html div.tableContainer table {
margin: 0 -16px 0 0
}

html>body div.tableContainer table {
float: none;
margin: 0;
width: 740px
}


head:first-child body div[class].tableContainer table {
width: 756px
}




thead.fixedHeader tr {
position: relative;

top: expression(document.getElementById("tableContainer").scrollTop)



head:first-child body thead[class].fixedHeader tr {
display: block
}

thead.fixedHeader th {
background: #C96;
border-left: 1px solid #EB8;
border-right: 1px solid #B74;
border-top: 1px solid #EB8;
font-weight: normal;
padding: 4px 3px;
text-align: center
}

thead.fixedHeader a, thead.fixedHeader a:link, thead.fixedHeader a:visited {
color: #FFF;
display: block;
text-decoration: none;
width: 100%
}


thead.fixedHeader a:hover {
color: #FFF;
display: block;
text-decoration: underline;
width: 100%
}





head:first-child body tbody[class].scrollContent {
display: block;
height: 262px;
overflow: auto;
width: 100%
}


tbody.scrollContent td, tbody.scrollContent tr.normalRow td {
background: #FFF;
border-bottom: 1px solid #EEE;
border-left: 1px solid #EEE;
border-right: 1px solid #AAA;
border-top: 1px solid #AAA;
padding: 2px 3px
}
tbody.scrollContent tr.alternateRow td {
background: #EEE;
border-bottom: 1px solid #EEE;
border-left: 1px solid #EEE;
border-right: 1px solid #AAA;
border-top: 1px solid #AAA;
padding: 2px 3px
}




head:first-child body thead[class].fixedHeader th {
width: 200px
}
head:first-child body thead[class].fixedHeader th th {
width: 250px
}
head:first-child body thead[class].fixedHeader th th th {
border-right: none;
padding: 4px 4px 4px 3px;
width: 316px
}




head:first-child body tbody[class].scrollContent td {
width: 200px
}
head:first-child body tbody[class].scrollContent td td {
width: 250px
}
head:first-child body tbody[class].scrollContent td td td {
border-right: none;
padding: 2px 4px 2px 3px;
width: 300px

top: expression(document.getElementById("tableContainer").scrollTop)
}
-->
</style>
</head><body>
<div id="tableContainer" class="tableContainer">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="scrollTable">
<thead class="fixedHeader">
<tr>
<th><a href="#">Header 1</a></th>
<th><a href="#">Header 2</a></th>
<th><a href="#">Header 3</a></th>
<th><a href="#">Header 4</a></th>
<th><a href="#">Header 5</a></th>
<th><a href="#">Header 6</a></th>
<th><a href="#">Header 7</a></th>
</tr>
</thead>
<tbody class="scrollContent">
<tr>
<td>Cell Content 1</td>
<td>Cell Content 2</td>
<td>Cell Content 3</td>
<td>Cell Content 4</td>
<td>Cell Content 5</td>
<td>Cell Content 6</td>
<td>Cell Content 7</td>
</tr>
<tr>
<td>More Cell Content 1</td>
<td>More Cell Content 2</td>
<td>More Cell Content 3</td>
<td>More Cell Content 4</td>
<td>More Cell Content 5</td>
<td>More Cell Content 6</td>
<td>More Cell Content 7</td>
</tr>
<tr>
<td nowrap>Even More Cell Content 1</td>
<td nowrap>Even More Cell Content 2</td>
<td nowrap>Even More Cell Content 3</td>
<td nowrap>Even More Cell Content 4</td>
<td nowrap>Even More Cell Content 5</td>
<td nowrap>Even More Cell Content 6</td>
<td nowrap>Even More Cell Content 7</td>
</tr>
<tr>
<td>And Repeat 1</td>
<td>And Repeat 2</td>
<td>And Repeat 3</td>
<td>And Repeat 4</td>
<td>And Repeat 5</td>
<td>And Repeat 6</td>
<td>And Repeat 7</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章