纯CSS实现表头固定

纯 CSS 实现表头固定之所以难,主要在两点。一是占有最大市场份额的 IE6 不支持 position:fixed。另一个,是人们想破头都想在一起表格中实现这种效果。不过外国真的人用纯 CSS 实现了这种效果,动用了数量惊人的 CSS hacks……我觉得,如果搞到代码如此难懂且难扩展,还不如用 javascript 好了。碰巧今天我也遇到这种需求,换个视角想想,真的搞出来了。

我们知道,CSS 是负责表现,HTML 是负责结构,同样的结构,换个样式,给人的感觉完全不同,这也说明人的眼睛是很容易受骗。因此前些狂热鼓吹 DIV+CSS 的日子里,人们总是想在页面去掉 table,用 div+span 弄出了一个个“table”来。虽然这种事是不可取,但也告诉我们,table 做得的事,通过一些组合我们也能做出来。换个思路来说,既然一个 table 做不了,就两个吧。上面的 table 模拟表头,下面的 table 模拟带滚动条的部分。在我们继续讲下去之前,我们先明确一下我们的需求吧,要不太抽象了。首先是表格为 4*9, 每列宽 170px, 总为 680px,滚动条在各浏览器默认为 16px,别忘了,width 是不包含 border 在内,四列就有 5 个纵向的 border,宽总长为 701px。

<table >
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

然后我们把这个 table 一分为二,第一个 table 为表头,第二个 table 要带滚动条,说明要在其父元素上应用 overflow 样式,因此它要外套一个 div。这个 div 与第一个 table 应该是等长的。不过不用花心思了,我们在它们的外面最套一个 div,设置其 width 为 701px,然后把这两个子元素的宽都设为 100% 就行了。注意,我们在 table 中显式添加 tbody 以提高表格的渲染效率。

<div id="scrollTable">
  <table class="thead">
    <tbody>
      <tr>
        <th>名称</th>
        <th>语法</th>
        <th>说明</th>
        <th>例子</th>
      </tr>
    </tbody>
  </table>
  <div>
    <table class="tbody">
      <tbody>
        <tr>
          <td>Simple attribute Selector</td>
          <td>[attr] </td>
          <td>选择具有此属性的元素</td>
          <td>blockquote[title] { <br/>color: red }</td>
        </tr>
        <tr>
          <td>attribute Value Selector</td>
          <td>[attr="value"] </td>
          <td>选出属性值精确等于给出值的元素</td>
          <td>h2[align="left"] { <br/>cursor: hand } </td>
        </tr>
        <tr>
          <td>"Begins-with" attribute Value Selector</td>
          <td>[attr^="value"] </td>
          <td>选出属性值以给出值开头的元素</td>
          <td>h2[align^="right"] { <br/>cursor: hand } </td>
        </tr>
        <tr>
          <td>"Ends-with" attribute Value Selector</td>
          <td>[attr$="value"] </td>
          <td>选出属性值以给出值结尾的元素</td>
          <td>div[class$="vml"]{<br/>cursor: hand} </td>
        </tr>
        <tr>
          <td>Substring-match attribute Value Selector</td>
          <td>[attr*="value"] </td>
          <td>选出属性值包含给出值的元素</td>
          <td>div[class*="grid"]{<br/> float:left} </td>
        </tr>
        <tr>
          <td>One-Of-Many Attribute Value Selector</td>
          <td>[attr~="value"] </td>
          <td>原元素的属性值为多个单词,给出值为其中一个。</td>
          <td>li[class~="last"]{<br/> padding-left:2em} </td>
        </tr>
        <tr>
          <td>Hyphen Attribute Value Selector</td>
          <td>[attr|="value"] </td>
          <td>原元素的属性值等于给出值,或者以给出值加“-”开头</td>
          <td>span[lang|="en"]{ <br/>color:green}</td>
        </tr>
        <tr>
          <td>反选属性值选择器</td>
          <td>[attr!="value"] </td>
          <td>非标准,jQuery中出现的</td>
          <td>span[class!="red"]{<br/>color:green}</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

表现层部分:

#scrollTable {
  width:701px;
  border: 1px solid #EB8;/*table没有外围的border,只有内部的td或th有border*/
  background: #FF8C00;
}
 
 
#scrollTable table {
  border-collapse:collapse; /*统一设置两个table为细线表格*/
}
 
