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

后端 未结 25 1546
有刺的猬
有刺的猬 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:29

    For most browsers released after 2017:

    You can use the position: sticky. See https://caniuse.com/#feat=css-sticky.

    There is no need for a fixed width column.

    Run the code snippet below to see how it works.

    .tscroll {
      width: 400px;
      overflow-x: scroll;
      margin-bottom: 10px;
      border: solid black 1px;
    }
    
    .tscroll table td:first-child {
      position: sticky;
      left: 0;
      background-color: #ffffd;
    }
    
    .tscroll td, .tscroll th {
      border-bottom: dashed #888 1px;
    }
    
    
    Heading 1 Heading 2 Heading 3
    9:00 AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL MMM NNN OOO PPP QQQ
    10:00 AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL MMM NNN OOO PPP QQQ
    11:00 AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL MMM NNN OOO PPP QQQ
    12:00 AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL MMM NNN OOO PPP QQQ
    13:00 AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL MMM NNN OOO PPP QQQ
    14:00 AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL MMM NNN OOO PPP QQQ
    15:00 AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL MMM NNN OOO PPP QQQ
    16:00 AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL MMM NNN OOO PPP QQQ
    17:00 AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL MMM NNN OOO PPP QQQ

提交回复
热议问题