Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix and Scrollable table structure using html div

Tags:

html

css

tabular

I want to create a table grid where first few columns are fixed and rest of them are scrollable as seen in this image.

enter image description here

Rest of the columns are dynamic, user can select and deselect columns. I am struggling to make that html using div or tables. Need guidance or sample structure to move on.

like image 817
Ghazanfar Khan Avatar asked Mar 24 '17 10:03

Ghazanfar Khan


People also ask

How do I make my table header fixed while scrolling in HTML?

By setting postion: sticky and top: 0, we can create a fixed header on a scroll in HTML tables.

How do I make a table row scrollable in HTML?

you don't need it to be overflow-x just make it overflow: scroll; . Just do this in the td element. Also, the content has to be long enough to scroll the element.

How do I make my table body scrollable?

We set the height of the table element to 120px to make restrict the height of it so we can make it scrollable. To make it scrollable, we set the overflow CSS property to scroll . Then we set the tr elements in the thead to absolute position so they stay in place.

How do you freeze a table pane in HTML?

To freeze the row/column we can use a simple HTML table and CSS. HTML: In HTML we can define the header row by <th> tag or we can use <td> tag also. Below example is using the <th> tag. We also put the table in DIV element to see the horizontal and vertical scrollbar by setting the overflow property of the DIV element.


1 Answers

With custom implementation. Just simple like this:

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

Or jsfiddle:

https://jsfiddle.net/h75zn59o/21/

Note:

position:absolute; is what causes that first header column to be fixed.

With the original CSS, it's just applied to "th", but using classes (in this example, col1, col2, etc.)

We can assign different fixed positions to different columns.

Because the columns are 100px wide, each following column is positioned another 100px left So, the first one is 0px, then 100px for col2, etc) to avoid overlap with the previous column.

like image 198
Ave Avatar answered Sep 28 '22 00:09

Ave