Help:Table/Advanced

Tables can be used as formatting instrument, but consider using a multi column list instead.

Horizontal alignment in cells
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   stays centered regardless of global right or left text alignment via   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
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   does the header cell text not follow the global text alignment set in the top line of the table wikitext.

When using   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   in the top line of the table wikitext. See example in the next section.

Aligning the data in data columns to the right
Both tables below use   The table to the right also aligns the text and data in all cells (except header cells) to the right.

If most columns consist of data cells, then you might choose to align the data to the right. It is easy. Just add  to the top line of the wikitext for the table:

or

and so on.

Decimal point alignment
There is no easy way to align a column of numbers so that the decimal points align. See multiple ways below.

One way to align columns of numbers at the decimal point is to use two columns, with the first right-justified and the second left-justified.

Wikitext Rendered result

If the column of numbers appears in a table with cell padding or cell borders, you can still align the decimal points without an unsightly gap in the middle, by forcing the border and padding between those two columns off.

Wikitext Rendered result

Or alternatively the decimal cell template can be used:

Wikitext Rendered result

Using two columns like this does have the disadvantage that searching the web page (either with a browser or a search engine) will usually not be able to find text that straddles the column boundary.

Also, if the table has cell spacing (and thus ), meaning that cells have separate borders with a gap in between, that gap will still be visible.

A cruder way to align columns of numbers is to use a figure space, which is intended to be the width of a numeral, though is font-dependent in practice:

Wikitext Rendered result

More companions in this line are: punctuation space (&#x2008;) to substitute a period or a comma, &minus; instead of the easily available on the typewriter&#x27;s keyboard hyphen-dash – this is the same width as the plus sign, also figure dash possibly the most useful for telephone numbers which you obviously will not find on Wikipedia.

When using OpenType fonts one may also alternate between font-variant-numeric:tabular-nums and font-variant-numeric:proportional-nums – this is especially visible in the kerning of Arabic digit &lang;1&rang;. In the standard browser sans-serif fonts &lang;1&rang; occupies the same width as other digits, ie. works as if font-variant-numerals:tabular-nums was turned on.

Some may find 0 useful for the alignment.

As a last resort, when using pre-formatted text, you can dispense with the table feature entirely and simply start the lines with a space, and put spaces to position the numbers—however, there should be a good reason to use pre-formatted text in an article:

Wikitext (just spaces!): Rendered result 432.1 43.21   4.321

Column alignment
Table alignment can be used to align the cells in a whole column without adding code to each cell. For example, left aligning the first column, and center aligning the fourth column. And default aligning all other cells to the right:

Place  just before the table and then add some classes to the table.

The "col" classes never effect the header cells.

The "default" classes also effect 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
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 Fill in the "replace with" box with

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

Note the exclamation point for a header cell. Fill in the "replace with" box with

Then click "Replace all".

Side by side tables
You can place two or more tables side by side. But the tables must wrap (one dropping below the other) in narrow screens if horizontal scrolling is to be avoided. Here is the relevant wikitext below. More info here. A blind editor who uses a screen reader says this method is OK.

Note that the space between the tables happens because  is on a line by itself. If you want no space use this:

Narrow your browser window to see the tables below wrap. This works in mobile view too. Click on "mobile view" at the bottom of any Wikipedia page.

These tables are adapted excerpts from versions of Iceland men's national handball team. Many sport articles have side by side tables.

can not be added within the table wikitext. It must be added outside the table wikitext. This is because the table captions will not be correctly placed in mobile portrait view, or other narrow mobile screens, when the tables wrap. This is especially noticeable if the caption is longer. In that case when one table drops below the other, then the caption will be severely wrapped above only the first column of the table.

Side by side tables/images
You can add images to the mix too. Add   to align an item to the top. See CSS vertical-align property for other options. The tables and images will wrap depending on screen width. Narrow your browser window to see. For example:



Add   to align an item to the bottom. You can choose the alignment for each item. Note the star aligned to the bottom.





You can add indentation by adding margin styling to the first div:





These tables below wrap better with display:inline-table; instead of display:inline-grid;

Tables and images can also be floated to the right by adding float:right;

See example to the right from this version of List of U.S. states and territories by incarceration and correctional supervision rate.

Scrolling tables
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.

Horizontal scrolling table
Adding -x to overflow:auto → overflow-x:auto forces overflowing table data to fit within page's width margin or otherwise predetermined wiki markup.

Tables with sticky headers

 * History: See most recent Village Pump discussion. And other archived discussions: 1. 2. 3. 4. 5. See T42763. See also: Community Wishlist Survey 2022/Reading/floating table headers and Community Wishlist Survey 2021/Reading/Enable sticky table headers.

A 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 Template:Sticky header for examples, more info, and specialized cases. It is the latest template.

For the old template import style see Global Search at Toolforge. Search for "" - in quotes. To get transclusion count and list of articles.

See list of articles transcluding. See transclusion count.

Table with sticky header in simple scroll window
See: Template:Sticky header. Scrolling table with sticky column headers, but not sticky row headers. This is very useful for articles with long tables. It makes it much faster to move up and down the page. Especially for list articles with multiple tables. For example see this version of List of countries by firearm-related death rate. That version has 3 long tables placed in scroll windows. A blind person using a screen reader said here that they work fine.

Add  above any table using sticky header. And  at the end.

Add  to control the scroll window height. Change "50" to other numbers for other heights.

Advanced scrolling tables with sticky headers
''Note: Sticky left-side row headers do not work correctly if table is inside a collapsible show/hide box. See example.'' Note: There is discussion about creating a simpler template.

See this CSS talk page for help with scrolling tables that have both sticky column and row headers. See this article discussion too.

This has moved beyond COVID tables. See example here:
 * Iga Świątek career statistics

There are several very 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:2020 monthly cumulative COVID-19 death totals by country
 * 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
These two scrolling tables with row/column sticky headers are also automatically updated: See:
 * Template:COVID-19 pandemic death rates
 * Template:COVID-19 vaccination data
 * Template talk:COVID-19 pandemic death rates.
 * Template talk:COVID-19 vaccination data.

Sortable tables
See Help:Sortable tables for details of how to make sortable tables. Tables can be made sortable by adding  to the top line of the table wikitext. Individual columns can then be excluded from sorting with  in the lowest header cell.

Note:   must go in the header cell with the sorting icon. That will be the lowest header cell.

Numerical and year sorting problems

Collapsible tables
See also: Help:Collapsing, and more examples at mw:Manual:Collapsible elements.

Classes can also be used to collapse tables, so they are hidden by default. Use the class to enable collapsing behaviour. Collapsible tables can be sortable, too, by also including the  class. By default, a collapsible table begins expanded. To change this, add the additional class. Alternatively, you can add, instead of , 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  class, but  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. also does not require a header row in the table, as  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 Rendered result

Examples without a header row

Wikitext Rendered result

Wikitext Rendered result

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 '''Rendered result. Click "hide" to see'''

Rendered result (without . Click "hide" to see

Starting off collapsed: Wikitext '''Rendered result. Click "hide" to see'''

Rendered result (without . Click "hide" to see

Sorting and collapsing

It is possible to collapse a sortable table. To do so, you need to use this code:

For example:

Wikitext

Rendered result

If you want the table to default to collapsed state, use the code

in place of

Highlight rows on hover
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  and. Both add a light blue highlight when you hover your cursor over a table row. also makes the background of the non-header cells white. It does not override existing background colors set for cells or rows. ( produces a light gray background for data cells.)