Jump to content

Help:Table

From Wikipedia, the free encyclopedia
(Redirected from Help:COLSPAN)

A table is an arrangement of columns and rows that organizes and positions data or images. Tables can be created on Wikipedia pages using special wikitext syntax, and many different styles and tricks can be used to customise them.

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

Automated assistants

[edit]

Source mode toolbar

[edit]
Vector toolbar – default
Monobook toolbar

To automatically insert a table, click or (Insert a table) on the edit toolbar. In the Vector toolbar the table icon is in the "Advanced" menu. If "Insert a table" is not on the toolbar follow these directions to add it.

The following text is inserted when Insert a table is clicked:

{| class="wikitable"
|+ Caption text
|-
! Header text !! Header text !! Header text
|-
| Example || Example || Example
|-
| Example || Example || Example
|-
| Example || Example || Example
|}

This code produces the following table:

Caption text
Header text Header text Header text
Example Example Example
Example Example Example
Example Example Example

The sample text ("Header text" or "Example") is intended to be replaced with actual data. You can fill in the table while in source mode. Or use the visual editor (VE).

Visual editor

[edit]

With the visual editor (VE) you directly fill in the cells without having to go through wikitext. VE makes it easy to add or delete rows or columns. In VE this is what shows up when clicking the table icon (in the "Insert" menu):

In addition, it is usually possible to add or import a table that exists elsewhere (e.g., in a spreadsheet, on another website) directly into the visual editor by:

  • dragging and dropping a .csv file into the visual editor, or
  • selecting, copying, and pasting the table into the visual editor.

Using other tools

[edit]

Other tools, such as those used to create wiki tables from Excel, can be used to create wikitable markup from spreadsheet and database tables. See § External links for a list of some.

Basic table markup

[edit]
Overview of basic table markup
Table element Wikitext Required Usage notes
Table start {| Required
Table caption |+ Optional Only between table start and first table row.
Table row |- Optional Can be omitted before the first row.
Table header cell ! or !! Optional Consecutive table header cells may be added on the same line separated by double marks (!!); or start on new lines, each with its own single mark (!).
Table data cell | or || Optional Consecutive table data cells may be added on the same line separated by double marks (||) or start on new lines, each with its own single mark (|). This mark is also used to separate HTML attributes from cell and caption contents.
Table end |} Required
  • The above marks must start on a new line, except the double marks (|| and !!) for optionally adding consecutive cells to a single line.
  • Blank spaces at the beginning of a line are ignored.
  • Content may either follow its cell mark on the same line (after any optional HTML attributes); or on lines below the cell mark (beware of undesired paragraphs though). Content that uses wiki markup that itself needs to start on a new line, such as with lists, headings, or nested tables, must be on its own new line.
  • To insert a pipe character (|) into a table caption or cell, use the <nowiki>|</nowiki> escaping markup.

HTML attributes

[edit]

Each mark, except table end (|}), optionally accepts one or more attributes. Attributes must be on the same line as the mark.

  • Cells and captions (| or ||, ! or !!, and |+) hold content—separate any attribute from its content with a single pipe (|), with attributes preceding content.
  • Table and row marks ({| and |-) do not directly hold content. Do not add a pipe (|) after any attributes.

Commonly included attributes in tables include: class, for example class="wikitable"; style, for CSS styling; scope, to indicate row or column header cells; rowspan, to extend cells by more than one row; colspan, to extend cells by more than one column.

Pipe syntax tutorial

[edit]

Although HTML table syntax also works, special wikicode can be used as a shortcut to create a table. The vertical bar or "pipe" symbol ( | ) codes function exactly the same as HTML table markup, so a knowledge of HTML table code helps understand pipe code. The shortcuts are as follows:

The entire table is encased with curly brackets and a vertical bar character (a pipe). So use {| to begin a table, and |} to end it. Each one needs to be on its own line:

{|
  table code goes here
|}

An optional table caption is included with a line starting with a vertical bar and plus sign "|+" and the caption after it:

{|
|+ caption
  table code goes here
|}

To start a new table row, type a vertical bar and a hyphen on its own line: "|-". The codes for the cells in that row start on the next line.

{|
|+ The table's caption
|-
  row code goes here
|-
  next row code goes here
|}

Type the codes for each table cell in the next row, starting with a bar:

{|
|+ The table's caption
|-
| cell code goes here
|-
| next row cell code goes here
| next cell code goes here
|}

Cells can be separated with either a new line and a single bar, or by a double bar "||" on the same line. Both produce the same output:

Wikitext

{|
|+ The table's caption
|-
|Cell 1 || Cell 2 || Cell 3
|-
|Cell A
|Cell B
|Cell C
|-
|Cell x
|Cell y||Cell z
|}

Produces

The table's caption
Cell 1 Cell 2 Cell 3
Cell A Cell B Cell C
Cell x Cell y Cell z

Optional parameters can modify the display and styling of cells, rows, or the entire table. The simplest way to add styling is to set the wikitable CSS class, which in Wikipedia's external style sheet is defined to apply a gray color scheme and cell borders to tables using it:

Wikitext

{| class=wikitable
|+ The table's caption
! Column header 1
! Column header 2
! Column header 3
|-
! Row header 1
| Cell 2 || Cell 3
|-
! Row header A
| Cell B
| Cell C
|}

Produces

The table's caption
Column header 1 Column header 2 Column header 3
Row header 1 Cell 2 Cell 3
Row header A Cell B Cell C

The table parameters and cell parameters are the same as in HTML, see http://www.w3.org/TR/html401/struct/tables.html#edef-TABLE and Table (HTML). However, the <thead>, <tbody>, <tfoot>, <colgroup>, and <col> elements are currently not supported in MediaWiki, as of December 2021.

A table can be useful even if none of the cells have content. For example, the background colors of cells can be changed with cell parameters, making the table into a diagram, like meta:Template talk:Square 8x8 pentomino example. An "image" in the form of a table is much more convenient to edit than an uploaded image.

If all the cells in a row are empty the cells still show up. If the header cell is also empty for that row all the cells show up, but they are narrow. That can be fixed with a simple <br> in one of the cells. That is what is done here:

Each row must have the same number of cells as the other rows, so that the number of columns in the table remains consistent.

With colspan and rowspan cells can span several columns or rows; (see § Mélange, below)

Rendering the pipe

[edit]

When cell content that contains a pipe character does not render correctly, simply add an empty format for that cell. The second pipe character in a line of |cell code will not display; it is reserved for adding a format. Wikicode between the first and second pipe is a format, but since emptiness or an error there is ignored, it just disappears. When this happens, add a dummy format. (For a real format, see § HTML attributes.) Use a third pipe character to render your first pipe character.

Rendering the first pipe when it is the third pipe in the cell code.

Wikitext

{| class=wikitable
|-
| ''formatting'' |P|i|p|e|
|C|e|l|l|2|
|-
| ''formatting'' |P|i|p|e|| ''formatting'' |C|e|l|l|2|
|-
||P|i|p|e|s|| C|e|l|l|2|
|}

Produces

P|i|p|e| e|l|l|2|
P|i|p|e C|e|l|l|2|
P|i|p|e|s e|l|l|2|

The third and later pipe characters will render, but to display two adjacent pipe characters in a cell, (instead of having them act as the first pipe at the start of a new cell), other pipe-rendering options are needed. Instead of using a dummy format to render a pipe, you can render it directly by 1) <nowiki>|</nowiki> (preferred) or 2) html: &#124; or &#x7C;. Each line of cell code in the following table has one wikicode pipe.

