Help:Advanced table features
This help page is a how-to guide. It explains concepts or processes used by the Wikipedia community. It is not one of Wikipedia's policies or guidelines and may reflect varying levels of consensus. |
Tables can be used as formatting instrument, but consider using a multi column list instead.
Horizontal alignment in cells
[edit]By default, text is aligned to the left of data cells.
By default, text is aligned to the center of header cells.
All of the above is true in both desktop and mobile view.
The alignment of header text in tables with class="wikitable" stays centered regardless of global right or left text alignment via style="text-align: VALUE;" on the top line of table wikitext.
Header text alignment in tables without class="wikitable" follows the global text alignment of the table.
Text alignment set in individual cells overrides everything else.
Aligning text in header cells versus other cells
[edit]Note: For vertical alignment of text see: Help:Table#Vertical alignment in cells.
If there is no global text alignment set in the top line of the table wikitext, then all text is left aligned, except for header cells which are default center aligned.
Only in tables with class="wikitable" does the header cell text not follow the global text alignment set in the top line of the table wikitext.
When using class="wikitable" header text now stays centered in both desktop and mobile view unless specifically changed per header cell. This is true wherever the header row is located (top, bottom, middle). Global text alignment for non-header cells is done by putting style="text-align: VALUE;" in the top line of the table wikitext. See example in the next section.
Aligning the data in data columns to the right
[edit]Both tables below use class="wikitable"
The table to the right also aligns the text and data in all cells (except header cells) to the right.
| Header | Header | Header |
|---|---|---|
| Text | data 12 | data 12 |
| Text | data 123 | data 123 |
| Text | data 1234 | data 1234 |
| Header | Header | Header |
|---|---|---|
| Text | data 12 | data 12 |
| Text | data 123 | data 123 |
| Text | data 1234 | data 1234 |
If most columns consist of data cells, then you might choose to align the data to the right. It is easy.
Just add style="text-align: right;" to the top line of the wikitext for the table:
{| class="wikitable" style="text-align: right;"
or
{| class="wikitable sortable" style="text-align: right;"
and so on.
Decimal point alignment
[edit]To align unlike decimals in a column, use Template:0 to pad one side with invisible zero characters, depending on the alignment.
Wikitext:
{| class="wikitable" style="text-align: right;"
! Heading
|-
| 432.1{{0|00}}
|-
| 43.21{{0|0}}
|-
| 4.321
|}
Produces:
| Heading |
|---|
| 432.1 |
| 43.21 |
| 4.321 |
Column alignment
[edit]{{Table alignment}} can be used to align the cells in a whole column without adding code to each cell. For example, to left align the first column, center align the fourth column, and default align all other cells to the right:
Wikitext:
{{Table alignment}}
{| class="wikitable sortable col1left col4center defaultright"
Place {{Table alignment}} just before the table and then add some classes to the table.
| classname | Purpose |
|---|---|
defaultleft | Align all table cells left by default |
defaultcenter | Align all table cells center by default |
defaultright | Align all table cells right by default |
colNleft | Align the cells in column N left, where N is a number |
colNcenter | Align the cells in column N center, where N is a number |
colNright | Align the cells in column N right, where N is a number |
The "col" classes never affect the header cells.
The "default" classes also affect the header cells if wikitable class is not used. With the wikitable class header cell content is always centered unless individually adjusted.
See template for more info, limitations, examples.
Aligning the text in the first column to the left
[edit]Template:Table alignment (see previous section) will not work on columns of row headers. There are some other limitations listed at Template:Table alignment. In these cases you can try the following method for the first column only.
This can be done in the wikitext source editor. In editing preferences check the box labeled: "enable the editing toolbar. This is sometimes called the '2010 wikitext editor'."
In the table section click "edit source" (wikitext editing). Click on "Advanced" in the editing toolbar. Then click on the "search and replace" icon on the right. In the popup form check the box for "Treat search string as a regular expression".
Fill in the "Search for" box with (\|-\n\|)
Fill in the "replace with" box with
$1style="text-align: left;" |
Then click "Replace all". All the text in the first column will be aligned to the left of their cells.
If for some reason those cells are header cells, then fill in the "Search for" box with
(\|-\n\!)
Note the exclamation point for a header cell. Fill in the "replace with" box with
$1style="text-align: left;" |
Then click "Replace all".
Side-by-side tables
[edit]This method wraps two or more blocks side by side in rows on wide screens, and unwraps responsively on narrow screens by stacking them vertically. These blocks can contain a table or other elements.
Example using Template:Flexbox wrap. Aligned to the top. It adds a 1em gap between blocks. The flex=none parameter value disables the grow and shrink behavior to maintain a table's or image's full dimensions.
Wikitext:
{{Flexbox wrap|start|flex=none}}
<!-- First table -->
{{Flexbox wrap|break|flex=none}}
<!-- Second table -->
{{Flexbox wrap|end}}
Produces:
| Header text | Header text | Header text |
|---|---|---|
| Example | Example | Example |
| Example | Example | Example |
| Example | Example | Example |
| Header text (another) | Header text | Header text |
|---|---|---|
| Example | Example | Example |
| Example | Example | Example |
| Example | Example | Example |
| Example | Example | Example |
Example on a table and image.
| Header text | Header text | Header text |
|---|---|---|
| Example | Example | Example |
| Example | Example | Example |
| Example | Example | Example |
To wrap long text, add the max-width: 30em; style (or a similar value) to the table that needs it, which allows it to narrow further on smaller screens. To avoid values that are too small, check the layout in mobile view (click "Mobile view" at the bottom).
Example on two layout tables, each with a description and image. Each table uses max-width: 310px; to responsively match its image width in order to wrap the description. For accessibility reasons, the tables use the role="none" attribute because they are for layout.
| Convert a US list/table to a state-by-state data map. With examples and step-by step instructions. Example: |
| Simple map below using another method. See its talk page for info. |
Note: Do not use display: inline-grid; or other non-table display values on a table, otherwise it can cause accessibility issues. Do not use display: inline-table; on a table, as it can break the responsive scrolling behavior on narrow screens (e.g., horizontal scrolling on tables wider than mobile screens). Do not use any display property values on a div wrapper for the same responsive issues just mentioned.
Note: Blocks are separated by a gap. Avoid adding margin or padding styles to the blocks because they can shift content in undesirable ways. The wikitable class adds 1em of top and bottom margin and 0 left and right margin to tables (margin: 1em 0;) to separate them from other elements and keep them flush left and right. A larger horizontal gap on a table (e.g., margin-right: 1em;) reduces the visible width of that table, most noticeable on mobile portrait with wider tables.
Side-by-side tables with single description
[edit]- Note: See here for an evaluation of 3 variations by a blind editor: "They all read well."
Example of a description and three tables nested in a layout table. These nested tables wrap on wide screens and vertically stack on narrow screens. For screen readers, each nested table has a caption that uses {{Screen reader-only}} to visually hide them in favor of the description, and still allow screen readers to read those captions. Note the role="none" attribute for a layout table, needed for accessibility reasons. Also note the margin: 0; style to remove the top and bottom margins on the nested tables. The template's gap separates the tables when side-by-side or vertically stacked.
Adapted from a version of Wealth inequality in the United States#Statistics.
Wikitext:
{{Mw-datatable}}{{Table alignment}}
{| class="wikitable" role="none"
| '''Note US wealth concentration (<span style="background-color: #9EFF9E; color: black;">green</span>) 1989 Q3 to 2026 Q2.'''
|-
|
{{Flexbox wrap|start|flex=none}}
{| class="wikitable mw-datatable defaultright col1left" style="margin: 0;"
|+ {{Screen reader-only|Wealth percentile band from 1989 Q3 to 2026 Q2}}
...
|}
{{Flexbox wrap|break|flex=none}}
<!-- Second nested table -->
{{Flexbox wrap|break|flex=none}}
<!-- Third nested table -->
{{Flexbox wrap|end}}
|}
Produces:
| Note US wealth concentration (green) 1989 Q3 to 2026 Q2. | |||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||
Scrolling tables
[edit]A whole table can be placed within a scrolling div so that new table lines appear on the screen as old table lines disappear. See MOS:SCROLL, and Help:Scrolling list, and this article discussion.
Vertical scrolling table
[edit]overflow-y: auto; forces overflowing content to scroll vertically within the container's height.
Wikitext:
<div style="height:7em; overflow-y: auto; border-width: 1px; border-style: solid;">
{| class="wikitable"
|-
| abc || def || ghi
|- style="height: 100px;"
| jkl || style="width: 100px;" | mno || pqr
|-
| stu || vwx || yz
|}
</div>
Produces:
| abc | def | ghi |
| jkl | mno | pqr |
| stu | vwx | yz |
Horizontal scrolling table
[edit]overflow-x: auto; forces overflowing content to scroll horizontally within the container's width.
Wikitext:
<div style="width: 12em; overflow-x: auto; border-width: 1px; border-style: solid;">
{| class="wikitable" style="width: 25em;"
|-
| abc || def || ghi
|-
| jkl || style="height: 70px;" | mno || pqr
|-
| stu || vwx || yz
|}
</div>
Produces:
| abc | def | ghi |
| jkl | mno | pqr |
| stu | vwx | yz |
Tables with sticky headers
[edit]- History: See most recent Village Pump discussion. And other archived discussions: 1. 2. 3. 4. 5. See Phab:T42763. See also: Meta: Community Wishlist Survey 2022/Reading/floating table headers and meta:Community Wishlist Survey 2021/Reading/Enable sticky table headers.
Template:Sticky header creates a sticky scrolling table in the sense of the vertical scrollbar for the whole page. When you scroll the page the table headers stay visible when the table goes beyond the top of the screen. See also: Template:Sticky table start. It has an option for side sticky headers too. It puts the table in a scrollbox, and the scrollbars are part of that box.
See {{Sticky header}} examples below:
| 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 |
| 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 |
See list of articles transcluding {{sticky header}}. See transclusion count.
Scrolling tables with sticky column and row headers
[edit]Note: Sticky left-side row headers do not work correctly if table is inside a collapsible show/hide box. See example. ("Timeshifter table" section)
See: Template:Sticky table start. It incorporates the old Covid CSS, and it is easier to use. See Special:WhatLinksHere/Template:Sticky table start for examples.
There are several advanced scrolling tables (with row and column headers that stay visible and sticky while scrolling) in COVID-19 pandemic by country and territory and in COVID-19 pandemic deaths. The scrolling and sticky headers work in cell phones, too. Widest scrolling tables are on top of the list below. Narrow your browser window until you see a horizontal scroll bar. Drag it left and right to see the sticky row headers that stay visible.
- Template:2021 2nd half. Monthly cumulative COVID-19 death totals by country
- Template:2022 first half. Monthly cumulative COVID-19 death totals by country
A totally blind screen reader user noted in a discussion here that the scrolling tables in the related article (COVID-19 pandemic deaths) were not a problem.
Automatically updated scrolling tables with sticky headers
[edit]These scrolling tables linked below with top/side sticky headers are also automatically updated. Narrow browser window to see sticky headers on the side.
- Template:COVID-19 pandemic death rates
- Template:COVID-19 vaccination data
- Template:COVID-19 pandemic death rates.
- Template:COVID-19 pandemic death rates by country.
See discussion:
Transclude a table from another page
[edit]A table can be put in a template. For example, see this list of life expectancy table template subpages. You can search for template subpages:
- Special:PrefixIndex - choose the template namespace.
{{Excerpt}} is the simplest tool for transcluding a table that is not in a template. Go there for many options. The simplest method:
{{Excerpt|Page source of table|Section with the table|this=This table is}}
It transcludes the table to wherever it is placed. It adds the following indented note above it in italics. The page section is linked, and there is an edit link for the source page:
- This table is an excerpt from Page source of table § Section with the table. [edit]
A hidden note <!-- NOTE --> just above the transcluded table is helpful. Such as:
- Table below is currently transcluded from here: https://en.wikipedia.org/wiki/Page_source_of_table#Section_with_the_table. If the source article or section name is changed, then the transclusion code below must also be changed.
A hidden note at the source section is helpful too. Such as:
- Table below is transcluded into another page: https://en.wikipedia.org/wiki/Page_with_transcluded_table#Section_with_the_table. Nothing needs to be added here for this to work. As long as the table remains in its own section, and that section name remains the same, then the transclusion works. If the section name here changes, then please change it in the {{excerpt}} template there.
All broken excerpts are automatically tracked at Category:Articles with broken excerpts and regularly fixed.
Sortable tables
[edit]
See Help:Sortable tables for details of how to make sortable tables. Tables can be made sortable by adding class="wikitable sortable" to the top line of the table wikitext. Individual columns can then be excluded from sorting with class="unsortable" in the lowest header cell. Note: that data-sort-type="VALUE" must go in the header cell with the sorting icon. That will be the lowest header cell. (See Help:Sortable tables § Forcing a column to have a particular data type).
Collapsible tables
[edit]Classes can also be used to collapse tables, so they are hidden by default. Use the class mw-collapsible to enable collapsing behaviour. Collapsible tables can be sortable, too, by also including the sortable class . By default, a collapsible table begins expanded. To change this, add the additional class mw-collapsed. Alternatively, you can add autocollapse, instead of mw-collapsed, which will automatically collapse the table if two or more collapsible elements are present on the page.
Note: Previous versions of this article recommended the collapsible class, but mw-collapsible is now preferred. It is a feature of the MediaWiki software, not a local customization, and can be used to make any element collapsible, not just tables. mw-collapsible also does not require a header row in the table, as collapsible did. Tables will show the "[hide]" / "[show]" controls in the first row of the table (whether or not it is a header row), unless a table caption is present.
Example with a header row
Wikitext:
{| class="wikitable mw-collapsible mw-collapsed"
|-
! Header
|-
| Content that starts hidden
|-
| More hidden content
|}
Produces:
| Header |
|---|
| Content that starts hidden |
| More hidden content |
Examples without a header row
Wikitext:
{| class="wikitable mw-collapsible"
|-
| First row is always visible
|-
| Content that starts visible
|}
Produces:
| First row is always visible |
| Content that starts visible |
Wikitext:
{| class="wikitable mw-collapsible mw-collapsed"
|-
| First row is always visible
|-
| Other rows start hidden
|}
Produces:
| First row is always visible |
| Other rows start hidden |
Tables with captions
Tables with captions will collapse to the table caption. The header will not be visible when collapsed. Use {{nowrap}} to keep the caption from being fit into a tiny vertical column when the table is collapsed.
Wikitext:
{| class="wikitable mw-collapsible"
|+ Somewhat long table caption
|-
! Header
|-
| Content starts visible
|-
| More visible content
|}
Produces:
| Header |
|---|
| Content starts visible |
| More visible content |
Starting off collapsed:
Wikitext:
{| class="wikitable mw-collapsible mw-collapsed"
|+ Somewhat long table caption
|-
! Header
|-
| Content starts hidden
|-
| More hidden content
|}
Produces:
| Header |
|---|
| Content starts hidden |
| More hidden content |
Sorting and collapsing
It is possible to collapse a sortable table. To do so, you need to use this code:
class="wikitable sortable mw-collapsible"
For example:
Wikitext:
{| class="wikitable sortable mw-collapsible"
|+ Sortable and collapsible table
|-
! Alphabetic
! Numeric
! Date
! class="unsortable" | Unsortable
|-
| d || 20 || 2008-11-24 || This
|-
| b || 8 || 2004-03-01 || column
|-
| a || 6 || 1979-07-23 || cannot
|-
| c || 4.2 || 1492-12-08 || be
|-
| e || 0 || 1601-08-13 || sorted.
|}
Produces:
| Alphabetic | Numeric | Date | Unsortable |
|---|---|---|---|
| d | 20 | 2008-11-24 | This |
| b | 8 | 2004-03-01 | column |
| a | 6 | 1979-07-23 | cannot |
| c | 4.2 | 1492-12-08 | be |
| e | 0 | 1601-08-13 | sorted. |
If you want the table to default to collapsed state, use the code
{| class="wikitable sortable mw-collapsible mw-collapsed"
in place of
{| class="wikitable sortable mw-collapsible"
Highlight rows on hover
[edit]Note: For usage examples, etc. go to {{row hover highlight}}.
To highlight rows upon hover, add {{row hover highlight}} (or its alias, {{mw-datatable}}) just above the table wikitext.
This enables use of class="mw-datatable" and class="hover-highlight". Both add a light blue highlight when you hover your cursor over a table row. class="mw-datatable" also makes the background of the non-header cells white. It does not override existing background colors set for cells or rows. (class="wikitable" produces a light gray background for data cells.)
Colors in tables
[edit]Background colors are common in table cells. In many sports articles for example. See here. For the accessibility reasons stated in WP:COLOR, information should not be communicated by the use of color alone.
Two ways of specifying color of text and background for a single cell are as follows:
Wikitext:
{|
|-
| style="background-color: red; color: white;"|abc
| def
| style="background-color: red;"|<span style="color: white;">ghi</span>
| jkl
|}
Produces:
| abc | def | ghi | jkl |
Like other parameters, colors can also be specified for a whole row or the whole table; parameters for a row override the value for the table, and those for a cell override those for a row.
There is no easy way to specify a color for a whole column: each cell in the column must be individually specified. Tools can make it easier.[which?]
Wikitext:
{| style="background-color: yellow; color: green;"
|-
| stu || style="background-color: silver;" | vwx || yz
|- style="background-color: red; color: white;"
| stu || style="background-color: silver;" | vwx || yz
|-
| stu || style="background-color: silver;" | vwx || yz
|}
Produces:
| stu | vwx | yz |
| stu | vwx | yz |
| stu | vwx | yz |
To force a cell to match one of the default colors of the class="wikitable" template, use style="background-color: #EAECF0;" for the darker header, and style="background-color: #F8F9FA;" for the lighter body. #A2A9B1 is the wikitables border color.
Avoid background:none or transparent
[edit]Avoid using background: none or background: transparent. See:
Background colors for column headers
[edit]style="background-color: COLOR;" works in sortable header cells. style="background: COLOR;" does not. It breaks sorting in that cell. See Help:Sortable tables. So use style="background-color: COLOR;" for all column header cells whether currently sortable or not. Someone may attempt to make them sortable later.
Color contrast with visited and unvisited links
[edit]See the link colors for various skins: Help:Link color#Standard colors. Unvisited Vector link: #3366CC. Visited Vector link: #795CB2.
Test contrast of link color with background color and text color:
Substitute various background colors for the default white Vector background (#FFFFFF):
- Unvisited Vector link color, white background color, and Vector text color.
- Visited Vector link color, white background color, and Vector text color.
From MOS:COLORS: For the default Vector 2022 skin normal text color is #202122, and unvisited blue link is #3366CC. "When links and references are not present, extra contrast is possible by setting the text color to pure black (#000000). If a dark background is desired, it should be used against white text (#FFFFFF) without links or references."
Color contrast of links in dark mode
[edit]In dark mode in article namespace table cells with background colors have black underlined links. For example:
- List of top-division football clubs in UEFA countries#Spain - scroll down a little to the table.
So any cell with WCAG-acceptable link color contrast in light mode has even better link color contrast in dark mode.
Unfortunately, the links are only black in article namespace. Not in template, help, talk, user, and other namespaces. For example see the Spain table template from the above-linked article: Template:2022–23 La Liga table.
Breaking up tables without losing colors
[edit]Normally, copying and pasting columns or rows removes the inline CSS styling such as cell colors. There is a way to break up a table (a too-wide table for example) into more tables without losing all the background colors, and other inline styling. Copy the table to 2 sandboxes (or one sandbox, and in the article itself). Then delete the columns not needed on one of the new tables. Repeat in the other sandbox (or in the article) except delete different columns.
See also
[edit]- Help:Table for general information about using tables. It also has many links in §§ See also and External links, which are not duplicated here.


