Guides › Wiki

How to make a table in MediaWiki and Wikipedia

Wikipedia and other wikis that run on MediaWiki have their own table code. A table starts with {| and ends with |}, each row starts with |-, and each cell with |. This guide shows the syntax, the classes that Wikipedia uses, merged and sortable tables, and the mistakes that break a table. Each example shows the code and the table as Wikipedia shows it.

Updated

The syntax

MediaWiki
{| class="wikitable"
|+ The inner planets
|-
! Planet !! Diameter (km) !! Moons
|-
| Mercury || 4879 || 0
|-
| Venus || 12104 || 0
|-
| Earth || 12756 || 1
|-
| Mars || 6792 || 2
|}
The inner planets
Planet Diameter (km) Moons
Mercury 4879 0
Venus 12104 0
Earth 12756 1
Mars 6792 2
CodeMeaning
{|The start of the table, on its own line. The table's attributes, such as class="wikitable", follow it.
|+The caption, above the table. Optional.
|-A new row.
!A header cell. More header cells on the same line are separated by !!.
|A data cell. More data cells on the same line are separated by ||.
|}The end of the table, on its own line.

Each cell can also be on its own line. This code makes the same table (with two planets). It is longer, but easier to change, and the changes are easier to see in the page's history:

MediaWiki
{| class="wikitable"
|+ The inner planets
|-
! Planet
! Diameter (km)
! Moons
|-
| Mercury
| 4879
| 0
|-
| Venus
| 12104
| 0
|}

The wikitable class: lines and a grey header

Without a class, a wiki table has no lines and no colours:

MediaWiki
{|
! Planet !! Diameter (km) !! Moons
|-
| Mercury || 4879 || 0
|-
| Venus || 12104 || 0
|}
Planet Diameter (km) Moons
Mercury 4879 0
Venus 12104 0

class="wikitable", as in the first example, gives the table grey lines, a grey header and some space in the cells. Almost all tables on Wikipedia use it. Your wiki may have other classes: the class names are set in its style sheet (MediaWiki:Common.css).

Align, colour and size the cells

A cell's attributes go before its text, with a | between them. A row's go after |-, and the table's after {|:

MediaWiki
{| class="wikitable" style="text-align: right;"
! Planet !! Diameter (km) !! Moons
|-
! scope="row" style="text-align: left;" | Mercury
| 4879 || 0
|-
! scope="row" style="text-align: left;" | Venus
| 12104 || 0
|- style="background: #fff3c4;"
! scope="row" style="text-align: left;" | Earth
| 12756 || 1
|}
Planet Diameter (km) Moons
Mercury 4879 0
Venus 12104 0
Earth 12756 1
  • Align a column. Wiki tables have no setting for a column. Align the whole table (here, to the right, for the numbers), and give the cells that differ their own style="text-align: left;". The header cells of a wikitable stay centred.
  • Row headers. A cell that names its row can be a header cell (!) in the row. scope="row" tells screen readers that it is the header of its row.
  • Colours. style="background: #fff3c4;" on a row or a cell. Do not let a colour be the only way to tell something: add a word or a mark too.
  • Widths. style="width: 100%;" after {| makes the table as wide as the page. style="width: 10em;" on a header cell sets the width of its column.

Merge cells

MediaWiki
{| class="wikitable"
! rowspan="2" | Planet
! colspan="2" | Size
|-
! Diameter (km) !! Mass (10<sup>24</sup> kg)
|-
| [[Earth]] || 12756 || 5.97
|-
| [[Mars]] || 6792 || 0.642
|-
| colspan="3" | ''Source: NASA Planetary Fact Sheet''
|}
Planet Size
Diameter (km) Mass (1024 kg)
Earth 12756 5.97
Mars 6792 0.642
Source: NASA Planetary Fact Sheet
  • colspan="2" merges a cell with the cell to its right. rowspan="2" merges it with the cell under it.
  • Do not write the cells that a merged cell covers. The second row has only two cells, because Planet takes the first place in it.
  • A cell across the whole table, as in the last row, is good for a note or a source.

Sortable and collapsible tables

MediaWiki
{| class="wikitable sortable"
! Planet !! Diameter (km) !! class="unsortable" | Notes
|-
| Mercury || 4879 || Closest to the Sun
|-
| Venus || 12104 || Hottest planet
|}
  • class="wikitable sortable" puts sort buttons in the header. A click sorts the rows by that column, a second click in reverse. Numbers and dates are sorted as numbers and dates.
  • class="unsortable" on a header cell leaves its column without a button.
  • data-sort-value="..." on a cell sorts it by that value instead of its text, for example data-sort-value="1969-07-20" on a cell with 20 July 1969.
  • class="wikitable mw-collapsible" adds a button that hides the table's rows. With mw-collapsed as well, the table starts hidden.

Links, line breaks, lists and the | sign in cells

A cell holds wiki text: [[links]], '''bold''', ''italic'', templates and references.

  • Line breaks: write <br /> where the line should break.
  • Lists and paragraphs: start the cell's text on a new line after the |. A list (*, #) works only at the start of a line.
  • The | sign: a | in a cell's text ends the cell's attributes, so the text before it disappears. Write it as &#124; or <nowiki>|</nowiki>. A | inside a link ([[Earth|our planet]]) is not a problem.
  • A table in a template's parameter: there, each | of the table would end the parameter. Write the table's | signs as {{!}}. Wiki software expands {{!}} to a | before it reads the table.

Common mistakes

What you seeCause and fix
The rest of the page is in the table The |} at the end is missing.
All cells are in one long row The |- lines between the rows are missing.
The code shows as text {| is not at the start of its line. Put it on a line of its own.
An attribute, such as style="...", shows in the cell The | between the attributes and the text is missing: | style="color: red;" | text.
Part of a cell's text is missing The text has a |. Write it as &#124;.
A row is longer than the others A cell is written where a merged cell from the row above already is. Leave it out.

Click Show preview before you save, and check the table on a narrow window too. A wide table on Wikipedia scrolls sideways on phones.

Other wikis: DokuWiki, Confluence and Jira

Other wikis have other table code:

  • DokuWiki: ^ Planet ^ Moons ^ for a header row and | Earth | 1 | for a row.
  • Jira and Confluence wiki markup: ||Planet||Moons|| for a header row and |Earth|1| for a row.
  • GitHub and GitLab wikis use Markdown: see the Markdown tables guide.

Convert a table to wiki code and back

From Excel, Google Sheets or Word to wiki code

Copy the cells and paste them in the MediaWiki Tables Generator. They come with their merged cells, bold and italic text, colours and alignment. The generator writes them as rowspan, colspan and style attributes, and gives a style to the whole row or table when all its cells have it. To import a file, select File › Import CSV or Excel file.

Change an existing wiki table

Copy the table's code from the page's source (Edit source). In the generator, select File › Paste table data, paste the code and click Load. Change the table in the grid and copy the new code back.

From Wikipedia to Excel or Google Sheets

Select the table on the page, from its first cell to its last, copy it and paste it in the spreadsheet. In Google Sheets, =IMPORTHTML("https://en.wikipedia.org/wiki/...", "table", 1) reads the first table of a page and keeps it up to date. See also From a web page to Excel.

In our editor The cells of the first row are header cells (!). Line breaks in a cell become <br />, and wiki markup typed in a cell, such as [[a link]], goes into the code as it is.