Fixed Header Table with Horizontal Scrollbar and Vertical Scrollbar on
This question addresses the issue of sticky headers and scrollbars in HTML/CSS tables when the container size reaches a certain point.
Existing Solutions
The issue is common, and a variety of solutions have been proposed, including:
CSS-Only Solution
While pure CSS cannot fully achieve fixed headers and scrollbars in all scenarios, it can provide a partial solution:
/* Give the table a container with fixed height and scrolling */ #container { height: 400px; overflow-y: scroll; } /* Make the table header fixed */ #header { position: sticky; top: -1px; }
CSS3 Solution (Not Supported in IE8)
For better control and improved support, CSS3 properties can be used:
/* Give the table a container with fixed height and scrolling */ #container { height: 400px; overflow-y: scroll; } /* Make the table header fixed and set its width */ #header { position: sticky; top: 0; width: 100%; }
Note: These solutions assume that the table header and body have fixed widths. If the widths are not fixed, additional CSS rules or calculations may be required.
The above is the detailed content of How to Implement a Fixed Header Table with Horizontal and Vertical Scrollbars Using CSS and JavaScript?. For more information, please follow other related articles on the PHP Chinese website!