推荐:用CSS实现的固定表头的HTML表格

原创 2004年10月31日 14:32:00

曾经在项目中实现过一个固定表头的HTML表格,但使用了非常臃肿的代码,因为实际上是画了三个一样的表格。偶然的机会,发现了一个纯粹用HTML和CSS实现的固定表头的表格。现将其简化代码摘录如下。
原地址见:
http://www.imaputz.com/cssStuff/bulletVersion.html

<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, <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Geneva, Arial, Helvetica, sans-serif;
 margin: 10px;
 padding: 0
}<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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%;
}

/* WinIE 6.x needs to re-account for it's scrollbar. Give it some padding */
/html div.tableContainer/* */ {
 padding: 0 16px 0 0
}

/* clean up for allowing display Opera 5.x/6.x and MacIE 5.x */
html>body div.tableContainer {
 height: auto;
 padding: 0;
 width: 740px
}

/* Reset overflow value to hidden for all non-IE browsers. */
/* Filter out Opera 5.x/6.x and MacIE 5.x                  */
head:first-child+body div[class].tableContainer {
 height: 285px;
 overflow: hidden;
 width: 756px
}

/* define width of table. IE browsers only                 */
/* if width is set to 100%, you can remove the width       */
/* property from div.tableContainer and have the div scale */
div.tableContainer table {
 float: left;
 width: 100%
}

/* WinIE 6.x needs to re-account for padding. Give it a negative margin */
/html div.tableContainer table/* */ {
 margin: 0 -16px 0 0
}

/* define width of table. Opera 5.x/6.x and MacIE 5.x */
html>body div.tableContainer table {
 float: none;
 margin: 0;
 width: 740px
}

/* define width of table. Add 16px to width for scrollbar.           */
/* All other non-IE browsers. Filter out Opera 5.x/6.x and MacIE 5.x */
head:first-child+body div[class].tableContainer table {
 width: 756px
}

/* set table header to a fixed position. WinIE 6.x only                                       */
/* In WinIE 6.x, any element with a position property set to relative and is a child of       */
/* an element that has an overflow property set, the relative value translates into fixed.    */
/* Ex: parent element DIV with a class of tableContainer has an overflow property set to auto */
thead.fixedHeader tr {
 position: relative;
 /* expression is for WinIE 5.x only. Remove to validate and for pure CSS solution      */
 top: expression(document.getElementById("tableContainer").scrollTop)
}


/* set THEAD element to have block level attributes. All other non-IE browsers            */
/* this enables overflow to work on TBODY element. All other non-IE, non-Mozilla browsers */
/* Filter out Opera 5.x/6.x and MacIE 5.x                                                 */
head:first-child+body thead[class].fixedHeader tr {
 display: block
}

/* make the TH elements pretty */
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
}

/* make the A elements pretty. makes for nice clickable headers                */
thead.fixedHeader a, thead.fixedHeader a:link, thead.fixedHeader a:visited {
 color: #FFF;
 display: block;
 text-decoration: none;
 width: 100%
}

/* make the A elements pretty. makes for nice clickable headers                */
/* WARNING: swapping the background on hover may cause problems in WinIE 6.x   */
thead.fixedHeader a:hover {
 color: #FFF;
 display: block;
 text-decoration: underline;
 width: 100%
}

/* define the table content to be scrollable                                              */
/* set TBODY element to have block level attributes. All other non-IE browsers            */
/* this enables overflow to work on TBODY element. All other non-IE, non-Mozilla browsers */
/* induced side effect is that child TDs no longer accept width: auto                     */
/* Filter out Opera 5.x/6.x and MacIE 5.x                                                 */
head:first-child+body tbody[class].scrollContent {
 display: block;
 height: 262px;
 overflow: auto;
 width: 100%
}

/* make TD elements pretty. Provide alternating classes for striping the table */
/*
http://www.alistapart.com/articles/zebratables/                             */
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
}

/* define width of TH elements: 1st, 2nd, and 3rd respectively.      */
/* All other non-IE browsers. Filter out Opera 5.x/6.x and MacIE 5.x */
/* Add 16px to last TH for scrollbar padding                         */
/*
http://www.w3.org/TR/REC-CSS2/selector.html#adjacent-selectors    */
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
}

/* define width of TH elements: 1st, 2nd, and 3rd respectively.      */
/* All other non-IE browsers. Filter out Opera 5.x/6.x and MacIE 5.x */
/* Add 16px to last TH for scrollbar padding                         */
/*
http://www.w3.org/TR/REC-CSS2/selector.html#adjacent-selectors    */
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

/* expression is for WinIE 5.x only. Remove to validate and for pure CSS solution */
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>

 


 

CSS实现固定表头 HTML表格

html>head>title>title>link rel="stylesheet" href="demo.css" type="text/css">head>body>p>这段空间的高度会自动计算...
  • sunyujia
  • sunyujia
  • 2008年02月14日 20:51
  • 2530

CSS固定表格表头

固定表格表头 div{ overflow-y: scroll; overflow-x: hidden; height: 100px; margin-top: 5px...
  • longvs
  • longvs
  • 2013年10月30日 17:22
  • 4064

css+html实现横纵表头锁定的表格

.div1{BORDER-RIGHT: #808080 2px solid; BORDER-TOP: #808080 2px solid; DISPLAY: inline; OVERFLOW: a...
  • precipitant
  • precipitant
  • 2006年11月25日 12:28
  • 4692

table表头和首列的表格固定-CSS实现的Table表头固定

效果就是上图 表头是固定的, 跟随滑动轴动,左边第一列也是可以跟谁滑动轴滚动 下面是代码原文是查看 ...
  • q3585914
  • q3585914
  • 2017年04月10日 14:06
  • 11199

用CSS实现的固定表头的HTML表格

http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">body { background: #FFF; color: #000; font: no...
  • hrstudy
  • hrstudy
  • 2004年11月09日 17:10
  • 1275

css+html横纵表头锁定的表格 程序的说明

完全依靠css+html实现的可以锁定横纵表头的表格。并且横纵表头都已分栏显示。只不过横纵表头分别z在左侧和上部。还不能实现右侧和下部的表尾的锁定。所以还无法添加锁定统计列。但是他的简单已经优于其它解...
  • precipitant
  • precipitant
  • 2006年11月27日 09:53
  • 6684

纯css实现table表头固定

页面table通常都需要做表头固定,参考了n种实现方式,最终还是选择了用纯css来做表头固定,只是因为方便,易于调试。 css代码如下,table通用,仅供参考。          tab...
  • xiaojunjuns1
  • xiaojunjuns1
  • 2017年07月10日 15:47
  • 2443

js、css实现table表头固定

原生table表头随表体一起上下滚动 在html中利用js、css将表头固定,可将原table拆分,分为表头table,表体table。 大致框架如: ...
  • Eric19911992
  • Eric19911992
  • 2017年03月09日 09:45
  • 556

表头固定Demo(Div+Css)

表头固定Demo             .frozenHeadTable         {          width:821px;          border-right:1p...
  • LazyLion2010
  • LazyLion2010
  • 2014年03月19日 16:36
  • 3093

固定表头可以纵向滚动的html表格

设计思路: 将表格的表头与内容分别放到两个标签里,然后将其放在一个里以使表头和表格的总宽度一致,将表格内容放到一个可滚动的div()里,为每个单元格添加类来固定宽度以实现表头与内容的单元格宽度一...
  • Jjwxyz
  • Jjwxyz
  • 2016年01月27日 15:42
  • 1753
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:推荐:用CSS实现的固定表头的HTML表格
举报原因:
原因补充:

(最多只允许输入30个字)