Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a scrollbar to an HTML5 table?

Tags:

html

css

I have an table in HTML5 that I would like to add a scrollbar to. I want the table to show ten rows and then the user can scroll down to see other songs. How can I add the scrollbar?

Here is my code for the table in HTML5:

<table id="my_table" table border="5">   <tr>     <th>URL</th>   </tr>   <tr>     <td>http://www.youtube.com/embed/evuSpI2Genw</td>   </tr>   <tr>     <td>http://www.youtube.com/embed/evuSpI2Genw</td>   </tr> </table> 

Here is my CSS code:

#my_table {     border-radius: 20px;     background-color: transparent;     color: black;     width: 500px;     text-align: center; } 
like image 487
Aaron Avatar asked Jul 11 '13 03:07

Aaron


People also ask

How do I add a scrollbar to a table in HTML?

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 add a scrollbar to a table cell?

The easiest way is to put inside your cell a div filling it and set its overflow style property. If you want the scrollbar to be always visible, even when the content isn't cropped, replace auto with scroll in the CSS.

How do I add a scrollbar to my HTML body?

The overflow style property of the html element affects the state of the document's scrollbars in all browser. If you want to hide the scrollbars, set it to 'hidden', if you want to force scrollbars to be always visible, set it to 'scroll'.

How do I add a vertical scrollbar to a table?

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

If you have heading to your table columns and you don't want to scroll those headings then this solution could help you:

This solution needs thead and tbody tags inside table element.

table.tableSection {     display: table;     width: 100%; } table.tableSection thead, table.tableSection tbody {     float: left;     width: 100%; } table.tableSection tbody {     overflow: auto;     height: 150px; } table.tableSection tr {     width: 100%;     display: table;     text-align: left; } table.tableSection th, table.tableSection td {     width: 33%; } 

Working fiddle

With comments

Note: If you are sure that the vertical scrollbar is always present, then you can use css3 calc property to make the thead cells align with the tbody cells.

table.tableSection thead {     padding-right:18px;   /* 18px is approx. value of width of scroll bar */     width: calc(100% - 18px); } 

You can do the same by detecting presence of scrollbar using javascript and applying the above styles.

like image 190
Mr_Green Avatar answered Oct 06 '22 01:10

Mr_Green