Scrollable table with fixed header

前端 未结 2 433
长发绾君心
长发绾君心 2021-01-16 14:20

I searched for some solutions in PHP/HTML/CSS for this, but nothing worked so far, maybe because in most of those examples were sooo much of code so I got lost in it. Could

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-16 14:49

    Table fixed header using CSS

    The simplest would be to position: sticky; your th elements:

    .tableFix { /* Scrollable parent element */
      position: relative;
      overflow: auto;
      height: 100px;
    }
    
    .tableFix table{
      width: 100%;
      border-collapse: collapse;
    }
    
    .tableFix th,
    .tableFix td{
      padding: 8px;
      text-align: left;
    }
    
    .tableFix thead th {
      position: sticky;  /* Edge, Chrome, FF */
      top: 0px;
      background: #fff;  /* Some background is needed */
    }
    H1Header 2Header 345th Header
    R1C2R1C2R1C3R1C4R1C5
    R2C1R2C2R2C3R2C4R2C5
    R3C1R3C2R3C3R3C4R3C5
    R4C1R4C2R4C3R4C4R4C5
    R5C1R5C2R5C3R5C4R5C5
    R6C1R6C2R6C3R6C4R6C5

    Table fixed header for older browsers

    If the browsers you need to support do not encompass the position's sticky value, you can take a look at Fix table head using a bit of Javascript

提交回复
热议问题