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;
}
My simplest solution is based on fixed columns layout. You'll have to set the width of each column, for example: 4 columns 100px each is equal to 400px total width.
The fixed table layout algorithm has many advantages over the automatic table layout algorithm (for example: the horizontal layout only depends on the table's width and the width of the columns, not the contents of the cells; allows a browser to lay out the table faster than the automatic table layout; the browser can begin to display the table once the first row has been received; etc.)
Then, you'll have to separate the thead from the tbody by forcing their display style to
block
rather than to the defaulttable-*
That what will make the
tbody
scrollable as a separate box and thethead
unrelated from it. And that's the main reason why you had to fix the column widths as done above.Working JsFiddle: https://jsfiddle.net/angiolep/65q1gdcy/1/
This is technique I have used on a number of occasions. It is originally based on this fiddle with a number of modifications. It is also fluid and column widths can be fixed by adding a width style to the
<th>
.Also as a JSFiddle
This was a challenging question. I think I finally have a solution that satisfies complete requirements: a vertical and horizontal scrollable dynamic table (dynamic because you can change the amount of rows and columns, and no cells have fixed width or height).
The HTML and CSS layout is quite simple as other people have mentioned. The key issue is recalculating (JavaScript) cell widths. And to make sure horizontal scrolling works, I also recalculate theader and tbody width.
Here's a fiddle https://jsfiddle.net/jmarcos00/6hv0dsj8/1/
HTML code:
CSS code:
JavaScript code:
you can try this
CSS:
HTML:
Check this fiddle : http://jsfiddle.net/Kritika/GLKxB/1/
this will keep the
table
headfixed
,andscroll
only thetable content
.Using flexboxes, no javascript, and it is responsive.