How do I create an HTML table with a fixed/frozen left column and a scrollable body?

后端 未结 25 2050
有刺的猬
有刺的猬 2020-11-21 22:27

I need a simple solution. I know it\'s similar to some other questions, like:

  • HTML table with fixed headers and a fixed column?
  • How can I lock the fir
25条回答
  •  孤城傲影
    2020-11-21 23:25

    //If the table has tbody and thead, make them the relative container in which we can fix td and th as absolute
    
    table tbody {
        position: relative;
    }
    
    table thead {
        position: relative;
    }
    
    //Make both the first header and first data cells (First column) absolute so that it sticks to the left
    
    table td:first-of-type {
        position: absolute;
    }
    
    table th:first-of-type {
        position: absolute;
    }
    
    //Move Second column according to the width of column 1 
    
    table td:nth-of-type(2) {
        padding-left: ;
    }
    
    table th:nth-of-type(2) {
        padding-left: ;
    }
    

提交回复
热议问题