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

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

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 first row and first column of a table when scrolling, possibly using JavaScript and CSS?
  • But I need just a single left column to be frozen and I would prefer a simple and script-less solution.


    If you want a table where only the columns scroll horizontally, you can position: absolute the first column (and specify its width explicitly), and then wrap the entire table in an overflow-x: scroll block. Don't bother trying this in IE7, however...

    Relevant HTML & CSS:

    table {
      border-collapse: separate;
      border-spacing: 0;
      border-top: 1px solid grey;
    }
    
    td, th {
      margin: 0;
      border: 1px solid grey;
      white-space: nowrap;
      border-top-width: 0px;
    }
    
    div {
      width: 500px;
      overflow-x: scroll;
      margin-left: 5em;
      overflow-y: visible;
      padding: 0;
    }
    
    .headcol {
      position: absolute;
      width: 5em;
      left: 0;
      top: auto;
      border-top-width: 1px;
      /*only relevant for first row*/
      margin-top: -1px;
      /*compensate for top border*/
    }
    
    .headcol:before {
      content: 'Row ';
    }
    
    .long {
      background: yellow;
      letter-spacing: 1em;
    }
    <div>
    <table>
            <tr><th class="headcol">1</th><td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td><td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td></tr>
            <tr><th class="headcol">2</th><td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td><td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td></tr>
            <tr><th class="headcol">3</th><td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td><td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td></tr>
            <tr><th class="headcol">4</th><td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td><td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td></tr>
            <tr><th class="headcol">5</th><td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td><td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td></tr>
            <tr><th class="headcol">6</th><td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td><td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td></tr>
    </table>
    </div>

    This is an interesting jQuery plugin that creates fixed headers and/or columns . Toggle fixed column to be true on the demo page to see it in action.


    In case of fixed width left column the best solution is provided by Eamon Nerbonne.

    In case of variable width left column the best solution I found is to make two identical tables and push one above another. Demo: http://jsfiddle.net/xG5QH/6/.

    <!DOCTYPE html>
    <html>
    <head>
    <style type="text/css">
    /* important styles */
    
    .container {
       /* Attach fixed-th-table to this container,
          in order to layout fixed-th-table
          in the same way as scolled-td-table" */
       position: relative;
    
       /* Truncate fixed-th-table */
       overflow: hidden;
    }
    
    .fixed-th-table-wrapper td,
    .fixed-th-table-wrapper th,
    .scrolled-td-table-wrapper td,
    .scrolled-td-table-wrapper th {
       /* Set background to non-transparent color
          because two tables are one above another.
        */
       background: white;
    }
    .fixed-th-table-wrapper {
       /* Make table out of flow */
       position: absolute;
    }
    .fixed-th-table-wrapper th {
        /* Place fixed-th-table th-cells above 
           scrolled-td-table td-cells.
         */
        position: relative;
        z-index: 1;
    }
    .scrolled-td-table-wrapper td {
        /* Place scrolled-td-table td-cells
           above fixed-th-table.
         */
        position: relative;
    }
    .scrolled-td-table-wrapper {
       /* Make horizonal scrollbar if needed */
       overflow-x: auto;
    }
    
    
    /* Simulating border-collapse: collapse,
       because fixed-th-table borders
       are below ".scrolling-td-wrapper table" borders
    */
    
    table {
        border-spacing: 0;
    }
    td, th {
       border-style: solid;
       border-color: black;
       border-width: 1px 1px 0 0;
    }
    th:first-child {
       border-left-width: 1px;
    }
    tr:last-child td,
    tr:last-child th {
       border-bottom-width: 1px;
    }
    
    /* Unimportant styles */
    
    .container {
        width: 250px;
    }
    td, th {
       padding: 5px;
    }
    </style>
    </head>
    
    <body>
    <div class="container">
    
    <div class="fixed-th-table-wrapper">
    <!-- fixed-th-table -->
    <table>
        <tr>
             <th>aaaaaaa</th>
             <td>ccccccccccc asdsad asd as</td>
             <td>ccccccccccc asdsad asd as</td>
        </tr>
        <tr>
             <th>cccccccc</th>
             <td>xxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyy zzzzzzzzzzzzz</td>
             <td>xxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyy zzzzzzzzzzzzz</td>
        </tr>
    </table>
    </div>
    
    <div class="scrolled-td-table-wrapper">
    <!-- scrolled-td-table
         - same as fixed-th-table -->
    <table>
        <tr>
             <th>aaaaaaa</th>
             <td>ccccccccccc asdsad asd as</td>
             <td>ccccccccccc asdsad asd as</td>
        </tr>
        <tr>
             <th>cccccccc</th>
             <td>xxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyy zzzzzzzzzzzzz</td>
             <td>xxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyy zzzzzzzzzzzzz</td>
        </tr>
    </table>
    </div>
    
    </div>
    </body>
    </html>
    
    链接地址: http://www.djcxy.com/p/88190.html

    上一篇: 如何在打印大型HTML表格时处理分页符

    下一篇: 我如何创建一个固定/冻结左列和可滚动主体的HTML表格?