- How To Fixed Table Header Using CSS
- Fixed Table Header
- Method 1 – Fix Table Header Using Overflow Property
- Method 2 – Fix Table Header Using Display: Flex
- Conclusion
- How to Create a Table with a Fixed Header and Scrollable Body
- Example of creating a table with a scrollable body by using the position property:
- Result
- Example of creating a table with a scrollable body by using the display property:
- Css fix table header scroll
- A table with a fixed (sticky) header
- position: sticky
- HTML
- CSS
How To Fixed Table Header Using CSS
Here’s the simple CSS trick about the Fixed table header for a scrollable table. A fixed header makes table data effortless to understand. Designers often need to implement fixed headers for better and clear data table presentations.
Fixed Table Header
There are two methods to create a fixed table header using CSS. Here we will learn both the techniques with meticulous details to implement a scrollable table with fixed header.
Method 1 – Fix Table Header Using Overflow Property
In this method, we are going to use overflow property in tag with fixed height to freeze table header.
.fixed_header < width: 400px; table-layout: fixed; border-collapse: collapse; >.fixed_header tbody < display:block; width: 100%; overflow: auto; height: 100px; >.fixed_header thead tr < display: block; >.fixed_header thead < background: black; color:#fff; >.fixed_header th, .fixed_header td Data Data Data ************ ------------ ++++++++++++ ************ ------------ ++++++++++++ ************ ------------ ++++++++++++ ************ ------------ ++++++++++++ ************ ------------ ++++++++++++ ************ ------------ ++++++++++++ ************ ------------ ++++++++++++ ************ ------------ ++++++++++++
Method 2 – Fix Table Header Using Display: Flex
table.flex-table < display: flex; flex-direction: column; height: 100%; >table.flex-table thead, table.flex-table tbody < display: block; >table.flex-table thead < margin-right: 0px; >table.flex-table tbody < flex: 1; overflow-y: scroll; >table.flex-table tr < width: 100%; display: flex; >table.flex-table tr td, table.flex-table tr th < display: block; flex: 1; >table.flex-widths.flex-table-aligned thead > tr Data Data Data ************ ------------ ++++++++++++ ************ ------------ ++++++++++++ ************ ------------ ++++++++++++ ************ ------------ ++++++++++++ ************ ------------ ++++++++++++ ************ ------------ ++++++++++++ ************ ------------ ++++++++++++ ************ ------------ ++++++++++++
Conclusion
In this article, we have learned all the methods to make a scrollable table body with fixed table header. Also, both the methods are entirely based on pure CSS to make table headers fixed.
We can apply fixed table headers while listing a long data table in HTML. I hope now you can quickly implement tables with sticky table headers.
How to Create a Table with a Fixed Header and Scrollable Body
In this tutorial, find some methods of creating an HTML table, which has a fixed header and scrollable body. Of course, you need to use CSS.
It is possible to achieve such a result by setting the position property to “sticky” and specifying 0 as a value of the top property for the element.
As usual, you can open the Try it Yourself demo link and play with the properties to understand them better.
You can also read the comments in front of each line to understand the properties better.
Example of creating a table with a scrollable body by using the position property:
html> html> head> title>Title of the document title> style> .tableFixHead < overflow-y: auto; /* make the table scrollable if height is more than 200 px */ height: 200px; /* gives an initial height of 200px to the table */ > .tableFixHead thead th < position: sticky; /* make the table heads sticky */ top: 0px; /* table head will be placed from the top of the table and sticks to it */ > table < border-collapse: collapse; /* make the table borders collapse to each other */ width: 100%; > th, td < padding: 8px 16px; border: 1px solid #ccc; > th < background: #eee; > style> head> body> div class="tableFixHead"> table> thead> tr> th>Col 1 th> th>Col 2 th> tr> thead> tbody> tr> td>1.1 td> td>2.1 td> tr> tr> td>1.2 td> td>2.2 td> tr> tr> td>1.3 td> td>2.3 td> tr> tr> td>1.4 td> td>2.4 td> tr> tr> td>1.5 td> td>2.5 td> tr> tr> td>1.6 td> td>2.5 td> tr> tr> td>1.7 td> td>2.5 td> tr> tr> td>1.8 td> td>2.5 td> tr> tbody> table> div> body> html>
Result
Great! Huh? But let’s face it! I don’t like to see that scrollbar starting from the head section of the table!
So, let’s continue to the next example and fix that issue together!
Thers’s another method of creating a table with a fixed header and scrollable body. In the example below, we set the display to “block” for the element so that it’s possible to apply the height and overflow properties.
Example of creating a table with a scrollable body by using the display property:
html> html> head> title>Title of the document title> style> .fixed_header < width: 400px; table-layout: fixed; border-collapse: collapse; > .fixed_header tbody < display: block; width: 100%; overflow: auto; height: 100px; > .fixed_header thead tr < display: block; > .fixed_header thead < background: black; color: #fff; > .fixed_header th, .fixed_header td < padding: 5px; text-align: left; width: 200px; > style> head> body> table class="fixed_header"> thead> tr> th>Col 1 th> th>Col 2 th> th>Col 3 th> th>Col 4 th> th>Col 5 th> tr> thead> tbody> tr> td>1 td> td>2 td> td>3 td> td>4 td> td>5 td> tr> tr> td>6 td> td>7 td> td>8 td> td>9 td> td>10 td> tr> tr> td>11 td> td>12 td> td>13 td> td>14 td> td>15 td> tr> tr> td>16 td> td>17 td> td>18 td> td>19 td> td>20 td> tr> tr> td>21 td> td>22 td> td>23 td> td>24 td> td>25 td> tr> tbody> table> body> html>
As mentioned before, we used overflow: auto on the tbody along with the display: block .
Here’s our result, and enjoy the difference!
Col 1 | Col 2 | Col 3 | Col 4 | Col 5 |
---|---|---|---|---|
1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 |
As you have noticed, we didn’t use borders in the previous examples. However, if you need to add borders, you can simply use border property on all td tags.
.fixed_header td < border: 1px solid #797878; >
Col 1 | Col 2 | Col 3 | Col 4 | Col 5 |
---|---|---|---|---|
1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 |
Css fix table header scroll
- Difference between HTML and CSS
- What are physical tags in HTML?
- HTML | Deprecated Tags
- Container and Empty Tags in HTML
- HTML Comments
- How to set background image in HTML ?
- How to define a list item in HTML5?
- Difference between semantic and non-semantic elements
- How to Create Color Picker input box in HTML ?
- HTML Hex Color Codes
- HTML Block and Inline Elements
- HTML Drag and Drop
- How to set the name for the object in HTML5?
- How to add space between elements ?
- How to create a clickable button in HTML ?
- HTML Links
- How to link back out of a folder using the a-href tag?
- HTML ping Attribute
- HTML disabled attribute
- What is the use of “#” symbol in link URL ?
- How to create a link with a media attribute in HTML5 ?
- How to specify the source URL of the quote using HTML5 ?
- How to specify that the target will be downloaded when a user clicks on the hyperlink in HTML5 ?
- How to specify the hyperlink target for the area of an image in HTML5 ?
- How to make a HTML link that forces refresh ?
- How to set the language of text in the linked document in HTML5 ?
- How to specify what media/device the target URL is optimized for ?
- How to specify URL of resource to be used by the object in HTML5 ?
- How to use Anchor tag as submit button ?
- Elements of a form Tag
- How to set an alternate text for area in HTML5 ?
- How to specify one or more forms the object belongs to ?
- How to specify one or more forms the keygen element belongs to ?
- How to turn on/off form autocompletion in HTML ?
- How to specify that a group of related form elements should be disabled using HTML?
- How to specify how form-data should be encoded when submitting to server ?
- How to specify the URL that will process the data supplied through input(s) when the form is submitted?
- How to specify one or more forms the label belongs to ?
- How to specify multiple forms the select field belongs to in HTML ?
- How to change the type?
- How to specify which form element a label is bound to ?
- How to create a multiline input control text area in HTML5 ?
- How to create form validation by using only HTML ?
- HTML Emojis
- How to animate a straight line in linear motion using CSS ?
- How to specify the media type of the script in HTML5 ?
- How to display video controls in HTML5 ?
- How to mute video using HTML5 ?
- How to add controls to an audio in HTML5 ?
- Create Scanning Animation Loader using HTML & CSS
- How to specify media type of data specified in data attribute in HTML5 ?
- How to set the height and width of the video player in HTML5 ?
- How to check whether an image is loaded or not ?
- How to specify the type of the media resource in HTML5 ?
- How to Create Image Hovered Detail using HTML & CSS ?
- How to define media type of style tag in HTML5 ?
- How to set multiple media resources for elements in HTML5 ?
- How to set a single line break in HTML5 ?
- How to create a progress bar using HTML and CSS?
- How to create Perspective Text using HTML & CSS ?
- How to isolate a part of text that may be formatted in a different direction using HTML5 ?
- Design an Event Webpage using HTML & CSS
- How to Skew Text on Hover using HTML and CSS?
- Programming a slideshow with HTML and CSS
- How to specify that an option-group should be disabled in HTML5 ?
- How to disable the drop-down list in HTML5 ?
- How to define scalar measurement within a given range in HTML5 ?
- How to set the security algorithm of key in HTML5 ?
- How to set minimum and maximum value of range in HTML5 ?
A table with a fixed (sticky) header
Often, we come across situations where we have a table with lots of data in it. So when we scroll, the table header are hidden off screen, and soon we don’t know what columns we’re looking at. This problem can easily be addressed by wrapping the table in a div , and a bit of friendly CSS.
position: sticky
Sticky positioning is kind of a hybrid, between relative and fixed . To quote https://developer.mozilla.org/en-US/docs/Web/CSS/position, «The element is positioned according to the normal flow of the document, and then offset relative to its nearest scrolling ancestor and containing block (nearest block-level ancestor), including table-related elements, based on the values of top, right, bottom, and left.» Basically, the element occurs in the normal document flow, until you scroll past a certain offset, at which point it sticks to the top of the specified container. We’re going to wrap our table in a div (which we’ll make scrollable by setting its height and overflow-y properties), and make the th s position in a sticky way. We’ll also collapse the internal borders of the table using the border-collapse property. Is it really that simple? It actually is. Here’s the code, in its full unadulterated glory (don’t worry, I didn’t include any spoiler from season 2 of The Umbrella Academy, but seriously, go watch it):
HTML
Sticky Header charset="UTF-8" /> rel="stylesheet" href="src/styles.css" /> class="wrapper"> Number Name Superpower Email 1 Luther Super-strength luther@umbrella.edu 2 Diego Telekinesis diego@umbrella.edu 3 Allison Rumor allison@umbrella.edu 4 Klaus Seance klaus@umbrella.edu 5 Five Time-travel five@umbrella.edu 6 Ben Inter-dimensional monster ben@umbrella.edu 7 Vanya Apocalyptic destruction vanya@umbrella.edu - Reginald [Spoiler] powers reginald@umbrella.edu - Pogo Human communication pogo@umbrella.edu - Cha-Cha Ruthlessness chacha@temps.comm - Hazel Compassion hazel@temps.comm
CSS
body font-family: "Segoe UI", sans-serif; > table width: 100%; border-collapse: collapse; > th, td padding: 8px; > th text-align: left; background: #eee; position: sticky; top: 0px; > .wrapper border: 1px solid #ddd; width: 100%; height: 300px; overflow-y: auto; >