HTML table with horizontal scrolling (first column fixed)

HtmlOverflowHtml TableFixed

Html Problem Overview


I have been trying to think of a way to make a table with a fixed first column (and the rest of the table with a horizontal overflow) I saw a post which had a similar question. but the fixed column bit did not seem to be resolved. Help?

Html Solutions


Solution 1 - Html

How about:

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>

You can test it out in this jsbin: http://jsbin.com/uxecel/4/edit

Solution 2 - Html

I have a similar table styled like so:

<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>

Solution 3 - Html

Based on skube's approach, I found the minimal set of CSS I needed was:

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

.horizontal-scroll-except-first-column > table {
  margin-left: 8em;
}

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

.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;
}

<div class="horizontal-scroll-except-first-column">
  <table>
    <tbody>
      <tr>
        <td>FIXED</td> <td>22222</td> <td>33333</td> <td>44444</td> <td>55555</td> <td>66666</td> <td>77777</td> <td>88888</td> <td>99999</td> <td>AAAAA</td> <td>BBBBB</td> <td>CCCCC</td> <td>DDDDD</td> <td>EEEEE</td> <td>FFFFF</td>
      </tr>
    </tbody>
  </table>
</div>

Solution 4 - Html

Use jQuery DataTables plug-in, it supports fixed header and columns. This example adds fixed column support to the html table "example":

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

For two fixed columns:

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

Solution 5 - Html

You can use below table style to have horizontal scrolling table with fixed first column.

<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>

Solution 6 - Html

Take a look at this JQuery plugin:

http://fixedheadertable.com

It adds vertical (fixed header row) or horizontal (fixed first column) scrolling to an existing HTML table. There is a demo you can check for both cases of scrolling.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlanView Question on Stackoverflow
Solution 1 - HtmlskubeView Answer on Stackoverflow
Solution 2 - HtmlkbrimingtonView Answer on Stackoverflow
Solution 3 - HtmljoeytwiddleView Answer on Stackoverflow
Solution 4 - Htmlmas_oz2k1View Answer on Stackoverflow
Solution 5 - HtmlPrashantView Answer on Stackoverflow
Solution 6 - HtmlMarkos FragkakisView Answer on Stackoverflow