zoukankan      html  css  js  c++  java
  • 使用css让表头固定的方法

    1、可以使用display: table; 100%; table-layout: fixed;

     table-layout: fixed;设置表格布局算法。tableLayout 属性用来显示表格单元格、行、列的算法规则。

    详细可见:https://www.w3school.com.cn/cssref/pr_tab_table-layout.asp

    https://www.w3school.com.cn/tiy/t.asp?f=csse_table_table-layout

    <div>
    	<div class="table-head">
    		<table id="contentTable">
    			<thead>
    				<tr>
    					<th><input type="checkbox"/></th>
    					<th>xxxxx</th>
    					<th>xxxxx</th>
    					<th>证件号码</th>
    					<th>姓名</th>
    					<th>性别</th>
    					<th>xxxxx</th>
    					<th>出生日期</th>
    					<th>xxx</th>
    					<th>xxx</th>
    					<th>ssss</th>
    					<th>xxxx</th>
    					<th>ccccc手机</th>
    					<th>家庭住址</th>
    					<th>xxxxx</th>
    					<th>xxxxx</th>
    					<th>xxxxx</th>
    					<th>修改日期</th>
    				</tr>
    			</thead>
    			<tbody>
    				<tr>
                            //21个td  省略
                        </tr>				
    			</tbody>
    		</table>
    	</div>
    </div>
    

    这里是样式

    /** 这是css */
    body{overflow-y: hidden}
    #contentTable tbody { display: block; height: 520px; overflow-y: scroll; }
    #contentTable thead,
    #contentTable tbody tr { display: table;  100%; table-layout: fixed; /**表格列的宽度由表格宽度决定,不由内容决定*/ text-align: center; }
     
    /**每一个th都要和对应的td宽度相同,如果不设置,则默认均分宽度,我设置的都是一些内容较多的,比如说身份证号码,电话,住址等*/
    #contentTable tr th:nth-of-type(1),#contentTable tr td:nth-of-type(1){ 1.5%;}
    #contentTable tr th:nth-of-type(2),#contentTable tr td:nth-of-type(2){ 8.5%;}
    #contentTable tr th:nth-of-type(3),#contentTable tr td:nth-of-type(3){ 7%;}
    #contentTable tr th:nth-of-type(4),#contentTable tr td:nth-of-type(4){ 8.5%;}
    #contentTable tr th:nth-of-type(8),#contentTable tr td:nth-of-type(8){ 5%;}
    #contentTable tr th:nth-of-type(9),#contentTable tr td:nth-of-type(9){ 5%;}
    #contentTable tr th:nth-of-type(12),#contentTable tr td:nth-of-type(12){ 8.5%;}
    #contentTable tr th:nth-of-type(13),#contentTable tr td:nth-of-type(13){ 6%;}
    #contentTable tr th:nth-of-type(14),#contentTable tr td:nth-of-type(14){ 15%;}
    #contentTable tr th:nth-of-type(16),#contentTable tr td:nth-of-type(16){ 5%;}
    #contentTable tr th:nth-of-type(18),#contentTable tr td:nth-of-type(18){ 5%;}
     
    /**这一个重要:写完上面的看效果应该是表头和内容对不齐。我们需要的是调节0.4em中的0.4,试着变大变小就能看到区别,我试了,刚好跳到0.4,所有的列都能垂直对齐*/
    #contentTable thead {  calc( 100% - 0.4em);/*表头与表格垂直对齐*/ }
    

    这里如果不需要右侧滚动条的时候,

    #sTable::-webkit-scrollbar{
        0px;
        opacity:0;
        -webkit-overflow-scrolling: touch;
    }
    

      

  • 相关阅读:
    文件夹无法删除解决方案
    常用Web Service汇总(天气预报、时刻表等)
    浏览器兼容手册
    如何在word2007下右键添加“新建Word 2003 文档”
    Centos7上实现不同网段的服务器文件共享
    ubuntu安装界面 会出现不完整情况
    Centos7搭建dhcp服务器
    Centos7上搭建ftp服务器
    Centos7上配置网络和本地yum方法
    浅谈网络流
  • 原文地址:https://www.cnblogs.com/shj-com/p/11290241.html
Copyright © 2011-2022 走看看