Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add scroll bar to table body

Tags:

html

css

I want to do this as easily as possible with out any additional libraries.

In my very long table I want to add a scrollbar to the <tbody> tag so that the head is always visible but it wont work. can you please help.

fiddle : http://jsfiddle.net/Hpx4L/

<table border="1">   <thead>     <tr>       <th>Month</th>       <th>Savings</th>     </tr>   </thead>   <tfoot>     <tr>       <td>Sum</td>       <td>$180</td>     </tr>   </tfoot>   <tbody style="overflow-y:scroll; height:100px;"> <!-- wont work -->     <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>           <tr>       <td>January</td>       <td>$100</td>     </tr>     <tr>       <td>February</td>       <td>$80</td>     </tr>   </tbody> </table> 
like image 327
Hello-World Avatar asked Jul 03 '13 14:07

Hello-World


People also ask

How do I add a scrollbar to my 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 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 I make a vertical table scrollable in HTML?

The approach of this article is to create table with 100% width using width property and create vertical scroll inside table body using overflow-y property. Overflow property is used to create scrollbar in table. Use display: block; property to display the block level element.

How do I add a vertical scroll bar in HTML?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.


1 Answers

you can wrap the content of the <tbody> in a scrollable <div> :

html

.... <tbody>   <tr>     <td colspan="2">       <div class="scrollit">         <table>           <tr>             <td>January</td>             <td>$100</td>           </tr>           <tr>             <td>February</td>             <td>$80</td>           </tr>           <tr>             <td>January</td>             <td>$100</td>           </tr>           <tr>             <td>February</td>             <td>$80</td>           </tr>           ... 

css

.scrollit {     overflow:scroll;     height:100px; } 

see my jsfiddle, forked from yours: http://jsfiddle.net/VTNax/2/

like image 93
braican Avatar answered Sep 21 '22 05:09

braican