help:Table
{{Short description|How to create and use tables in markup}}
{{Table help}}
{{Wikipedia how to|H:TABLE}}
{{Wiki markup}}
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, or HTML syntax, and many different styles and tricks can be used to customise them.
Tools
= Source mode toolbar =
File:WikiEditor-advanced menu-en table.png is circled.]]
File:Toolbar2.PNG is on the right end.]]
To automatically insert a table, click 25px or File:Button insert table.png (Insert a table) on the edit toolbar. In the Vector toolbar the table icon is in the "Advanced" menu.
The following wikitext is inserted when Insert a table 25px is clicked.
class="wikitable"
|+ 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. Row numbers (1-3) and column letters (A-C) have been substituted below to help visualization.
Wikitext
class="wikitable"
|+ Caption text | ||
Header A | Header B | Header C |
---|---|---|
row 1 A | row 1 B | row 1 C |
row 2 A | row 2 B | row 2 C |
row 3 A | row 3 B | row 3 C |
Produces:
class="wikitable"
|+ Caption text | ||
Header A | Header B | Header C |
---|---|---|
row 1 A | row 1 B | row 1 C |
row 2 A | row 2 B | row 2 C |
row 3 A | row 3 B | row 3 C |
You can fill in the table while in source mode. Or use the visual editor (VE). To create more complex table structures in source mode with cells that span multiple rows or columns, see {{slink||Colspan and rowspan}}.
= Visual editor =
{{hatnote|See: Help:Tables and VisualEditor. And: Help:Creating tables#Tables and 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):
class="wikitable"
|+ ! ! ! ! |
|
| | |
|
| | |
|
| | |
For more complex table structures, Visual editor offers cell-merging operations; see details here.
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 =
{{See also|Help:Creating tables#Spreadsheet/database tables to wikitables|Wikipedia:Tools#Importing (converting) content to Wikipedia (MediaWiki) format|label 1=Help:Creating tables § Spreadsheet/database tables to wikitables|label 2=Wikipedia:Tools § Importing (converting) content to Wikipedia (MediaWiki) format}}
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.
Table basics
Tables can be added to articles and other pages using either standard HTML table elements, or with special wikicode markup developed just to facilitate creating tables without a knowledge of HTML.
= Markup =
{{See also|Help:Basic table markup}}
{{mw-datatable}}
class="wikitable mw-datatable"
|+Overview of basic table markup |
scope="col" | Table element
! scope="col" | Wikitext ! scope="col" | Required ! scope="col" | Usage notes |
---|
scope="row" | Table start
| {{kbd| | Required | Note: {{kbd| |
|-
! scope="row" | Caption
| {{kbd|
| Optional
| Only between table start and first table row.
|-
! scope="row" | Row
| {{kbd|
| Optional
| Can be omitted before the first row.
|-
! scope="row" | Header cell
| {{kbd|
| 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 (!
).
|-
! scope="row" | Data cell
| {{nowrap|{{kbd|
| 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.
|-
! scope="row" | Table end
| {{kbd|
| 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 with table scope include: class
, for example class="wikitable"
; or style
for CSS styling. Other attributes have row- or column scope, e.g., scope
, to indicate row or column header cells; rowspan
, to extend cells by more than one row; and colspan
, to extend cells by more than one column.
Wikicode syntax tutorial
{{Main|Help:Basic table markup}}
{{For|a history of the development of wikicode table syntax|Help:Table/History of pipe syntax development{{!}}/History of pipe syntax development}}
A table may be created entirely in wikicode using special table markup developed for the purpose. No knowledge of HTML is required.
= Begin and end delimiters =
Use
to begin a table, and
|
table code goes here |
Optional parameters like {{kbd|class}} may be placed on the Begin-table delimiter line. See {{slink||Class}} below.
= Caption =
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 |
= Row start =
= Cells =
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 |
For details about table cells, see {{slink||Cell operations}} below.
= Optional parameters =
{{anchor|wikitable class}} 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
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 |
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 {{tag|thead|o}}, {{tag|tbody|o}}, {{tag|tfoot|o}}, {{tag|colgroup|o}}, and {{tag|col|o}} elements are currently not supported in MediaWiki, {{as of|lc=y|2021|12}}.
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
- {{oldid2|1206274265|Sorting buttons in a separate row|2024 revision of {{delink|{{slink|Help:Sortable tables#Sorting buttons in a separate row}}}}}}
{{t|sort under}} is simpler for putting the sorting arrows below the header text in order to narrow a table.
With colspan
and rowspan
cells can span several columns or rows; {{see below|{{section link||Colspan and rowspan}}, below}}
= Displaying a pipe character =
Displaying a pipe character as part of the text of a table cell requires a workaround. See {{slink||Rendering the pipe}}.
= 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 "{{visible anchor|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
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 |
Examples
= 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
A
| B |
C
| D |
A | B |
C | D |
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
class=wikitable style="text-align: center;"
|+ Multiplication table |
×
! 1 ! 2 ! 3 |
---|
1
| 1 || 2 || 3 |
2
| 2 || 4 || 6 |
Produces
class=wikitable style="text-align: center;"
|+ Multiplication table |
×
! 1 ! 2 ! 3 |
---|
1
| 1 || 2 || 3 |
2
| 2 || 4 || 6 |
Whole table operations
= Class =
One or more classes may be added to the § Begin-table delimiter line at the top of the table, and may be used to supply predefined style to the table. The wikicode table {{kbd|class}} attribute corresponds directly to the "{{kbd|class}}" attribute of the HTML <table>
element. To add multiple classes to a table, place them all on the same line, embedded in double quotes and separated by a space:
class="wikitable sortable collapsible"
Some predefined classes are: {| class="wikitable sortable floatright" |+ A sortable wikitable |
breed
! ht ! wt |
---|
Labrador Retriever
| 23 | 70 |
French Bulldog
| 12 | 25 |
German Shepherd
| 27 | 78 |
Golden Retriever
| 23 | 71 |
English Bulldog
| 14 | 45 |
- {{kbd|wikitable}} – provides column headers with bold font on shaded background, and other common style. See here for details. This class should be used for almost all tables, unless there is a reason not to.
- {{kbd|sortable}} – adds up & down icons to column headers which enable sorting the table on a given column. See sortable tables.
- {{kbd|mw-collapsible}} – See collapsible tables.{{efn|group=note|Collapsible tables: the class {{kbd|collapsible}} is available, but no longer recommended; use {{kbd|mw-collapsible}} instead. See here for further details.}}
- {{kbd|plainrowheaders}} – applies left-aligned and normal-weight formatting of row headers. See Table header layout. Must be placed after wikitable if they both appear in the class declaration.
- {{kbd|floatright}} – same as applying
to the entire tablestyle="float:right; clear:right; margin-left:8px" - {{kbd|floatleft}} – same as applying
to the entire tablestyle="float:left; clear:left; margin-right:8px"
Note: Template:Table may also be used to apply classes to the § Begin-table delimiter line.
= Captions and summaries =
{{More|Wikipedia:Manual of Style/Accessibility/Data tables tutorial#Proper table captions and summaries}}
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 {{t|sro}} template: Template:Screen reader-only. Add the template to the table caption, and then only screen reader users will see it. Example:
:
For current table caption and summary guidelines see the w3.org page: [https://www.w3.org/WAI/tutorials/tables/caption-summary Caption & Summary, in Tables Tutorial]. Web Accessibility Initiative (WAI). The summary info in the paragraph below is out of date:
A {{em|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="{{var|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.
| ||||
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
class=wikitable
|+ style="text-align: left;" | Data reported for 2014–2015, by region{{dummy ref|99}}
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