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

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

    Here is another modification of the most popular answer, but with handling of variable length of text in the first column labels: http://jsfiddle.net/ozx56n41/

    Basically, I'm using the second column for creating row height, like was mentioned. But my fiddle actually works unlike most mentioned above.

    HTML:

    This is a long label
    This is a long label
    QWERTYUIOPASDFGHJKLZXCVBNM QWERTYUIOPASDFGHJKLZXCVBNM
    Short label
    Short label
    QWERTYUIOPASDFGHJKLZXCVBNM QWERTYUIOPASDFGHJKLZXCVBNM

    CSS:

    body {
        font: 16px Calibri;
    }
    #outerdiv {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        width: 100%;
        border-top: 1px solid grey;
    }
    #innerdiv {
        overflow-x: scroll;
        margin-left: 100px;
        overflow-y: visible;
        padding-bottom: 1px;
    }
    table {
        border-collapse:separate;
    }
    td {
        margin: 0;
        border: 1px solid grey;
        border-top-width: 0;
        border-left-width: 0px;
        padding: 10px;
    }
    td.headcol {
        /* Frozen 1st column */
        position: absolute;
        left: 0;
        top: auto;
        border-bottom-width: 1px;
        padding: 0;
        border-left-width: 1px;
    }
    td.hiddenheadcol {
        /* Hidden 2nd column to create height */
        max-width: 0;
        visibility: hidden;
        padding: 0;
    }
    td.headcol div {
        /* Text container in the 1st column */
        width: 100px;
        max-width: 100px;
        background: lightblue;
        padding: 10px;
        box-sizing: border-box;
    }
    td.hiddenheadcol div {
        /* Text container in the 2nd column */
        width: 100px;
        max-width: 100px;
        background: red;
        padding: 10px;
    }
    td.long {
        background:yellow;
        letter-spacing:1em;
    }
    

提交回复
热议问题