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; }
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.
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.
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'.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With