44

Does anyone know a vanilla way of making the body of a table scrollable using only html and css?

The obvious solution

tbody { height: 200px; overflow-y: scroll; } 

does not work.

Wouldnt this be the obvious use of tables?

am I doing something wrong?

4 Answers 4

70

You need to declare a height first, else your table will expand to the according width of its content.

table{ overflow-y:scroll; height:100px; display:block; } 

EDIT: after clarifying your problem I edited the fiddle: check out this Example or that way. It's rather hacky and not guaranteed to work crossbrowser but might work for your case.

Sign up to request clarification or add additional context in comments.

8 Comments

what happens to the thead part of the table?
Using display:block would work, but generally wouldn't be recommended as the browser would no longer consider it to be a table.
The examble should solve this link --and I want the header to stay in place
@MartinKristiansen you mean, you want to scroll the body while the head stays fixed? You can't achieve with pure css.
@Christoph: Thats what I wanna do -- and it would seem like that was exactly what tables were made for -- but from the looks of it, I think your right.
|
21

You can't do that with a table. Wrap the table with a div a give it something like:

div.wrapper { overflow:hidden; overflow-y: scroll; height: 100px; // change this to desired height } 

1 Comment

This should work. Since increasing the height of the table stretches the row alignment.
19

You can wrap the table with a parent div and make him scrollable:

.div_before_table { overflow:hidden; overflow-y: scroll; height: 500px; } 

And to keep the table header sticky you can add a fixed class:

.th.fixed { top: 0; z-index: 2; position: sticky; background-color: white; } 

You can see the example here (click on Show Code Snippet and then Run Code Snippet) :

table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #ddd; } /* The scrollable part */ .scrollable { height: 150px; overflow-y: scroll; border-bottom: 1px solid #ddd; } th { position: sticky; background-color: white; z-index: 2; top: 0; }
<div class="scrollable"> <table> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Helen Bennett</td> <td>UK</td> </tr> <tr> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> </table> </div>

3 Comments

This is not accurate enough. I am using a gradient for the background-color of the table head. Using th makes it to repeat the color from left to right for each column of the table head.
This code works for me. Thanks.
To prevent the header from scrolling, you can directly set <thead style={{ position: "sticky", top: 0 }}> on the <thead>.
-1

Do you want somthing like this ?

Pure CSS Scrollable Table with Fixed Header (1)

http://anaturb.net/csstips/sheader.htm

http://www.scientificpsychic.com/blogentries/html-and-css-scrolling-table-with-fixed-heading.html

4 Comments

Yep except that I still have to make the width of the cols in the table static. I really really dont wanna have to decide the width before runtime.
and the second solution is a hack if I ever saw one. without the width tag set, there is absolutely no garantie that the coloms will align with the headers
A similar question has been asked here: stackoverflow.com/questions/10838700/…
And this code guy suggests to set the width at server side in case you need to set it dynamically. cssbakery.com/2010/12/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.