#scrollTable table.thead{ /*表头*/
  /*div的第一个子元素*/
  width:100%;
}
 
#scrollTable table.thead th{/*表头*/
  border: 1px solid #EB8;
  border-right:#C96;
  color:#fff;
  background: #FF8C00;/*亮桔黄色*/
}
 
#scrollTable div{/*能带滚动条的表身*/
  /*div的第二个子元素*/
  width:100%;
  height:200px;
  overflow:auto;/*必需*/
}
 
#scrollTable table.tbody{/*能带滚动条的表身的正体*/
  width:100%;
  border: 1px solid #C96;
  border-right:#B74;
  color:#666666;
  background: #ECE9D8;
}
#scrollTable table.tbody td{/*能带滚动条的表身的格子*/
  border:1px solid #C96;
}
<!doctype html> <html dir="ltr" lang="zh-CN"> <head> <meta charset="utf-8"/> <title> 纯 CSS 实现表头固定 by 司徒正美 </title> <style type="text/css"> <pre><code> #scrollTable { width:701px; border: 1px solid #EB8;/*table 没有外围的 border,只有内部的 td 或 th 有 border*/ background: #FF8C00; } #scrollTable table {border-collapse:collapse; /* 统一设置两个 table 为细线表格 */} #scrollTable table.thead{ /* 表头 */ /*div 的第一个子元素 */ width:100%; } #scrollTable table.thead th{/* 表头 */ border: 1px solid #EB8; border-right:#C96; color:#fff; background: #FF8C00;/* 亮桔黄色 */ } #scrollTable div{/* 能带滚动条的表身 */ /*div 的第二个子元素 */ width:100%; height:200px; overflow:auto;/* 必需 */ } #scrollTable table.tbody{/* 能带滚动条的表身的正体 */ width:100%; border: 1px solid #C96; border-right:#B74; color:#666666; background: #ECE9D8; } #scrollTable table.tbody td{/* 能带滚动条的表身的格子 */ border:1px solid #C96; } &lt;/style&gt; </code></pre> <p></head><br /> <body><br /> <div id="scrollTable"><br /> <table class="thead"></p> <pre><code> &lt;tbody&gt; &lt;tr&gt; &lt;th&gt; 名称 &lt;/th&gt; &lt;th&gt; 语法 &lt;/th&gt; &lt;th&gt; 说明 &lt;/th&gt; &lt;th&gt; 例子 &lt;/th&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;div&gt; &lt;table class="tbody"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;Simple attribute Selector&lt;/td&gt; &lt;td&gt;[attr] &lt;/td&gt; &lt;td&gt; 选择具有此属性的元素 &lt;/td&gt; &lt;td&gt;blockquote[title] {&lt;br/&gt;color: red}&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;attribute Value Selector&lt;/td&gt; &lt;td&gt;[attr="value"] &lt;/td&gt; &lt;td&gt; 选出属性值精确等于给出值的元素 &lt;/td&gt; &lt;td&gt;h2[align="left"] {&lt;br/&gt;cursor: hand} &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;"Begins-with" attribute Value Selector&lt;/td&gt; &lt;td&gt;[attr^="value"] &lt;/td&gt; &lt;td&gt; 选出属性值以给出值开头的元素 &lt;/td&gt; &lt;td&gt;h2[align^="right"] {&lt;br/&gt;cursor: hand} &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;"Ends-with" attribute Value Selector&lt;/td&gt; &lt;td&gt;[attr$="value"] &lt;/td&gt; &lt;td&gt; 选出属性值以给出值结尾的元素 &lt;/td&gt; &lt;td&gt;div[class$="vml"]{&lt;br/&gt;cursor: hand} &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Substring-match attribute Value Selector&lt;/td&gt; &lt;td&gt;[attr*="value"] &lt;/td&gt; &lt;td&gt; 选出属性值包含给出值的元素 &lt;/td&gt; &lt;td&gt;div[class*="grid"]{&lt;br/&gt; float:left} &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;One-Of-Many Attribute Value Selector&lt;/td&gt; &lt;td&gt;[attr~="value"] &lt;/td&gt; &lt;td&gt; 原元素的属性值为多个单词,给出值为其中一个。&lt;/td&gt; &lt;td&gt;li[class~="last"]{&lt;br/&gt; padding-left:2em} &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Hyphen Attribute Value Selector&lt;/td&gt; &lt;td&gt;[attr|="value"] &lt;/td&gt; &lt;td&gt; 原元素的属性值等于给出值,或者以给出值加“-”开头 &lt;/td&gt; &lt;td&gt;span[lang|="en"]{&lt;br/&gt;color:green}&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; 反选属性值选择器 &lt;/td&gt; &lt;td&gt;[attr!="value"] &lt;/td&gt; &lt;td&gt; 非标准,jQuery 中出现的 &lt;/td&gt; &lt;td&gt;span[class!="red"]{&lt;br/&gt;color:green}&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p></body><br /> </html><br />

