Tables that can be used for aligning/recording data properly but sometimes it happens that the data in the table is too long so in order to read the data properly the header respective to various columns should be available all the time while traversing the table. In such cases, a sticky table head is required to make the table more informative and accurate which can be implemented using CSS attributes i.e is position and top of the elements of the head row
Syntax:
- In CSS file:
<style> .header{ position:sticky; top: 0 ; } </style> - In HTML file:
<th class="header" scope="col">
Below examples illustrates the approach:
Example 1: Table at the top with long list of columns and a fixed header.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> <link rel="stylesheet" href= integrity= "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous" /> integrity= "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"> </script> <script src= integrity= "sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"> </script> <script src= integrity= "sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"> </script> <style> .header { position: sticky; top:0; } .container { width: 600px; height: 300px; overflow: auto; } h1{ color: green; } </style> </head> <body> <div class="container"> <h1>GeeksforGeeks</h1> <b>Sticky header in table</b> <table class="table"> <thead style="position: sticky;top: 0" class="thead-dark"> <tr> <th class="header" scope="col">Course</th> <th class="header" scope="col">Start Date</th> <th class="header" scope="col">Fees</th> <th class="header" scope="col">Type</th> </tr> </thead> <tbody> <tr> <td>CAT</td> <td>1st Aug</td> <td>Free</td> <td>Online</td> </tr> <tr> <td>GATE</td> <td>5th July</td> <td>Free</td> <td>Online</td> </tr> <tr> <td>DSA</td> <td>1st July</td> <td>2499</td> <td>Online</td> </tr> <tr> <td>Java Backend</td> <td>28th March</td> <td>10999</td> <td>Offline</td> </tr> <tr> <td>SDE</td> <td>1st Sept</td> <td>299</td> <td>Online</td> </tr> <tr> <td>SUDO Placement</td> <td>20th Sept</td> <td>Free</td> <td>Online</td> </tr> </tbody> </table> </body> </html> |
Output:

Example 2 Header with a text followed by table with a sticky header.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> <link rel="stylesheet" href= integrity= "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous" /> integrity= "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"> </script> <script src= integrity= "sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"> </script> <script src= integrity= "sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"> </script> <style> .header { position: sticky; top:0; } body { height: 800px; } </style> </head> <body> <table class="table"> <thead style="position: sticky;top: 0" class="thead-dark"> <tr> <th class="header" scope="col">Course</th> <th class="header" scope="col">Start Date</th> <th class="header" scope="col">Fees</th> <th class="header" scope="col">Type</th> </tr> </thead> <tbody> <tr> <td>CAT</td> <td>1st Aug</td> <td>Free</td> <td>Online</td> </tr> <tr> <td>GATE</td> <td>5th July</td> <td>Free</td> <td>Online</td> </tr> <tr> <td>DSA</td> <td>1st July</td> <td>2499</td> <td>Online</td> </tr> <tr> <td>Java Backend</td> <td>28th March</td> <td>10999</td> <td>Offline</td> </tr> <tr> <td>SDE</td> <td>1st Sept</td> <td>299</td> <td>Online</td> </tr> <tr> <td>SUDO Placement</td> <td>20th Sept</td> <td>Free</td> <td>Online</td> </tr> </tbody> </table> </body> </html> |
Output:

Recommended Posts:
- How to set Bootstrap 4 scroll to the bottom of long sticky sidebar?
- How to create sticky table headers in Chrome ?
- Create a Sticky Social Media Bar using HTML and CSS
- How to create an Affix or sticky Navbar ?
- How to set sticky navbar only for first section of page?
- How to make whole row in a table clickable as link in Bootstrap ?
- How to make the existing bootstrap 2 table responsive?
- Include Bootstrap in AngularJS using ng-bootstrap
- Difference between bootstrap.css and bootstrap-theme.css
- Difference Between Bootstrap 3 and Bootstrap 4
- Difference between Bootstrap 4 and Bootstrap 5
- Begin Web Development with a Head Start
- HTML | <head> Tag
- Delete a Node from linked list without head pointer
- HTML | DOM head Property
- Lodash | _.head() Method
- How to add content in <head> section using jQuery/JavaScript?
- JavaScript SyntaxError - A declaration in the head of a for-of loop can't have an initializer
- Elements that are used in head section of HTML page
- How to load data from JSON into a Bootstrap Table?
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.


