GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Main page
  • Contents
  • Current events
  • Random article
  • About Wikipedia
  • Contact us
  • Help
  • Learn to edit
  • Community portal
  • Recent changes
  • Upload file
  • Special pages
  • Search
  • Donate

This view

  • Original page
  • New address

GeezerVison is showing en.wikipedia.org — Help:Advanced table features - Wikipedia

  • Help:Table
  • Wikitext
    • Intro
    • Editing
    • Expanding
    • Summary
  • VisualEditor
    • Intro
    • Editing
    • Adding
    • Expanding
    • Summary
  • Basics
    • Creation
    • Captions
    • Locations
    • Widths
  • Advanced
    • Sortable
    • Formatting
    • Collapsing
    • Conditional
    • History
  • Category
    • Templates
  • MOS
    • Accessibility
    • Tutorial
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

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

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

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.

Basic table
HeaderHeaderHeader
Textdata 12data 12
Textdata 123data 123
Textdata 1234data 1234
Aligned right
HeaderHeaderHeader
Textdata 12data 12
Textdata 123data 123
Textdata 1234data 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

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
|}

Rendered result

Heading
432.100
43.210
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, to left align the first column, center align the fourth column, and default align all other cells to the right:

{{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
defaultleftAlign all table cells left by default
defaultcenterAlign all table cells center by default
defaultrightAlign all table cells right by default
colNleftAlign the cells in column N left, where N is a number
colNcenterAlign the cells in column N center, where N is a number
colNrightAlign 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

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

See also: Template:Column-generating template families for other options.

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

Caption text
Header textHeader textHeader text
ExampleExampleExample
ExampleExampleExample
ExampleExampleExample
Caption text
Header text
(another)
Header textHeader text
ExampleExampleExample
ExampleExampleExample
ExampleExampleExample
ExampleExampleExample

Example on a table and image.

Caption text
Header textHeader textHeader text
ExampleExampleExample
ExampleExampleExample
ExampleExampleExample
New York City

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:
See: List of US states by minimum wage.
Simple map below using another method. See its talk page for info.
Medicaid coverage gap of US Affordable Care Act.

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

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.

Note US wealth concentration (green) 1989 Q3 to 2026 Q2.
Wealth percentile band from 1989 Q3 to 2026 Q2
Wealth percentile
band
Wealth
19892026
Top 0.1%8.6%15.0%
99-99.9%14.2%17.5%
90-99%38.0%36.4%
50-90%35.7%28.8%
Top cumulative wealth from 1989 Q3 to 2026 Q2
Cumulative
from top
Wealth
19892026
Top 0.1%8.6%15.0%
Top 1%22.8%32.5%
Top 10%60.8%68.9%
Top 50%96.5%97.7%
Bottom cumulative wealth from 1989 Q3 to 2026 Q2
Cumulative
from bottom
Wealth
19892026
Bottom 99.9%91.4%85.0%
Bottom 99%77.2%67.5%
Bottom 90%39.2%31.1%
Bottom 50%3.5%2.3%
{{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}}
|}

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.

Vertical scrolling table

abcdefghi
jklmnopqr
stuvwxyz
<div style="height:7em; overflow:auto; border:2px solid red;">
{| class=wikitable
|-
| abc || def || ghi
|- style="height: 100px;"
| jkl || style="width: 100px;" | mno || pqr
|-
| stu || vwx || yz
|}
</div>

Horizontal scrolling table

See also: Help:Table § Overflowing tables

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

abcdefghi
jklmnopqr
stuvwxyz
<div style="width:12em; overflow-x:auto; border:2px solid lime;">
{| class=wikitable style="width:25em"
|-
| abc || def || ghi
|- 
| jkl || style="height: 70px;" | mno || pqr
|-
| stu || vwx || yz
|}
</div>

Tables with sticky headers

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:

Caption
ColorAB C
Max101112
Red123
Lime456
Gold789
Blue101112
Total222630
Caption
Color Data
AB C
Red123
Lime456
Gold789
Blue101112
Max101112
Total222630

See list of articles transcluding {{sticky header}}. See transclusion count.

Scrolling tables with sticky column and row headers

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

Note: Contact User:Tol (talk) for help with automated tables.
See also: Category:Automatically updated COVID-19 pandemic table templates

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:

  • Template talk:COVID-19 pandemic death rates#Automation of updates.
  • Template talk:COVID-19 vaccination data#Automation of updates to this template.

Transclude a table from another page

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

Main pages: Help:Sortable tables and mw:Help:Sortable tables

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

See also: Help:Collapsing § Collapsible tables

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 (see § Sortable tables). 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.(see § Tables with captions)

Example with a header row

Wikitext

{| class="wikitable mw-collapsible mw-collapsed"
|-
! Header
|-
| Content that starts hidden
|-
| More hidden content
|}

Rendered result

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
|}

Rendered result

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
|}

Rendered result

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"
|+{{nowrap|Somewhat long table caption}}
|-
! Header
|-
| Content starts visible
|-
| More visible content
|}

Rendered result.
Click "hide" to collapse


Somewhat long table caption
Header
Content starts visible
More visible content

Rendered result
(without {{nowrap}}.
Click "hide" to collapse


Somewhat long table caption
Header
Content starts visible
More visible content

Starting off collapsed:

Wikitext

{| class="wikitable mw-collapsible mw-collapsed"
|+{{nowrap|Somewhat long table caption}}
|-
! Header
|-
| Content starts hidden
|-
| More hidden content
|}

Rendered result.
Click "show" to see


Somewhat long table caption
Header
Content starts hidden
More hidden content

Rendered result
(without {{nowrap}}.
Click "show" to see


Somewhat long table caption
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
|-
! scope=col | Alphabetic
! scope=col | Numeric
! scope=col | Date
! scope=col 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.
|}

Rendered result

Sortable and collapsible table
Alphabetic Numeric Date Unsortable
d202008-11-24This
b82004-03-01column
a61979-07-23cannot
c4.21492-12-08be
e01601-08-13sorted.

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

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

Guidelines, help, info: Manual of Style/Tables, MOS:COLOR (guideline), Help:Using colours, List of colors, Web colors.
Link colors: MOS:COLORS (help), Help:Link color, WebAIM: Link Contrast Checker.
Contrast tools: WebAIM: Contrast Checker, {{Color contrast ratio}}, {{Greater color contrast ratio}}, {{Color contrast conformance}}.
Templates: {{background color}}, {{font color}}, {{Coltit}}, {{Color}}.
Value-dependent color: {{cell color}}.
Night mode: mw:Recommendations for night mode compatibility on Wikimedia wikis.
Color blindness: Help:Using colours#Schemes for colour-blind readers.

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.

Wiki markup

{| 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

stuvwxyz
stuvwxyz
stuvwxyz

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

Avoid using background: none or background: transparent. See:

  • mw:Recommendations for night mode compatibility on Wikimedia wikis#Avoid using background: none or background: transparent

Background colors for column headers

style="background-color:…" works in sortable header cells. style="background:…" does not. It breaks sorting in that cell. See Help:Sortable tables. So use style="background-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

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:

  • WebAIM: Link Contrast Checker.

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

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.

Tracked in Phabricator
Task T376931

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

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

  • Help:Table for general information about using tables. It also has many links in §§ See also​ and External links, which are not duplicated here.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Help:Advanced_table_features&oldid=1384889448"
Categories:
  • Wikipedia how-to
  • Wikipedia tables
  • Wikipedia text help
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.