运行代码

发现表头的格子与表身的格子不对齐。这时我们需要动用 col 标签,col 允许我们统一设置 tbody 中索引值与它相同的 td 或 th 的背景色,文字对齐方式与宽度。虽然 CSS2.1 的相邻选择器与 CSS3 的子元素过滤伪类能让我们用更精简的方式设置它们,而且是样式与结构分离那种,可惜 IE 家族总是拖后腿。我们再看一下它们的长度,由于最后一个表格有可能受滚动条挤压而缩短长度,我们保证前三列长度相等就行了,剩余的都分配给最后一个,换言之,最后一个不用设置。另,IE 下可以设置滚动条的样式,我们也把玩一翻吧。

<table class="thead">
        <col width="170px"></col> <col width="170px"></col> <col width="170px"></col><col></col>
        <tbody>
//********************略*****************
        </tbody>
 </table>
 <div>
      <table class="tbody">
          <col width="170px"></col> <col width="170px"></col> <col width="170px"></col><col></col>
          <tbody>
//********************略*****************   
          </tbody>
      </table>
 </div>

表现层部分:

#scrollTable {
  width:701px;
  border: 1px solid #EB8;/*table没有外围的border,只有内部的td或th有border*/
  background: #FF8C00;
}
 
#scrollTable table {
  border-collapse:collapse; /*统一设置两个table为细线表格*/
}
/*表头 div的第一个子元素**/
#scrollTable table.thead{
  width:100%;
}
/*表头*/
#scrollTable table.thead th{
  border: 1px solid #EB8;
  border-right:#C96;
  color:#fff;
  background: #FF8C00;/*亮桔黄色*/
}
/*能带滚动条的表身*/
/*div的第二个子元素*/
#scrollTable div{
  width:100%;
  height:200px;
  overflow:auto;/*必需*/
  scrollbar-face-color:#EB8;/*那三个小矩形的背景色*/
  scrollbar-base-color:#ece9d8;/*那三个小矩形的前景色*/
  scrollbar-arrow-color:#FF8C00;/*上下按钮里三角箭头的颜色*/
  scrollbar-track-color:#ece9d8;/*滚动条的那个活动块所在的矩形的背景色*/
  scrollbar-highlight-color:#800040;/*那三个小矩形左上padding的颜色*/
  scrollbar-shadow-color:#800040;/*那三个小矩形右下padding的颜色*/
  scrollbar-3dlight-color: #EB8;/*那三个小矩形左上border的颜色*/
  scrollbar-darkshadow-Color:#EB8;/*那三个小矩形右下border的颜色*/
}
/*能带滚动条的表身的正体*/
#scrollTable table.tbody{
  width:100%;
  border: 1px solid #C96;
  border-right:#B74;
  color:#666666;
  background: #ECE9D8;
}
/*能带滚动条的表身的格子*/
#scrollTable table.tbody td{
  border:1px solid #C96;
}
<!doctype html> <html dir="ltr" lang="zh-CN"> <head> <meta charset="utf-8"/> <title> 纯 CSS 实现表头固定 by 司徒正美 </title> <style type="text/css"> <pre><code> #scrollTable { width:701px; border: 1px solid #EB8;/*table 没有外围的 border,只有内部的 td 或 th 有 border*/ background: #FF8C00; } #scrollTable table {border-collapse:collapse; /* 统一设置两个 table 为细线表格 */} /* 表头 div 的第一个子元素 **/ #scrollTable table.thead{width:100%;} /* 表头 */ #scrollTable table.thead th{ border: 1px solid #EB8; border-right:#C96; color:#fff; background: #FF8C00;/* 亮桔黄色 */ } /* 能带滚动条的表身 */ /*div 的第二个子元素 */ #scrollTable div{ width:100%; height:200px; overflow:auto;/* 必需 */ scrollbar-face-color:#EB8;/* 那三个小矩形的背景色 */ scrollbar-base-color:#ece9d8;/* 那三个小矩形的前景色 */ scrollbar-arrow-color:#FF8C00;/* 上下按钮里三角箭头的颜色 */ scrollbar-track-color:#ece9d8;/* 滚动条的那个活动块所在的矩形的背景色 */ scrollbar-highlight-color:#800040;/* 那三个小矩形左上 padding 的颜色 */ scrollbar-shadow-color:#800040;/* 那三个小矩形右下 padding 的颜色 */ scrollbar-3dlight-color: #EB8;/* 那三个小矩形左上 border 的颜色 */ scrollbar-darkshadow-Color:#EB8;/* 那三个小矩形右下 border 的颜色 */ } /* 能带滚动条的表身的正体 */ #scrollTable table.tbody{ width:100%; border: 1px solid #C96; border-right:#B74; color:#666666; background: #ECE9D8; } /* 能带滚动条的表身的格子 */ #scrollTable table.tbody td{border:1px solid #C96;} &lt;/style&gt; </code></pre> <p></head><br /> <body><br /> <div id="scrollTable"><br /> <table class="thead"><br /> <col width="170px"></col> <col width="170px"></col> <col width="170px"></col><col></col><br /> <tbody><br /> <tr><br /> <th> 名称 </th><br /> <th> 语法 </th><br /> <th> 说明 </th><br /> <th> 例子 </th><br /> </tr><br /> </tbody><br /> </table><br /> <div><br /> <table class="tbody"><br /> <col width="170px"></col> <col width="170px"></col> <col width="170px"></col><col></col><br /> <tbody><br /> <tr><br /> <td>Simple attribute Selector</td><br /> <td>[attr] </td><br /> <td> 选择具有此属性的元素 </td><br /> <td>blockquote[title] {<br/>color: red}</td><br /> </tr><br /> <tr><br /> <td>attribute Value Selector</td><br /> <td>[attr="value"] </td><br /> <td> 选出属性值精确等于给出值的元素 </td><br /> <td>h2[align="left"] {<br/>cursor: hand} </td><br /> </tr><br /> <tr><br /> <td>"Begins-with" attribute Value Selector</td><br /> <td>[attr^="value"] </td><br /> <td> 选出属性值以给出值开头的元素 </td><br /> <td>h2[align^="right"] {<br/>cursor: hand} </td><br /> </tr><br /> <tr><br /> <td>"Ends-with" attribute Value Selector</td><br /> <td>[attr$="value"] </td><br /> <td> 选出属性值以给出值结尾的元素 </td><br /> <td>div[class$="vml"]{<br/>cursor: hand} </td><br /> </tr><br /> <tr><br /> <td>Substring-match attribute Value Selector</td><br /> <td>[attr*="value"] </td><br /> <td> 选出属性值包含给出值的元素 </td><br /> <td>div[class*="grid"]{<br/> float:left} </td><br /> </tr><br /> <tr><br /> <td>One-Of-Many Attribute Value Selector</td><br /> <td>[attr~="value"] </td><br /> <td> 原元素的属性值为多个单词,给出值为其中一个。</td><br /> <td>li[class~="last"]{<br/> padding-left:2em} </td><br /> </tr><br /> <tr><br /> <td>Hyphen Attribute Value Selector</td><br /> <td>[attr|="value"] </td><br /> <td> 原元素的属性值等于给出值,或者以给出值加“-”开头 </td><br /> <td>span[lang|="en"]{<br/>color:green}</td><br /> </tr><br /> <tr><br /> <td> 反选属性值选择器 </td><br /> <td>[attr!="value"] </td><br /> <td> 非标准,jQuery 中出现的 </td><br /> <td>span[class!="red"]{<br/>color:green}</td><br /> </tr><br /> </tbody><br /> </table><br /> </div><br /> </div></p> <p></body><br /> </html><br />

运行代码

如果您觉得此文有帮助,可以打赏点钱给我支付宝 1669866773@qq.com ,或扫描二维码