HTML table with fixed header and footer and scrollable body without fixed widths

后端 未结 4 2188
南旧
南旧 2020-12-15 19:05

I want to to create a table with fixed thead and tfoot and a scrollable tbody!

I\'ve tried several approaches, both CSS only a

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-15 19:33

    I finally implemented a working solution!

    The relevant CSS is the following:

    .wrapper {
      width: 90%;
      position: relative;
      border: 1px solid #000;
      background: #efefef;
      overflow: hidden;
      border-radius: 7px;
    }
    
    .container {
      overflow-y: auto;
      height: 200px;
      border-top: 41px solid transparent;
      border-bottom: 41px solid transparent;
    }
    
    table {
      border-spacing: 0;
      border-collapse: collapse;
      width: 100%;
    }
    
    td + td {
      border-left: 1px solid #fff;
    }
    
    td, th {
      border-bottom: 1px solid #fff;
      background: #efefef;
      padding: 10px;
    }
    
    thead tr th,
    tfoot tr td {
      height: 0;
      line-height: 0;
      margin: 0;
      padding-top: 0;
      padding-bottom: 0;
      color: transparent;
      border: none;
      white-space: nowrap;
    }
    
    thead tr th div,
    tfoot tr td div {
      position: absolute;
      color: #fff;
      height: 20px;
      padding: 10px;
      margin-left: -10px;
      line-height: normal;
      width: 100%;
      z-index: 2;
      text-align: left;
      font-weight: bold;
    }
    
    thead tr th div {
      border-left: 1px solid #000;
      border-bottom: 1px solid #000;
    }
    
    tfoot tr td div {
      border-top: 1px solid #000;
    }
    
    tfoot tr td div.c1,
    thead tr th div.c1 {
      background: violet;
    }
    
    tfoot tr td div.c2,
    thead tr th div.c2 {
      background: green;
    }
    
    tfoot tr td div.c3,
    thead tr th div.c3 {
      background: yellow;
    }
    
    thead tr th div {
      top: 0;
    }
    
    tfoot tr td div {
      bottom: 0;
    }
    
    thead tr th:first-child div,
    tfoot tr td:first-child div {
      border-left: none;
    }
    

    And this is the markup:

    Header one *leads the width* (case 1)
    Header one *leads the width* (case 1)
    Header two
    Header two
    Header three
    Header three
    Column one Column two *leads the width* (case 2) Column three [first]
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three
    Column one Column two *leads the width* (case 2) Column three [LATEST]
    Footer one
    Footer one
    Footer two
    Footer two
    Footer three *leads the width* (case 3)
    Footer three *leads the width* (case 3)

    It works on Chrome, Firefox, Safari and IE11 (I don't know how it behaves on older browsers). See it on codepen: https://codepen.io/daveoncode/pen/LNomBE

提交回复
热议问题