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. Since changing the display property of tbody, we should change the property for thead element as well to prevent from breaking the table layout.
Example:
<!DOCTYPE html> <html> <head> <title> Display table with vertical scrollbar </title> <style> table.scrolldown { width: 100%; /* border-collapse: collapse; */ border-spacing: 0; border: 2px solid black; } /* To display the block as level element */ table.scrolldown tbody, table.scrolldown thead { display: block; } thead tr th { height: 40px; line-height: 40px; } table.scrolldown tbody { /* Set the height of table body */ height: 50px; /* Set vertical scroll */ overflow-y: auto; /* Hide the horizontal scroll */ overflow-x: hidden; } tbody { border-top: 2px solid black; } tbody td, thead th { width : 200px; border-right: 2px solid black; } td { text-align:center; } </style> </head> <body> <table class="scrolldown"> <!-- Table head content --> <thead> <tr> <th>Heading 1</th> <th>Heading 2</th> <th>Heading 3</th> <th>Heading 4</th> <th>Heading 5</th> </tr> </thead> <!-- Table body content --> <tbody> <tr> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> </tr> <tr> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> </tr> <tr> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> </tr> </tbody> </table> <body> </html> |
Output:

Recommended Posts:
- Hide scroll bar, but while still being able to scroll using CSS
- How to group the body content in a table using HTML5 ?
- How to scroll to an element inside a div using javascript?
- How to get the width of scroll bar using JavaScript ?
- HTML width/height Attribute vs CSS width/height Property
- How to create equal width table cell using CSS ?
- How to Create Shrink Header on Scroll using HTML, CSS and JavaScript ?
- How to Create Scroll Indicator using HTML CSS and JavaScript ?
- How to create Vertical Navigation Bar using HTML and CSS ?
- HTML | DOM body Property
- HTML | body Tag
- HTML | <body> background Attribute
- HTML | body text Attribute
- HTML | <body> bgcolor Attribute
- HTML | <body> link Attribute
- HTML | <body> vlink Attribute
- HTML | <body> alink Attribute
- How to get all HTML content from DOMParser excluding the outer body tag ?
- HTML | DOM Body Object
- HTML | <table> width Attribute
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


