Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<table><tbody> scrollable?

Tags:

html

css

scroll

I would like to have a table with a scrollbar to the right.
I want to accomplish this without any plugins(jQuery) just with css.
The table header is supposed to stay fixed.

What do I need to do to get this working?

like image 392
JNK Avatar asked Mar 17 '11 21:03

JNK


People also ask

How do you make a table data scrollable in HTML?

To make an HTML table vertically scrollable, we can wrap the table with a <div> . Then, we can set a fixed height for the <div> using the height property. After that, we can set the overflow-y property to scroll .

How do I make my Tbody scrollable?

If you want tbody to show a scrollbar, set its display: block; . Set display: table; for the tr so that it keeps the behavior of a table. To evenly spread the cells, use table-layout: fixed; . Anyhow, to set a scrollbar, a display reset is needed to get rid of the table-layout (which will never show scrollbar).

How do I add a scrollbar to a table?

Suppose we want to add a scroll bar option in HTML, use an “overflow” option and set it as auto-enabled for adding both horizontal and vertical scroll bars. If we want to add a vertical bar option in Html, add the line “overflow-y” in the files.

How do you make a table vertically scrollable in CSS?

use overflow-y if you only want a vertical scroll bar and overflow if you want both a vertical and horizontal. Note: setting an overflow attribute to scroll will always display the scrollbars. If you want the horizontal/vertical scrollbars to only show up when needed, use auto .


1 Answers

You have taken on a task that, if you succeed, will make you a hero. I tried this and the straightforward thing -- to position:fixed; the <thead> -- is impossible. I had to copy all of the <thead> into a new object. But when you do that, the horizontal spacing of the <th> elements all goes away so the headings don't line up with the <td>s anymore. I ended up doing something like this:

First of all, abandon ie6 and ie7. There's no hope for those guys.

  1. Make two copies of the table, one where the body is invisible and the <thead> is visible, and the other where it's vice-versa.

  2. Give z-index:1; to the table with the visible <thead>.

  3. Give z-index:0; to the table with the visible <tbody>.

  4. Deal with horizontal scrolling, but not until after you find that onScroll isn't an ie8 event (not to mention ie6), so that you have to take a setInterval break every tenth of a second or so just to handle scrolling the <thead> left and right in ie8.

This will give you a table body of infinite scroll in both axes, with a table head that scrolls in the x axis only. Pretty much works in FF, Chrome, and Safari. But is shaky in ie8. A real pita.

Good luck, and please write if you get this to work!

like image 138
Pete Wilson Avatar answered Sep 22 '22 04:09

Pete Wilson