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

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

    If you don't want to touch your current table too much you can make a fake pinned column in front of the table.

    The example shows one way of doing it without JS

    table {
      border-collapse: collapse;
      border-spacing: 0;
      border: 1px solid #ffffd;
      min-width: 600px;
    }
    
    .labels {
      display:flex;
      flex-direction: column
    }
    
    .overflow {
      overflow-x: scroll;
      min width: 400px;
      flex: 1;
    }
    
    .label {
      display: flex;
      align-items: center;
      white-space:nowrap;
      padding: 10px;
      flex: 1;
      border-bottom: 1px solid #ffffd;
      border-right: 2px solid #ffffd;
    }
    
    .label:last-of-type {
      overflow-x: scroll;
      border-bottom: 0;
    }
    
    td {
      border: 1px solid #ffffd;
      padding: 10px;
    }
    
    .flex {
      display:flex;
      max-width: 600px;
      padding: 0;
      border: 5px solid #ffffd;
    }
    Label 1 Lorem ipsum dolor sit amet. Lorem ipsum dolor.
    Lorem ipsum dolor sit amet consectetur adipisicing Lorem ipsum dolor sit amet consectetur adipisicing
    Lorem ipsum dolor sit amet consectetur adipisicing Lorem ipsum dolor sit amet consectetur adipisicing
    Lorem ipsum dolor sit amet consectetur adipisicing Lorem ipsum dolor sit amet consectetur adipisicing

提交回复
热议问题