Displaying adjacent pipes

Wikitext

{| class=wikitable
|-
|<nowiki>|</nowiki> Pipes34:<nowiki>||</nowiki>
|-
|&#124; Pipes34:&#124;&#x7C;
|}

Produces

| Pipes34:||
| Pipes34:||

Template {{!}}, because of the order in which things are parsed, is equivalent to typing in a single | pipe character. The single <nowiki />| parser-tag does not apply here. See how they do not escape the second pipe, as &#124 and <nowiki>|</nowiki> did above:

Common mechanisms that do not work in tables.

Wikitext

{| class=wikitable
|-
|<nowiki />| Pipe3:|
|-
|Pipe2:{{!}} Pipe3:{{!}}
|}

Produces

Pipe3:|
Pipe3:|

Scope

[edit]

Column headers are identified by ! scope="col" | instead of |. Row headers are identified by ! scope="row" | instead of |. Each header cell should be on a separate line in the wiki-markup. The scope="col" and scope="row" markup should be used for column and row headers in all data tables because it explicitly associates the header with the corresponding cells, which helps ensure a consistent experience for screen readers. The Manual of Style requires the use of scope for column and row headers.

For complex tables, when a header spans two columns or rows, use ! scope="colgroup" colspan="2" | or ! scope="rowgroup" rowspan="2" | respectively to clearly identify the header as a column header of two columns or a row header of two rows. When headers are unclear, this can cause accessibility issues; therefore, use id= to set a unique value without spaces on each header, then reference the id(s) on the data cells that have unclear headers using headers= with a space separating each id.

Header cells typically render differently from regular cells, depending on the browser. They are often rendered in a bold font and centered. If this rendering is not desired from an aesthetic point of view, the table can be styled with the "plainrowheaders" class which left-aligns the row headers and removes the bolding. Left-alignment of row headers only occurs if class=wikitable and scope=row are both used.

A typical example may be marked up like this:

Wikitext

{| class="wikitable plainrowheaders"
 |+ The table's caption
 ! scope=col | Column header 1
 ! scope=col | Column header 2
 ! scope=col | Column header 3
 |-
 ! scope=row | Row header 1
 | Cell 2 || Cell 3
 |-
 ! scope=row | Row header A
 | Cell B || Cell C
|}

Produces

The table's caption
Column header 1 Column header 2 Column header 3
Row header 1 Cell 2 Cell 3
Row header A Cell B Cell C

Row headers

[edit]

Note that with row headers you need to use a separate row in the wikitext for the row header cell. See the correct format in the last table in the previous section. Note the use of single and double pipes (bars).

Here below is what the same table (without class=plainrowheaders) looks like if the data cell wikitext is on the same line as the row header wikitext. Note that the data cell text is bolded, and the data cell backgrounds are the same shade of gray as the column and row headers. Data cells should normally have plain unbolded text, and a lighter background.

Wikitext

{| class=wikitable
 |+ The table's caption
 ! scope=col | Column header 1
 ! scope=col | Column header 2
 ! scope=col | Column header 3
 |-
 ! scope=row | Row header 1 || Cell 2 || Cell 3
 |-
 ! scope=row | Row header A || Cell B || Cell C
|}

Produces

The table's caption
Column header 1 Column header 2 Column header 3
Row header 1 Cell 2 Cell 3
Row header A Cell B Cell C

Simple tables

[edit]

Minimalist table

[edit]

Both of these generate the same output. Choose a style based on the number of cells in each row and the total text inside each cell.

Wikitext

{|
|-
| A
| B
|-
| C
| D
|}
{|
|-
| A || B
|-
| C || D
|}

Produces (note that there are no borders).

A B
C D

Multiplication table

[edit]

Note that in this example class="wikitable" is used to style the table with Wikipedia's external style sheet for tables. It adds borders, background shading, and bold header text.

Wikitext

{| class=wikitable style="text-align: center;"
|+ Multiplication table
|-
! ×
! 1
! 2
! 3
|-
! 1
| 1 || 2 || 3
|-
! 2
| 2 || 4 || 6
|}

Produces

Multiplication table
× 1 2 3
1 1 2 3
2 2 4 6

Width

[edit]
Note: width=X is obsolete in HTML 5, and so it could eventually be ignored by MediaWiki. See: Wikipedia: HTML 5#Table attributes. Use CSS styles instead: Such as style="width: Xpx;" or style="width: Xem;" or other CSS units. Number by itself signifies pixels.

See width examples on this subpage. There it is easier to narrow the browser window and not lose one's place as on a long page such as here on Help:Table. Overall table width does not act the same on Wikipedia mobile view as it does on mobile view on webpages outside Wikipedia. See the subpage examples.

Setting widths is discouraged for the most part on Wikipedia because it interferes with the ability of the browser to adjust content to suit the browser window, device size, zoom settings, user-end font size choices, and other constraints. Verbose notes columns are almost always a problem, especially when there are more than a few other columns. Editors frequently try to make the notes column wider, which messes up the other columns. For this reason, verbose notes columns should be avoided, or the notes should be shortened and links to longer notes below the table should be added instead.

style=max-width:Xem can be used in table headers. The following table excerpt is adapted from this version of List of countries by wealth per adult. The goal is to narrow the data columns, and have the country names spread out on one line each. All of this makes it easier when scanning down a country list. But the country names have to wordwrap if needed (in narrow portrait views on some cell phones, etc.). So style=max-width:Xem was used in selected column heads. It also avoids using header line breaks <br> which annoy people using screen readers due to the pauses. Em units are good because they increase in size along with the zoom setting. It is important to check if the max-width you have chosen also works correctly in cell phones, and is not breaking words. You may need to increase it a bit. Here is the relevant header wikitext:

Wikitext

