带水平滚动的HTML表格(第一列固定)

时间:2010-08-04 03:06:02

标签: html overflow html-table fixed

我一直试图想办法用一个固定的第一列制作一个表(并且表的其余部分有一个水平溢出)我看到一个帖子有一个类似的问题。但固定列位似乎没有得到解决。帮助

6 个答案:

答案 0 :(得分:96)

怎么样:

table {
  table-layout: fixed; 
  width: 100%;
  *margin-left: -100px; /*ie7*/
}
td, th {
  vertical-align: top;
  border-top: 1px solid #ccc;
  padding: 10px;
  width: 100px;
}
.fix {
  position: absolute;
  *position: relative; /*ie7*/
  margin-left: -100px;
  width: 100px;
}
.outer {
  position: relative;
}
.inner {
  overflow-x: scroll;
  overflow-y: visible;
  width: 400px; 
  margin-left: 100px;
}
<div class="outer">
  <div class="inner">
    <table>
      <tr>
        <th class=fix></th>
        <th>Col 1</th>
        <th>Col 2</th>
        <th>Col 3</th>
        <th>Col 4</th>
        <th class="fix">Col 5</th>
      </tr>
      <tr>
        <th class=fix>Header A</th>
        <td>col 1 - A</td>
        <td>col 2 - A (WITH LONGER CONTENT)</td>
        <td>col 3 - A</td>
        <td>col 4 - A</td>
        <td class=fix>col 5 - A</td>
      </tr>
      <tr>
        <th class=fix>Header B</th>
        <td>col 1 - B</td>
        <td>col 2 - B</td>
        <td>col 3 - B</td>
        <td>col 4 - B</td>
        <td class=fix>col 5 - B</td>
      </tr>
      <tr>
        <th class=fix>Header C</th>
        <td>col 1 - C</td>
        <td>col 2 - C</td>
        <td>col 3 - C</td>
        <td>col 4 - C</td>
        <td class=fix>col 5 - C</td>
      </tr>
    </table>
  </div>
</div>

你可以在这个jsbin中测试它:http://jsbin.com/uxecel/4/edit

答案 1 :(得分:23)

我有一个类似的表格样式:

<table style="width:100%; table-layout:fixed">
    <tr>
        <td style="width: 150px">Hello, World!</td>
        <td>
            <div>
                <pre style="margin:0; overflow:scroll">My preformatted content</pre>
            </div>
        </td>
    </tr>
</table>

答案 2 :(得分:9)

使用jQuery DataTables插件,它支持固定的标题和列。 此示例将固定列支持添加到html表“example”:

http://datatables.net/extensions/fixedcolumns/

对于两个固定列:

http://www.datatables.net/release-datatables/extensions/FixedColumns/examples/two_columns.html

答案 3 :(得分:4)

基于skube的方法,我发现我需要的最小CSS集是:

.horizontal-scroll-except-first-column {
    width: 100%;
    overflow: auto;
    margin-left: 20em;
}

.horizontal-scroll-except-first-column > table > * > tr > th:first-child,
.horizontal-scroll-except-first-column > table > * > tr > td:first-child {
    position: absolute;
    width: 20em;
    margin-left: -20em;
}

.horizontal-scroll-except-first-column > table > * > tr > th,
.horizontal-scroll-except-first-column > table > * > tr > td {
    /* Without this, if a cell wraps onto two lines, the first column
     * will look bad, and may need padding. */
    white-space: nowrap;
}

使用HTML:

<div class="horizontal-scroll-except-first-column">
    <table>
        ...
    </table>
</div>

答案 4 :(得分:3)

看看这个JQuery插件:

http://fixedheadertable.com

它将垂直(固定标题行)或水平(固定的第一列)滚动到现有HTML表格。有一个演示,您可以检查两种滚动情况。

答案 5 :(得分:0)

您可以使用下面的表格样式来拥有固定第一列的水平滚动表格。

<style type="text/css">
    table, th, td {
        border: 1px solid black;
    }    
    .table-style {
        overflow-x: auto;
    }
    .table-style tr th:first-child {
        position: sticky;
        left: 0;
        z-index: 2;
        background-color: white;
    }
</style>

<div class="table-style">
<table>
    <thead>
        <tr>
            <th>_col1_row1_</th>
            <th>_col2_row1_</th>
            <th>_col3_row1_</th>
            <th>_col4_row1_</th>
            <th>_col5_row1_</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th>_col1_row2_</th>
            <td>_col2_row2_</td>
            <td>_col3_row2_</td>
            <td>_col4_row2_</td>
            <td>_col5_row2_</td>
        </tr>
        <tr>
            <th>_col1_row3_</th>
            <td>_col2_row3_</td>
            <td>_col3_row3_</td>
            <td>_col4_row3_</td>
            <td>_col5_row3_</td>
        </tr>
        <tr>
            <th>_col1_row4_</th>
            <td>_col2_row4_</td>
            <td>_col3_row4_</td>
            <td>_col4_row4_</td>
            <td>_col5_row4_</td>
        </tr>
        <tr>
            <th>_col1_row5_</th>
            <td>_col2_row5_</td>
            <td>_col3_row5_</td>
            <td>_col4_row5_</td>
            <td>_col5_row5_</td>
        </tr>
    </tbody>
</table>