Template:Sticky header
This template uses TemplateStyles: |
This template makes a table's column headers stick to the top of the screen as the table's data is scrolled in and out of view. It's used on tall tables that have column headers that might be difficult to remember as you scroll through the data.
Usage
Include this template by adding {{sticky header}}
or its redirect {{sticky-header}}
above a table. Add one of the following classes to the table start wikitext.
Class | Summary |
---|---|
sticky-header
|
Make the first header row top sticky. |
sticky-header-multi
|
Requires sortable table. Make multiple header rows top sticky. Avoid use with the sorttop class that becomes sticky after sorting. Avoid making headers sticky that aren't for the entire table (ex. section header rows). Avoid making excessively tall header rows sticky that might block too much data on short screens (ex. mobile landscape).
|
An optional sticky-header-scroll
class can be added to a div around the table to limit the height/width and make it scrollable. See #Scrollable table.
Single sticky header row
The sticky-header
class is used to make the first header row top sticky. Sortable is not required.
Color | A | B | C |
---|---|---|---|
Max | 10 | 11 | 12 |
Red | 1 | 2 | 3 |
Lime | 4 | 5 | 6 |
Gold | 7 | 8 | 9 |
Blue | 10 | 11 | 12 |
Total | 22 | 26 | 30 |
<syntaxhighlight lang=wikitext highlight=1-2>
Color | A | B | C |
---|---|---|---|
Max | 10 | 11 | 12 |
Red | 1 | 2 | 3 |
Lime | 4 | 5 | 6 |
Gold | 7 | 8 | 9 |
Blue | 10 | 11 | 12 |
Total | 22 | 26 | 30 |
</syntaxhighlight>
Multiple sticky header rows
The sticky-header-multi
class is used to make multiple header rows top sticky. Sortable table is required since sortable is currently the only way to move consecutive rows of column headers to the <thead>
element. If some or all columns should not be sortable, then class=unsortable
can be put in the header cell with the sorting icon. Table top will still be sticky. See Help:Sortable tables. If JavaScript is disabled, then sortable and this solution won't work.
Avoid using the sorttop
class since sortable moves those rows into the <thead>
element after sorting, which makes them top sticky too. A solution might be to move them to the bottom and use the sortbottom
class instead.
Color | Data | ||
---|---|---|---|
A | B | C | |
Red | 1 | 2 | 3 |
Lime | 4 | 5 | 6 |
Gold | 7 | 8 | 9 |
Blue | 10 | 11 | 12 |
Max | 10 | 11 | 12 |
Total | 22 | 26 | 30 |
<syntaxhighlight lang=wikitext highlight=1-2>
Color | Data | ||
---|---|---|---|
A | B | C | |
Red | 1 | 2 | 3 |
Lime | 4 | 5 | 6 |
Gold | 7 | 8 | 9 |
Blue | 10 | 11 | 12 |
Max | 10 | 11 | 12 |
Total | 22 | 26 | 30 |
</syntaxhighlight>
Header rows not for whole table
Consecutive rows of column headers are top sticky, so avoid adding a row of headers right under the column headers that don't apply to the entire table such as a section header meant to visually separate the table.
A solution might be to move each section to a column or separate tables, which also avoids accessibility issues per MOS:COLHEAD.
Another solution might be to add a blank row of data cells (| colspan=4 |
) between the last column header row and the first section header row so the latter is not included in the consecutive header rows.
Color | Data | ||
---|---|---|---|
A | B | C | |
Section 1 | |||
Red | 1 | 2 | 3 |
Lime | 4 | 5 | 6 |
Section 2 | |||
Gold | 7 | 8 | 9 |
Blue | 10 | 11 | 12 |
<syntaxhighlight lang=wikitext highlight=1-2,9-10>
Color | Data | ||
---|---|---|---|
A | B | C | |
Section 1 | |||
Red | 1 | 2 | 3 |
Lime | 4 | 5 | 6 |
Section 2 | |||
Gold | 7 | 8 | 9 |
Blue | 10 | 11 | 12 |
</syntaxhighlight>
Excessively tall header rows
Avoid excessively tall header rows that might block too much or all data when sticky on a small mobile screen, especially in landscape orientation. Some solutions might be to move some of the header text to the table caption, more concise header text, remove line-breaks (<br>
) in the headers, or split the table up into smaller tables to reduce headers.
Header group 1 | |||
---|---|---|---|
Header group 2 |
Header group 3 | ||
Header 1 |
Header 2 |
Header 3 |
Header 4 |
data | data | data | data |
data | data | data | data |
data | data | data | data |
data | data | data | data |
data | data | data | data |
data | data | data | data |
data | data | data | data |
data | data | data | data |
Scrollable table
An optional sticky-header-scroll
class can be added to a div around a very tall/wide table to limit its height/width and make it scrollable in a smaller area. This helps to prevent the table from overflowing beyond the width of the main content area. It also helps minimize the amount of vertical page scrolling to bypass table content, especially on small mobile screens. It works with the sticky-header
and sticky-header-multi
table classes. The column headers will stick to the top of the div instead of the page.
Column 1 | Column 2 | Column 3 | Column 4 | Column 5 | Column 6 | Column 7 | Column 8 | Column 9 | Column 10 | Column 11 | Column 12 | Column 13 | Column 14 | Column 15 | Column 16 | Column 17 | Column 18 | Column 19 | Column 20 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data | data |
<syntaxhighlight lang=wikitext highlight=1-3,10>
Column 1 | Column 2 | Column 3 | Column 4 | Column 5 | Column 6 | Column 7 | Column 8 | Column 9 | Column 10 | Column 11 | Column 12 | Column 13 | Column 14 | Column 15 | Column 16 | Column 17 | Column 18 | Column 19 | Column 20
⫶ |
---|
</syntaxhighlight>
Known issues
Tested in browsers on Windows 10, Windows 11, iOS 17 (iphone SE 2020 and iPhone 14 Pro Max), and Android 14 (Samsung Galaxy S21).
- On Android phones, table headers aren't top-sticky if at least one table using this template is wider than the screen, which is more likely in portrait orientation. The table's horizontal scroll doesn't work with this template, so wide tables span outside of the main content area making the entire page wider and requiring you to instead horizontally scroll the entire page. Zooming out to see the entire table makes the headers sticky, but also makes the text smaller and less readable the wider the table is. Currently, the only semi-solution is to view the zoomed out sticky headers in landscape orientation instead of portrait orientation so that the text is more readable. This issue doesn't exist with the
sticky-header-scroll
div class.
See also
- Help:Table/Advanced#Tables with sticky headers
- {{shy}} – Can be used to help narrow columns by adding a soft hyphen to a word to allow it to wrap.
More template styles for tables:
- {{sort under}} - moves the sorting arrows under the headers.
- {{row hover highlight}} - adds row hover highlighting, and option for white background.
- {{static row numbers}} - adds a column of row numbers to a table.
- {{table alignment}} - aligns the cells in a column, or a whole table.