纯CSS表头固定的实现代码

  发布时间:2018-11-09 16:04:14   作者:程序员阿宇   我要评论
这篇文章主要介绍了纯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实现表头固定</title>
    <style type="text/css">

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

    </style>
  </head>
  <body>
    <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>


  </body>
</html>

发现表头的格子与表身的格子不对齐。这时我们需要动用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实现表头固定 </title>
    <style type="text/css">

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

    </style>
  </head>
  <body>
    <div id="scrollTable">
      <table class="thead">
        <col width="170px"></col> <col width="170px"></col> <col width="170px"></col><col></col>
        <tbody>
          <tr>
            <th>名称</th>
            <th>语法</th>
            <th>说明</th>
            <th>例子</th>
          </tr>
        </tbody>
      </table>
      <div>
        <table class="tbody">
          <col width="170px"></col> <col width="170px"></col> <col width="170px"></col><col></col>
          <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>

  </body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 前端CSS Grid 布局示例详解

    CSS Grid 是一种二维布局系统,可以同时控制行和列,相比 Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,这篇文章主要介绍了前端CSS Grid 布局详解,需要的朋
    2025-04-16
  • CSS Padding 和 Margin 区别全解析

    CSS 中的 padding 和 margin 是两个非常基础且重要的属性,它们用于控制元素周围的空白区域,本文将详细介绍 padding 和 margin 的概念、区别以及如何在实际项目中使用它们
    2025-04-07
  • CSS will-change 属性示例详解

    will-change 是一个 CSS 属性,用于告诉浏览器某个元素在未来可能会发生哪些变化,本文给大家介绍CSS will-change 属性详解,感兴趣的朋友一起看看吧
    2025-04-07
  • CSS去除a标签的下划线的几种方法

    本文给大家分享在 CSS 中,去除a标签(超链接)的下划线的几种方法,本文给大家介绍的非常详细,感兴趣的朋友一起看看吧
    2025-04-07
  • 前端高级CSS用法示例详解

    在前端开发中,CSS(层叠样式表)不仅是用来控制网页的外观和布局,更是实现复杂交互和动态效果的关键技术之一,随着前端技术的不断发展,CSS的用法也日益丰富和高级,本文将
    2025-04-07
  • css中的 vertical-align与line-height作用详解

    文章详细介绍了CSS中的`vertical-align`和`line-height`属性,包括它们的作用、适用元素、属性值、常见使用场景、常见问题及解决方案,感兴趣的朋友跟随小编一起看看吧
    2025-03-26
  • 浅析CSS 中z - index属性的作用及在什么情况下会失效

    z-index属性用于控制元素的堆叠顺序,值越大,元素越显示在上层,它需要元素具有定位属性(如relative、absolute、fixed或sticky),本文给大家介绍CSS 中z - index属性的作用
    2025-03-21
  • CSS @media print 使用详解

    文章详细介绍了CSS中的打印媒体查询@mediaprint包括基本语法、常见使用场景和代码示例,如隐藏非必要元素、调整字体和颜色、处理链接的URL显示、分页控制、调整边距和背景等
    2025-03-18
  • CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

    本文介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,感兴趣的朋友一起
    2025-03-10
  • 前端 CSS 动态设置样式::class、:style 等技巧(推荐)

    本文介绍了Vue.js中动态绑定类名和内联样式的两种方法:对象语法和数组语法,通过对象语法,可以根据条件动态切换类名或样式;通过数组语法,可以同时绑定多个类名或样式,此外
    2025-02-26

最新评论