Help:Table

From English Wikipedia @ Freddythechick
For more help, see Help:Table, Help:Tables and VisualEditor, Help:Creating tables, Help:Wikitable, Help:Basic table markup, Help:Table/Advanced, WP:Advanced table formatting, Help:Tables and locations, and Help:Sortable tables. For style info, see Wikipedia:Manual of Style/Tables. For accessibility, see Wikipedia:Manual of Style/Accessibility/Data tables tutorial. <inputbox>

id=style-searchbox width=18 break=no type=fulltext searchbuttonlabel=Search all searchfilter=intitle:table namespaces=Help

</inputbox>

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

Source mode toolbar

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: <syntaxhighlight lang="wikitext">

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

</syntaxhighlight>

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

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

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

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

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

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 <syntaxhighlight lang="wikitext">

The table's caption
Cell 1 Cell 2 Cell 3
Cell A Cell B Cell C
Cell x Cell y Cell z
</syntaxhighlight>

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 <syntaxhighlight lang="wikitext">

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
</syntaxhighlight>

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

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 <syntaxhighlight lang=wikitext>

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|
</syntaxhighlight>

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 <syntaxhighlight lang=wikitext>

| Pipes34:||
| Pipes34:||
</syntaxhighlight>

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 <syntaxhighlight lang=wikitext>

Pipe3:|
Pipe3:|
</syntaxhighlight>

Produces

Pipe3:|
Pipe3:|

Scope

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 <syntaxhighlight lang="wikitext" highlight="7,8,10,11">

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
</syntaxhighlight>

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

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 <syntaxhighlight lang="wikitext" highlight="7,9">

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
</syntaxhighlight>

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

Minimalist table

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 <syntaxhighlight lang=wikitext>

A B
C D

</syntaxhighlight>

<syntaxhighlight lang=wikitext>

A B
C D
</syntaxhighlight>

Produces (note that there are no borders).

A B
C D

Multiplication table

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

<syntaxhighlight lang=wikitext>

Multiplication table
× 1 2 3
1 1 2 3
2 2 4 6
</syntaxhighlight>

Produces

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

Width

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 <syntaxhighlight lang=wikitext highlight=6-8>