{{static row numbers}}{{sticky header}}{{table alignment}}
{|class="wikitable sortable static-row-numbers sticky-header col1left" style=text-align:right
|+ Median and mean wealth per adult ([[USD]]) by country
|- 
! Location
! style=max-width:4em | Median wealth
! style=max-width:4em | Mean wealth
...

Produces

{{sort under}} narrows the table columns further:

{{static row numbers}}{{sticky header}}{{sort under}}{{table alignment}}
{|class="wikitable sortable static-row-numbers sticky-header sort-under col1left" style=text-align:right

If using {{sticky header}}, then for cell phones even narrower headers are better:

Here below is what the table looks like without style=max-width:Xem and without a caption.
Narrow your browser window to see how the table below contracts versus the ones above. The table above (even if some more columns are added) maintains one line per country for narrower browser and screen widths. So it is therefore more readable and scannable in long country tables. The table format below can greatly increase in number of lines, and require more vertical scrolling, especially if more columns are added.

Setting column widths. Other methods

[edit]

To force initial column widths to specific requirements, rather than accepting the width of the widest text element in a column's cells, follow this example. Note that wrap-around of text is forced for columns where the width requires it. Do not use min-width:Xpx;

Wikitext

{| class=wikitable
|-
! scope="col" style="width: 50px;" | Name
! scope="col" style="width: 250px;" | Effect
! scope="col" style="width: 350px;" | Games found in
|-
| Poké Ball || Regular Poké Ball || All versions
|-
| Great Ball || Better than a Poké Ball || All versions
|}

Produces

Name Effect Games found in
Poké Ball Regular Poké Ball All versions
Great Ball Better than a Poké Ball All versions

To set column widths in a table without headers, specify the width in the first cell for each column.

Wikitext

{| class=wikitable
|-
| style="width: 100pt;" | This column is 100 points wide
| style="width: 200pt;" | This column is 200 points wide
| style="width: 250pt;" | This column is 250 points wide
|-
| blah || blih || bluh
|}

Produces

This column is 100 points wide This column is 200 points wide This column is 250 points wide
blah blih bluh

You can also use percentages, such as style="width: 50%;" to equalize the widths of a two-column table.

One application of setting the widths is aligning columns of consecutive tables. The following are separate tables, with columns set to 350px and 225px.

Wikitext

{| class=wikitable
|-
! scope=col style="width: 350px;" | Country
! scope=col style="width: 225px;" | Capital
|-
| Netherlands || Amsterdam
|}

{| class=wikitable
|-
! scope=col style="width: 350px;" | Country
! scope=col style="width: 225px;" | Capital
|-
| France || Paris
|}

Produces

Country Capital
Netherlands Amsterdam
Country Capital
France Paris

Height

[edit]

The height of the whole table can be specified, as well as the height of a row. The height of the middle row below is set to style=height:7em and the total height of the whole table is set to style=height:14em units. Doing the math shows that the height of the top and bottom rows are 3.5 em units each. 3.5 + 3.5 + 7 = 14. If there is wordwrap then the height will increase to accommodate this (except on mobile where the bottom of the table will be cut off).

Wiki markup

{| class=wikitable style=height:14em;
|-
! Left !! Center !! Right
|-
| Top left cell || Top center cell || Top right cell
|- style=height:7em
| Middle left cell || Middle center cell || Middle right cell
|-
| Bottom left cell || Bottom center cell || Bottom right cell
|}

Produces

Left Center Right
Top left cell Top center cell Top right cell
Middle left cell Middle center cell Middle right cell
Bottom left cell Bottom center cell Bottom right cell

Whole table operations

[edit]

Captions and summaries

[edit]

Explicit table captions (or titles) are recommended for data tables as a best practice; the Wikipedia Manual of Style considers them a high priority for accessibility reasons (screen readers), as a caption is explicitly associated with the table, unlike a normal wikitext heading or introductory sentence. All data tables on Wikipedia require captions. A caption is provided with the |+ markup, similar to a table row (|-), but it does not contain any cells, and is not within the table border. Captions are always displayed, appearing as a title centered (in most browsers), above the table. A caption can be styled (with inline, not block, CSS), and may include wikilinks, reference citations, etc.

Many article editors dislike table captions when the table is directly below, or not far away from, a similar article heading. But screen reader users still need a table caption to quickly browse from table to table. In this case use the popular {{sro}} template: Template:Screen reader-only. Add the template to the table caption, and then only screen reader users will see it. Example:

|+ {{sro|Table caption}}

A summary provides an overview of the data of a table for text and audio browsers, and does not normally display in graphical browsers. The summary (also a high Manual of Style priority for tables) is a synopsis of content, and does not repeat the caption text; think of it as analogous to an image's alt description. A summary is added with summary="Summary text here.", on the same line as the {| that opened the table, along with any class= and other parameters for the table as a whole. The summary= attribute is, however, obsolete in HTML 5.

Wiki markup example showing left-aligned caption with a source citation:

{| class=wikitable
|+ style="text-align: left;" | Data reported for 2014–2015, by region<ref name="Garcia 2005"/>
|-
! scope=col | Year !! scope=col | Africa !! scope=col | Americas !! scope=col | Asia & Pacific !! scope=col | Europe
|-
! scope=row | 2014
| 2,300 || 8,950 || ''9,325'' || 4,200
|-
! scope=row | 2015
| 2,725 || ''9,200'' || 8,850 || 4,775
|}

Produces

Data reported for 2014–2015, by region[99]
Year Africa Americas Asia & Pacific Europe
2014 2,300 8,950 9,325 4,200
2015 2,725 9,200 8,850 4,775

Colspan and rowspan

[edit]

It is possible to create cells that stretch over two or more columns. For this, one uses |colspan=n | content. Similarly, one can create cells that stretch over two or more rows. This requires |rowspan=m | content. In the table code, one must leave out the cells that are covered by such a span. The resulting column- and row-counting must fit.

Wikitext

{| class=wikitable style="text-align: center;"
!col1
!col2
!col3
!col4
|-
!row1
| colspan=2 | A
<!-- column counting: cell 'B' can not exist -->
|C
|-
!row2
|AA
|BB
|CC
|-
!row3
|AAA
| rowspan=2 | BBB
|CCC
|-
!row4
|AAAA
<!-- row counting: cell 'BBBB' can not exist -->
|CCCC
|}

Produces

col1 col2 col3 col4
row1 A C
row2 AA BB CC
row3 AAA BBB CCC
row4 AAAA CCCC

In the code, the cell | colspan="2" | A spans two columns. Note that, in the next column, a cell expected to contain "B" does not exist.

Similar: in the code, cell | rowspan="2" | BBB spans two rows. A cell expected to contain "BBBB" does not exist.

Wikitext

{| class=wikitable
|-
! Column 1 !! Column 2 !! Column 3
|-
| rowspan=2 | A
| colspan=2 style="text-align: center;" | B
<!-- column 3 of this row occupied by cell B (which is to the left) -->
|-
<!-- column 1 of this row occupied by cell A (above) -->
| C
| D
|-
| E
| rowspan=2 colspan=2 style="text-align: center;" | F
<!-- column 3 of this row occupied by cell F (to the left) -->
|-
| G
<!-- columns 2 and 3 of this row occupied by cell F (above) -->
|-
| colspan=3 style="text-align: center;" | H
<!-- columns 2 and 3 of this row occupied by cell H (to the left) -->
|}

Produces

Column 1 Column 2 Column 3
A B
C D
E F
G
H

Note that using rowspan=2 for cell G combined with rowspan=3 for cell F to get another row below G and F won't work, because all (implicit) cells would be empty.

Below is the same table with the order of the declared rows and cells shown in parentheses. The uses of rowspan and colspan are also shown.

Wikitext

{| class=wikitable
|-
! Column 1<br>(row 1 cell 1) !! Column 2<br>(row 1 cell 2) !! Column 3<br>(row 1 cell 3)
|-
| rowspan=2 | A<br>(row 2 cell 1)<br><code>rowspan=2</code>
| colspan=2 style="text-align: center;" | B<br>(row 2 cell 2)<br><code>colspan=2</code>
|-
| C<br>(row 3 cell 1) <!-- column 1 occupied by cell A -->
| D<br>(row 3 cell 2)
|-
| E<br>(row 4 cell 1)
| rowspan=2 colspan=2 style="text-align: center;" |F<br>(row 4 cell 2)<br><code>rowspan=2 colspan=2</code>
|-
| G<br>(row 5 cell 1) <!-- column 2+3 occupied by cell F -->
|-
| colspan=3 style="text-align: center;" | H<br>(row 6 cell 1)<br><code>colspan=3</code>
|}

Produces

Column 1
(row 1 cell 1)
Column 2
(row 1 cell 2)
Column 3
(row 1 cell 3)
A
(row 2 cell 1)
rowspan=2
B
(row 2 cell 2)
colspan=2
C
(row 3 cell 1)
D
(row 3 cell 2)
E
(row 4 cell 1)
F
(row 4 cell 2)
rowspan=2 colspan=2
G
(row 5 cell 1)
H
(row 6 cell 1)
colspan=3

Note that although cell C is in column 2, C is the 1st cell declared in row 3, because column 1 is occupied by cell A, which was declared in row 2. Cell G is the only cell declared in row 5, because cell F occupies the other columns but was declared in row 4.

Setting borders

[edit]

Note: Wikipedia:HTML 5#Table attributes. CSS to replace obsolete attributes for borders, padding, spacing, etc.

Add a border around a table using the CSS property border: thickness style color;, for example border:3px dashed red. This example uses a solid (non-dashed) gray border that is one pixel wide:

Wikitext

{| style="border-spacing: 2px; border: 1px solid darkgray;"
! style="width: 140px;" | Left
! style="width: 150px;" | Center
! style="width: 130px;" | Right
|- style="text-align: center;"
| [[File:Starred.svg |120px]]
| [[File:Star full.svg |120px]]
| [[File:Stargreen.svg |120px]]
|- style="text-align: center;"
|Red star || Orange star || Green star
|}

Produces

Left Center Right
Red star Orange star Green star

Note the bottom-row texts are centered by style="text-align: center;" while the star images are not centered (i.e. left aligned).

As long as the File: specs omit the parameter |thumb they don't show the caption lines in the table (only during mouse-over). The border color darkgray matches typical tables or infoboxes in articles; however, it could be any color name (as in style="border: 1px solid darkgreen;") or use a hex-color (such as: #DDCCBB).

Borders of every cell

[edit]

Wikitext

{| border=1
|-
| A || B || C
|-
| D || E || F
|}

Produces

A B C
D E F

If all cells have the same border color, the resulting double borders may not be wanted; add the border-collapse: collapse; CSS property on the table opening tag to reduce them to single ones (cellspacing=... is obsolete).

Additionally, the W3C allows the use of the otherwise obsolete border= attribute on the table root ({|) if its value is "1". This adds a one-pixel border, in the default color, to the table and all of its cells at once:

Using the border-collapse property to combine the double borders, as described above:

Wikitext

{| border=1 style="border-collapse: collapse;"
|-
| A || B || C
|-
| D || E || F
|}

Produces

A B C
D E F

Float table left or right

[edit]

Two table classes floatleft and floatright (case sensitive) help floating the table and adjusting table margins so that they do not stick to the text. floatleft floats the table to the left and adjusts right margin. floatright does the opposite. Example:

Wikitext

This paragraph is before the table. The text in column 2 spans both rows because of format specifier "rowspan=2" so there is no coding for "Col 2" in the 2nd row, just Col 1 and Col 3.
{| class="wikitable floatright"
| Col 1, row 1
| rowspan="2" | Col 2, row 1 (and 2)
| Col 3, row 1
|-
| Col 1, row 2
| Col 3, row 2
|}
{| class="wikitable floatleft"
| Col 1, row 1
| rowspan="2" | Col 2, row 1 (and 2)
| Col 3, row 1
|-
| Col 1, row 2
| Col 3, row 2
|}
Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, explicabo. Nemo enim ipsam voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt, ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur?

As it appears in a browser:

This paragraph is before the table. The text in column 2 spans both rows because of format specifier "rowspan=2" so there is no coding for "Col 2" in the 2nd row, just Col 1 and Col 3.

Col 1, row 1 Col 2, row 1 (and 2) Col 3, row 1
Col 1, row 2 Col 3, row 2
Col 1, row 1 Col 2, row 1 (and 2) Col 3, row 1
Col 1, row 2 Col 3, row 2

Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, explicabo. Nemo enim ipsam voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt, ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur?

Note that although there are other ways to float a table, such as style="float:left;", style="float:right;", the only parameters that allow you to position the table under a floated multimedia object are floatleft and floatright. For example:

Aligning the table with the floatleft class produces:

Col 1, row 1 Col 2, row 1 (and 2) Col 3, row 1
Col 1, row 2 Col 3, row 2

Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, explicabo. Nemo enim ipsam voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt, ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur?

But aligning it with style="float:left;" produces:

Col 1, row 1 Col 2, row 1 (and 2) Col 3, row 1
Col 1, row 2 Col 3, row 2

Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, explicabo. Nemo enim ipsam voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt, ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur?

Centering tables

[edit]

align="center" is deprecated in HTML5, and does not work well in Mediawiki software. For example; it will not override the left alignment of tables via class=wikitable.

Centered tables can be achieved, but they don't "float"; that is to say, no text appears to either side. The trick is
{| style="margin-left: auto; margin-right: auto; border: none;" [note 1]

Wikitext

Text before table...
{| class="wikitable" style="margin-left: auto; margin-right: auto; border: none;"
|+ Cells left-aligned, table centered
|-
! scope="col" | Duis
! scope="col" | aute
! scope="col" | irure
|-
| dolor || in reprehenderit || in voluptate velit
|-
| esse cillum dolore || eu fugiat nulla || pariatur.
|}
...text after table

As it appears in a browser:

Text before table...

Cells left-aligned, table centered
Duis aute irure
dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur.

...text after table.

Static ('sticky') headers

[edit]

Headers can be frozen so they're always at the top (or left) as one scrolls through a large table. See {{Sticky header}} and {{Sticky table start}}.

Nested tables

[edit]

Nesting data tables with header cells makes it difficult for assistive screen readers to parse them sensibly.

Editors sometimes use headerless tables as an aid to content layout, especially where it is easier than the equivalent use of divs and CSS styling. For complex layouts, rowspan and colspan may be used, but again it is sometimes simpler and more maintainable to use nested tables.

Nested tables must start on a new line.

In the following example, five different tables are shown nested inside the cells of a sixth, main table. None has any header cells. Automatically, the two tables |A| and |B|B| are vertically aligned instead of the usual side-by-side of text characters in a cell. float is used to fix each of tables |C| and |D| to their own position within one cell of the table. This may be used for charts and schematics.

α cell2
NESTED
TABLE
the original table again
A
B B
C
D

Wikitext:

{| style="border: 1px solid black;"
| style="border: 1px solid black;" | &alpha;
| style="border: 1px solid black; text-align:center;" | cell2
{| style="border: 2px solid black; background: #ffffcc;" <!-- The nested table must be on a new line -->
| style="border: 2px solid darkgray;" | NESTED
|-
| style="border: 2px solid darkgray;" | TABLE
|}
| style="border: 1px solid black; vertical-align: bottom;" | the original table again
| style="border: 1px solid black; width:100px" |
{| style="border: 2px solid black; background: #ffffcc"
| style="border: 2px solid darkgray;" | A
|}
{| style="border: 2px solid black; background: #ffffcc"
| style="border: 2px solid darkgray;" | B
| style="border: 2px solid darkgray;" | B
|}
| style="border: 1px solid black; width: 50px" |
{| style="border: 2px solid black; background:#ffffcc; float:left"
| style="border: 2px solid darkgray;" | C
|}
{| style="border: 2px solid black; background:#ffffcc; float:right"
| style="border: 2px solid darkgray;" | D
|}
|}

Colors in tables

[edit]

Background colors are common in table cells. In many sports articles for example. See here.

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?]

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

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 using background: none or background: transparent. See:

Background colors for column headers

[edit]

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.

[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):

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."

[edit]

In dark mode in article namespace table cells with background colors have black underlined links. For example:

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.

Copying off-wiki tables with colored cells to wikis

[edit]

There are many off-wiki tables with useful coloring that meet WCAG access standards. Currently, there does not seem to be a way to copy those tables to a wiki and keep styling such as colors (background or text color). It is possible to convert PDF tables to Excel and keep the colors. Or to HTML tables and keep the colors. But there does not seem to be a way to copy any of those colored tables (PDF, Excel, HTML, etc.) to a wiki. If there is a way, please leave a note on Help talk:Table.

If desired one can keep the coloring by copying the table as an image. This may be necessary for frequently updated colored tables where it would be too time consuming to add color to many cells after each update. There is no problem with copyright for most data tables, colored or not. They are in the public domain. See Commons:Template:PD-chart.

Table cell templates

[edit]

See {{Table cell templates}} for a large set of templates to configure text and color in cells in a standard way, producing stock output. For example: "Yes" ({{Yes}}), "No" ({{No}}), "—" ({{N/A}}), "N/A" ({{N/A|N/A}}), "?" ({{dunno}}), on colored backgrounds.

Template output for selected table cell templates
Template usage Using template Without template
{{Yes}} Yes Yes
{{No}} No No
{{Dunno}} ? ?
{{N/A}}
{{N/A|N/A}} N/A N/A
{{n/a|n/a}} n/a n/a

For example, see Comparison of text editors, which makes frequent use of table cell templates.

More cell operations

[edit]

Setting cell parameters

[edit]

At the start of a cell, add your parameter followed by a single pipe. For example, style="width: 300px;"| sets that cell to a width of 300 pixels. To set more than one parameter, leave a space between each one.

Wikitext

{| style="color: white;"
|-
| style="background-color: red;"|cell1 
| style="width: 300px; background: blue;"|cell2
| style="background-color: green;"|cell3
|}

Produces

cell1 cell2 cell3

Vertical alignment in cells

[edit]

By default, text is aligned to the vertical middle of the cell:

Row header A longer piece of text. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Imagine someone scrolling down the page, seeing the tops of "empty" columns, and wondering why they're empty.
Short text

To align the text to the top of the cell, apply the style="vertical-align: top;" CSS to the rows (unfortunately, it seems to be necessary to apply this individually to every single row). The valign=... attribute is deprecated and MediaWiki may stop using it.

Wikitext

{| class=wikitable style="width: 400px;"
|- style="vertical-align: top;"
! scope="row" style="width: 10%;" | Row header
| style="width: 70%;"|A longer piece ...
| style="width: 20%;"|Short text
|}

Produces

Row header A longer piece of text. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Imagine someone scrolling down the page, seeing the tops of "empty" columns, and wondering why they're empty.
Short text

Cell content indenting and padding

[edit]

The contents of a cell can be indented or padded on any side. Also, the text can be aligned. In the second row the text is aligned to the right. See the following examples.

Wikitext

{| class=wikitable
|-
| Cell content that is not indented or padded
|-
| style="padding-left: 2em;" | style="padding-left: 2em;"
|-
| style="text-align:right; padding-right: 2em;" | style="text-align:right; padding-right: 2em;"
|-
| style="padding-top: 2em;" | style="padding-top: 2em;"
|-
| style="padding-bottom: 2em;" | style="padding-bottom: 2em;"
|-
| style="padding: 3em 5%;" | style="padding: 3em 5%;" {{space|4}} (Top and Bottom: 3em. Left and Right: 5%)
|-
| style="padding: 3em 4em 5%;" | style="padding: 3em 4em 5%;" {{space|4}} (Top: 3em. Left and Right: 4em. Bottom: 5%)
|-
| style="padding: 3%;" | style="padding: 3%;" {{space|4}} (Top, Right, Bottom, and Left: all 3%)
|-
| style="padding: 1em 20px 8% 9em;" | style="padding: 1em 20px 8% 9em;" {{space|4}} (Top: 1em. Right: 20px. Bottom: 8%. and Left: 9em.)
|}

Produces

Cell content that is not indented or padded
style="padding-left: 2em;"
style="text-align:right; padding-right: 2em;"
style="padding-top: 2em;"
style="padding-bottom: 2em;"
style="padding: 3em 5%;"      (Top and Bottom: 3em. Left and Right: 5%)
style="padding: 3em 4em 5%;"      (Top: 3em. Left and Right: 4em. Bottom: 5%)
style="padding: 3%;"      (Top, Right, Bottom, and Left: all 3%)
style="padding: 1em 20px 8% 9em;"      (Top: 1em. Right: 20px. Bottom: 8%. and Left: 9em.)

Pattern for arguments: The arguments to style="padding: " can be seen as being ordered by a 12-hour clock, starting at noon and going clockwise, in the following sense: "top" is associated with noon (i.e. 12 o'clock, the top of a clock), "right" is 3 o'clock, "bottom" is 6 o'clock, and "left" is 9 o'clock. The arguments are ordered clockwise starting at noon: top → right → bottom → left (see this[note 2] footnote for an example with an explanation).

This same order is also used elsewhere, such as when specifying a cell's borders with border-style: .

Setting default cell padding

Use cellpadding= to set the default padding for each cell in a table. If class=wikitable is used then cellpadding is ignored. The default space between cells can be changed using cellspacing=.

If cellpadding is not used

Wikitext

{| style="border:1px solid black"
|-
| Cell || Cell 
|-
| Cell || Cell 
|}

Produces

Cell Cell
Cell Cell

Using cellpadding=10:

Wikitext

{| cellpadding=10; style="border:1px solid black"
|-
| Cell || Cell 
|-
| Cell || Cell 
|}

Produces

Cell Cell
Cell Cell

Using cellpadding=0:

Wikitext

{| cellpadding=0; style="border:1px solid black"
|-
| Cell || Cell 
|-
| Cell || Cell 
|}

Produces

Cell Cell
Cell Cell

Using cellpadding=0 and cellspacing=0:

Wikitext

{| cellpadding=0; cellspacing=0; style="border:1px solid black"
|-
| Cell || Cell 
|-
| Cell || Cell 
|}

Produces

Cell Cell
Cell Cell

Individual cell borders

[edit]

The same CSS used for tables can be used in a cell's format specifier (enclosed in |...|) to put a border around each cell:

Wikitext

{| style="border-spacing: 2px; border: 1px solid darkgray;"
! style="width: 140px;" | Left
! style="width: 150px;" | Center
! style="width: 130px;" | Right
|- style="text-align: center;"
| style="border: 1px solid blue;"|
[[File:Starred.svg |120px]]
| style="border: 1px solid #777777;"|
[[File:Star full.svg |120px]]
| style="border: 1px solid #22AA55;"|<!-- greenish border -->
[[File:Stargreen.svg |120px]]
|- style="text-align: center;"
|Red star || Orange star || Green star
|}

Produces

Left Center Right

Red star Orange star Green star

Note that only the image cells have individual borders, not the text. The lower hex-colors (such as: #616161) are closer to black. Typically, all borders in a table would be one specific color.

Border styles

The style='border:' and style='border-style:' properties can accept the following arguments:

Argument Cell border Additional notes
none style="border-style: none;"
solid style="border-style: solid;"
double style="border-style: double;"
dashed style="border-style: dashed;"
dotted style="border-style: dotted;"
ridge style="border:5px ridge red;" 3D ridged border
groove style="border:5px groove blue;" 3D grooved border
inset style="border: 5px inset yellow;" 3D inset border
outset style="border: 5px outset cyan;" 3D outset border
inherit style="border-style: inherit;" Inherit value from parent
initial style="border-style: initial;" Set to default value
Borders of a cell using border-top, border-right, border-bottom, border-left

Wikitext

{| 
! style="border-top: solid;" | <code>style="border-top: solid;"</code>
|-
| style="border-top: solid 2px red; border-right: dashed 3px green; border-bottom: double 5px blue; border-left: dotted 6px yellow;" |<pre>border-top: solid 2px red;
border-right: dashed 3px green;
border-bottom: double 5px blue;
border-left: dotted 6px yellow;</pre>
|}

Produces

style="border-top: solid;"
border-top: solid 2px red;
border-right: dashed 3px green;
border-bottom: double 5px blue;
border-left: dotted 6px yellow;
Top, right, bottom, and left borders of a cell using style='border-style:'

To set the left, right, bottom, or top border of a single cell, one may use style='border-style:' which takes between 1 and 4 arguments, each of which is either none, solid, double, dotted, dashed, groove, ridge, inset, outset, inherit, or initial.

These arguments are ordered according to the pattern described here. For instance, style="border-style: solid none solid none;" where the four parameters correspond respectively to the
     'border-style: top right bottom left;'
borders of the cell. For reasons described after this example, there are many ways to change the following code that would not result in any changes to the table that is actually displayed.

Wikitext

{| class=wikitable
|-
| Top_Left
<!-- border-style: top right bottom left; -->
| style="border-style: solid solid none none;" | Top_Center
| Top_Right
|-
| style="border-style: none none solid solid;" | Middle_Left
| style="border-style: none none none none;" | Middle_Center
| style="border-style: solid solid none none;" | Middle_Right
|-
| Bottom_Left
| style="border-style: none none solid solid;" | Bottom_Center
| Bottom_Right
|}

Produces

Top_Left Top_Center Top_Right
Middle_Left Middle_Center Middle_Right
Bottom_Left Bottom_Center Bottom_Right

Note, however, that in the following table, none of the central cell's (i.e. Middle_Center's) borders are removed despite the code style="border-style: none none none none;":

Wikitext

{| class=wikitable
|-
| Top_Left || Top_Center || Top_Right
|-
| Middle_Left
| style="border-style: none none none none;" | Middle_Center
| Middle_Right
|-
| Bottom_Left || Bottom_Center || Bottom_Right
|}

Produces

Top_Left Top_Center Top_Right
Middle_Left Middle_Center Middle_Right
Bottom_Left Bottom_Center Bottom_Right

This happens because the code class="wikitable" places a border (top, right, bottom, and left) around every cell in the table so that, for instance, there are actually two borders between the cells "Middle_Center" and "Middle_Right". So to remove the border between cells "Middle_Center" and "Middle_Right", it is necessary to remove both the right border of "Middle_Center" and the left border of "Middle_Right":

Wikitext

{| class=wikitable
|-
| Top_Left || Top_Center || Top_Right
|-
| Middle_Left
<!-- 'border-style: top right bottom left;' -->
| style="border-style: none none none none;" | Middle_Center
| style="border-style: none none none none;" | Middle_Right
<!-- In the above line, the three left-most "none" arguments can be replaced with "solid" (or other valid arguments) and there would be no change to the table that is displayed. Only the fourth argument, which is Middle_Right's left border, needs to be "none". For example, the line above could be replaced with:
| style="border-style: solid solid solid none;" | Middle_Right
-->
|-
| Bottom_Left || Bottom_Center || Bottom_Right
|}

Produces

Top_Left Top_Center Top_Right
Middle_Left Middle_Center Middle_Right
Bottom_Left Bottom_Center Bottom_Right

To remove selected external borders of a wikitable, both remove them from the adjacent cells and begin the whole table with code like {| class="wikitable" style="border: none;".

Note that replacing {| class="wikitable" with
     {| style="border-collapse: collapse;"
has the effect of removing all cell borders that would otherwise appear by default around every cell in the table. With this change, you must insert a single cell border between two adjacent cells rather than remove two cell borders.

Row operations

[edit]

Height

[edit]

Border

[edit]

Row numbers

[edit]

See Help:Sortable tables sections on row numbers. And:

Column operations

[edit]

Nowrap

[edit]

In a table that spans the entire width of a page, cells narrower than the widest cell tend to wrap. To keep an entire column from wrapping, use style="white-space: nowrap;" in a non-header cell on the longest/widest cell to affect the entire column. This is not a good idea though, because in cell phones the column will not wrap, and part of the table may extend past the screen (especially in portrait view).

Without nowrap, as it appears in a browser:

Wikitext

{|class="wikitable sortable"
|-
! scope=col | Episode
! scope=col | Date
! scope=col | Summary
|-
|"The Journey Begins"
|January 1, 2010
|[[Lorem ipsum]] ... <!-- This text is a truncation of the actual (long) text displayed in the output below -->
|}

Produces

Episode Date Summary
"The Journey Begins" January 1, 2010 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

With nowrap, on both the Episode and Date columns, as it appears in a browser:

Wikitext

{|class="wikitable sortable"
|-
! scope=col | Episode
! scope=col | Date
! scope=col | Summary
|-
| style="white-space: nowrap;" |"The Journey Begins"
| style="white-space: nowrap;" |January 1, 2010
|[[Lorem ipsum]] ... <!-- This text is a truncation of the actual (long) text displayed in the output below -->
|}

Produces

Episode Date Summary
"The Journey Begins" January 1, 2010 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Tooltips

[edit]

You can add tooltips to columns by using the {{Tooltip}} template. Simply replace the column-title with {{Tooltip|Column title|The tool tip}}, which makes it appear like so: Column title.

Workarounds

[edit]

Overflowing tables

[edit]

In some cases where tables are so large horizontally that they go past the boundaries of one of Wikipedia's skins, such as Vector, the following code may be used. Place the following code above a wikitable initialization:

<div class="overflowbugx" style="overflow-x:auto;">

Then, after the closing bracket for the wikitable, place the following code:

</div>

This will prevent the table from causing the entire page to scroll when scrolling through the table horizontally.

Non-rectangular tables

[edit]

{{diagonal split header|HEADER-FOR-ROW-HEADERS|HEADER-FOR-COLUMN-HEADERS}} can be used to diagonally split a header cell, as in the top-left cell in the rendered result below:[note 3]

Wikitext

{| class=wikitable
! {{diagonal split header|From|To}}
! Solid !! Liquid !! Gas
|-
! Solid
| Solid-solid transformation || Melting || Sublimation
|-
! Liquid
| Freezing || {{sdash}} || Boiling/evaporation
|-
! Gas
| Deposition || Condensation || {{sdash}}
|}

Produces

To
From
Solid Liquid Gas
Solid Solid-solid transformation Melting Sublimation
Liquid Freezing Boiling/evaporation
Gas Deposition Condensation

Cell borders can be hidden by adding border: none; background: none; to the style attributes of either the table or the cell,[note 4] though this may not work in older browsers. Another use case for this is for implementing aligned multi-column tables:

Wikitext

{| class=wikitable style="border: none; background: none;"
! colspan=2 rowspan=2 style="border: none; background: none;" | [[File:Pfeil_SO.svg|none|link=|20px]]
! colspan=3 | To
|-
! Solid !! Liquid !! Gas
|-
! rowspan=3 | From
! Solid
| Solid–solid transformation || Melting || Sublimation
|-
! Liquid
| Freezing || {{sdash}} || Boiling/evaporation
|-
! Gas
| Deposition || Condensation || {{sdash}}
|}

Produces

To
Solid Liquid Gas
From Solid Solid–solid transformation Melting Sublimation
Liquid Freezing Boiling/evaporation
Gas Deposition Condensation

Note that the removal of the link on an image is dependent on it being purely decorative (as it will be ignored by assistive devices).

Wikitext

{| class=wikitable style="border: none; background: none;"
! scope=col | Year
! scope=col | Size
| rowspan=5 style="border: none; background: none;"|
! scope=col | Year
! scope=col | Size
| rowspan=5 style="border: none; background: none;"|
! scope=col | Year
! scope=col | Size
|-
| 1990 || 1000<br>(est) || 2000 || 1357 || 2010 || 1776
|-
| 1991 || 1010 || 2001 || 1471 || 2011 || 1888
|-
| colspan=2 style="text-align: center;"|| colspan=2 style="text-align: center;"|| colspan=2 style="text-align: center;"||-
| 1999 || 1234 || 2009 || 1616 || 2019 || 1997<br>(est)
|}

Produces

Year Size Year Size Year Size
1990 1000
(est)
2000 1357 2010 1776
1991 1010 2001 1471 2011 1888
1999 1234 2009 1616 2019 1997
(est)

Row template

[edit]

Regardless of whether wikitable format or HTML is used, the wikitext of the rows within a table, and sometimes even within a collection of tables, may have much in common, e.g.:

  • the basic code for a table row
  • code for color, alignment, and sorting mode
  • fixed texts such as units
  • special formats for sorting

In such a case, it can be useful to create a template that produces the syntax for a table row, with the data as parameters. This can have many advantages:

  • easily changing the order of columns, or removing a column
  • easily adding a new column if many elements of the new column are left blank (if the column is inserted and the existing fields are unnamed, use a named parameter for the new field to avoid adding blank parameter values to many template calls)
  • computing fields from other fields, e.g. population density from population and area
  • duplicating content and providing span tags with display: none; for the purpose of having one format for sorting and another for display
  • easy specification of a format for a whole column, such as color and alignment

Example:

Using {{Help:Table/example row template}}

Wikitext

{| class="wikitable sortable"
|-
! scope=col | a
! scope=col | b
! scope=col | a/b
{{Help:Table/example row template|  50|200}}
{{Help:Table/example row template|   8| 11}}
{{Help:Table/example row template|1000| 81}}
|}

Produces

a b a/b
50 200 0.25
8 11 0.72727272727273
1000 81 12.345679012346

Conditional table row

[edit]

For a conditional row in a table, we can have:

Wikitext

{| class=wikitable
 {{ #if:1|{{!}}-
  ! scope=row {{!}} row one, column one
  {{!}}row one, column two}}
 {{ #if: |{{!}}-
  ! scope=row {{!}} row two, column one
  {{!}}row two, column two}}
 |-
  ! scope=row {{!}} row three, column one
  | row three, column two
|}

Produces

row one, column one row one, column two
row three, column one row three, column two

With comments to explain how it works, where note how the second row is missing:

Wikitext

{| class=wikitable
 <!--
  Row one is shown because the '1' evaluates
  to TRUE.
 -->
 {{ #if:1|{{!}}-
  ! scope=row {{!}} row one, column one
  <!--
   Any {{!}}'s are evaluated to the pipe
   character '|' since the template '!' just
   contains '|'.
  -->
  {{!}}row one, column two}}
 <!--
  Row two is NOT shown because the space
  between the ':' and the '|' evaluates to FALSE.
 -->
 {{ #if: |{{!}}-
  ! scope=row {{!}} row two, column one
  {{!}}row two, column two}}
 <!--
  Row three is shown.
 -->
 |-
  ! scope=row {{!}} row three, column one
  | row three, column two
|}

Produces

row one, column one row one, column two
row three, column one row three, column two

Classes

[edit]

There are several other CSS classes, besides the basic class=wikitable, documented above.

In the first line of table code, after the {|, instead of specifying a style directly, you can also specify a CSS class, which may be used to apply styles. The style for this class can be specified in various ways:

  • in the software itself, per skin (for example the class sortable)
  • collectively for all users of one wiki in MediaWiki:Common.css (for example, on this and some other projects there is or was the class wikitable, later moved to shared.css)
  • separately per skin in MediaWiki:Monobook.css etc.
  • individually on one wiki in a user subpage
  • individually, but jointly for tables of the class concerned on all web pages, on the local computer of the user.

Instead of remembering table parameters, you just include an appropriate class after the {|. This helps keep table formatting consistent, and can allow a single change to the class to fix a problem or enhance the look of all the tables that are using it at once. For instance, this:

Wikitext

{| style="border-spacing: 2px;"
|+ Multiplication table
|-
! scope=col | &times;
! scope=col | 1
! scope=col | 2
! scope=col | 3
|-
! scope=row | 1
| 1 || 2 || 3
|-
! scope=row | 2
| 2 || 4 || 6
|}

Produces

Multiplication table
× 1 2 3
1 1 2 3
2 2 4 6

becomes this:

Wikitext

{| class=wikitable
|+ Multiplication table
|-
! scope=col | &times;
! scope=col | 1
! scope=col | 2
! scope=col | 3
|-
! scope=row | 1
| 1 || 2 || 3
|-
! scope=row | 2
| 2 || 4 || 6
|}

Produces

Multiplication table
× 1 2 3
1 1 2 3
2 2 4 6

simply by replacing inline CSS for the table by class=wikitable. This is because the wikitable class in MediaWiki:Common.css contains a number of table.wikitable CSS style rules. These are all applied at once when you mark a table with the class. You can then add additional style rules if desired. These override the class's rules, allowing you to use the class style as a base and build up on it:

Wikitext

{| class=wikitable style="font-style: italic; font-size: 120%; border: 3px dashed red;"
|+ Multiplication table
|-
! scope=col | &times;
! scope=col | 1
! scope=col | 2
! scope=col | 3
|-
! scope=row | 1
| 1 || 2 || 3
|-
! scope=row | 2
| 2 || 4 || 6
|}

Produces

Multiplication table
× 1 2 3
1 1 2 3
2 2 4 6

Notice that the table retains the gray background of the wikitable class, and the headers are still bold and centered. But now the text formatting has been overridden by the local style= statement; all of the text in the table has been made italic and 120% normal size, and the wikitable border has been replaced by the red dashed border.

Other table syntax

[edit]

The types of table syntax that MediaWiki supports are:

  1. Wikicode  |  syntax
  2. HTML (and XHTML)
  3. Mixed XHTML and wikicode (Do not use)

All three are supported by MediaWiki and create (currently) valid HTML output, but the pipe syntax is the simplest. Mixed HTML and wikicode  |  syntax (i.e., unclosed  |  and |- tags) don't necessarily remain browser-supported in the future, especially on mobile devices.

See also HTML element#Tables. Note, however, that the thead, tbody, tfoot, colgroup, and col elements are currently not not supported in MediaWiki, as of July 2015.

Table syntax comparison

[edit]
Table syntax comparison
XHTML Wiki-pipe
Table <table>...</table> {|
...
|}
Caption <caption>caption</caption> |+ caption
Header cell <th scope="col">column header</th>

<th scope="row">row header</th>

! scope="col" | column header

! scope="row" | row header

Row <tr>...</tr> |-
Data cell <td>cell1</td><td>cell2</td>
<td>cell3</td>
| cell1 || cell2 || cell3

or
| cell1
| cell2
| cell3

Cell attribute <td style="font-size: 87%;">cell1</td> | style="font-size: 87%;" | cell1
Sample table
<table>
  <tr>
    <td>1</td>
    <td>2</td>
  </tr>
  <tr>
    <td>3</td>
    <td>4</td>
  </tr>
</table>
{|
| 1 || 2
|-
| 3 || 4
|}
1 2
3 4
Pros
  • Can preview or debug with any XHTML editor
  • Can be indented for easier reading
  • Well-known
  • Insensitive to newlines
  • No characters like "|", which can collide with template and parser function syntax
  • Easy to write
  • Easy to read
  • Takes little space
  • Can be learned quickly
Cons
  • Tedious
  • Takes a lot of space
  • Difficult to read quickly
  • Debugging more difficult because of tag pairing requirements
  • Indented code might not match nesting.
  • Confusing newline behaviour as they only occasionally break cells.
  • Unfamiliar syntax for experienced HTML editors
  • Rigid structure
  • Cannot be indented for clarity
  • HTML tag text may be easier to read than pipes, plus signs, dashes, etc.
  • Requires using {{!}} to pass a  |  character in a parameter.
  • Sensitive to newlines; see Help:Newlines and spaces.

Pipe syntax. HTML output

[edit]

The pipe syntax, developed by Magnus Manske, substitutes pipes ( | ) and other symbols for HTML. There is an online script, which converts HTML tables to pipe-syntax tables.

The pipes must start at the beginning of a new line, except when separating parameters from content or when using || to separate cells on a single line. The parameters are optional.

Tables

[edit]

A table is defined by {| parameters |}, which generates <table params>...</table>.

Rows

[edit]

For each table, an HTML <tr> tag is generated for the first row. To start a new row, use:

|-

which generates another <tr>.

Parameters can be added like this:

|- params

which generates <tr params>.

Note:

  • <tr> tags are automatically opened before the first <td> equivalent
  • <tr> tags are automatically closed at another <tr> equivalent and at the </table> equivalent

Cells

[edit]

Cells are generated either like this:

|cell1
|cell2
|cell3

or like this:

|cell1||cell2||cell3

which both generate:

<td>cell1</td><td>cell2</td><td>cell3</td>.

The || equals a newline +  | .

Parameters in cells can be used like this:

|params|cell1||params|cell2||params|cell3

which results in:

<td params>cell1</td>
<td params>cell2</td>
<td params>cell3</td>

Headers

[edit]

The code used produces a <th>...</th>, functioning the same way as <td>...</td>, but with different style and semantic meaning. A  !  character is used instead of the opening  | , and !! can be used like ||, to enter multiple headers on the same line. Parameters still use "|", though. Example:

!params|cell1

Captions

[edit]

A <caption> tag is created by |+caption which generates the HTML <caption>caption</caption>.

You can also use parameters: |+params|caption which generates <caption params>caption</caption>.

Vertical column headers

[edit]

Sometimes it is desirable (such as in a table predominantly made of numbers) to rotate text such that it proceeds from top to bottom or bottom to top instead of from left to right or right to left. This can be done with CSS but the easiest way on Wikipedia is to enclose the text of each heading in a {{vertical header}} template. For example:

! {{vertical header|Date/Page}}

If the text includes an equals sign then replace it with {{=}}.

Rotated column headers using {{vertical header}}
Date/Page
05/08 4266 7828 7282 1105 224 161 916 506 231
04/08 4127 6190 6487 1139 241 205 1165 478 301

If you want to use sticky headers with vertical headers it is important to make the sticky header less tall for cell phone use. {{abbr}} is one way. Also by moving some info to the table caption. See example with various methods at {{vertical header}}.

Indenting tables

[edit]

While tables should not normally be indented, when their surrounding paragraphs are also indented, you can indent tables using margin-left.

To achieve the same indentation as a colon, use margin-left:1.6em.

Wikitext

{| class="wikitable" style="margin-left:1.6em;"
|-
! Header 1
! Header 2
|-
| row 1, cell 1
| row 1, cell 2
|-
| row 2, cell 1
| row 2, cell 2
|}

Produces

Header 1 Header 2
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2

Tables on talk pages

[edit]

Note that indentation applied to only the first line of the table wikitext (the line that begins with "{|") is sufficient to indent the entire table.

:::{| class=wikitable

Do not attempt to use colons for indentation anywhere within the rest of the table code (not even at the beginning of a line), as that will prevent the MediaWiki software from correctly reading the code for the table.

See also

[edit]

Templates

[edit]

Notes

[edit]
  1. ^ border: none; avoids an unsightly empty column in tables narrower than the browser window on Android Chrome.
  2. ^ In style="padding: 3em 4em 5%;", the value 4em is used for both the "left" padding and the "right" padding, so the order going clockwise is: top (3em) → right [and hence also left] (4em) → bottom (5%); there is no "→ left" in this case because the "left" padding has already been defined. In style="padding: 3em 5%;", the value 3em is used for both the "top" and "bottom" padding while the value 5% is used for both the "left" and "right" padding, so the order going clockwise is: top [and hence also bottom] (3em) → right [and hence also left] (5%); there is no "→ bottom" nor is there "→ left" in this case because the "bottom" and "left" padding have already been defined. The same reasoning also applies to style="padding: 1em 20px 8% 9em;", and style="padding: 3%;".
  3. ^ A simplified version of Template:Table_of_phase_transitions
  4. ^ Applying background: none to the entire table will, perhaps predictably, subtly alter its color palette.
[edit]

Wikimedia sister projects

[edit]