Help:Table
Template:Short description Template:Table help Template:Wikipedia how to Template:Wiki markup <templatestyles src="Help:Table/styles.css" />
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.
ToolsEdit
Source mode toolbarEdit
To automatically insert a table, click File:Vector toolbar insert table button new.png 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 File:Vector toolbar insert table button new.png is clicked.
<syntaxhighlight lang=wikitext class=gridbox>
Header text | Header text | Header text |
---|---|---|
Example | Example | Example |
Example | Example | Example |
Example | Example | Example |
</syntaxhighlight>
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 <syntaxhighlight lang=wikitext>
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 |
</syntaxhighlight>
Produces:
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 Template:Slink.
Visual editorEdit
{{#invoke:Hatnote|hatnote}} 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):
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 toolsEdit
Template:See also 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 basicsEdit
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.
MarkupEdit
Template:See also Template:Mw-datatable
Table element | Wikitext | Required | Usage notes |
---|---|---|---|
Table start | Template:Kbd | Required | Note: Template:Kbd Entire table goes here Template:Kbd |
Caption | Template:Kbd | Optional | Only between table start and first table row. |
Row | Template:Kbd | Optional | Can be omitted before the first row. |
Header cell | Template:Kbd or Template: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 (! ).
|
Data cell | Template:Nowrap | 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 | Template: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 attributesEdit
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 tutorialEdit
<templatestyles src="Help:Table/styles.css" /> {{#invoke:Labelled list hatnote|labelledList|Main article|Main articles|Main page|Main pages}} Template:For
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 delimitersEdit
Use {|
to begin a table, and |}
to end it. Each one needs to be on its own line:
<syntaxhighlight lang=wikitext class=gridbox>
table code goes here</syntaxhighlight>
Optional parameters like Template:Kbd may be placed on the Begin-table delimiter line. See Template:Slink below.
CaptionEdit
An optional table caption is included with a line starting with a vertical bar and plus sign "|+
" and the caption after it:
<syntaxhighlight lang=wikitext highlight=2 class=gridbox>
</syntaxhighlight>
Row startEdit
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.
<syntaxhighlight lang=wikitext highlight=3,5 class=gridbox>
row code goes here next row code goes here</syntaxhighlight>
For details about rows, see Template:Slink below.
CellsEdit
Type the codes for each table cell in the next row, starting with a bar:
<syntaxhighlight lang=wikitext highlight=4,6,7 class=gridbox>
cell code goes here | |
next row cell code goes here | next cell code goes here |
</syntaxhighlight>
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" highlight=4>
Cell 1 | Cell 2 | Cell 3 |
Cell A | Cell B | Cell C |
Cell x | Cell y | Cell z |
Produces
Cell 1 | Cell 2 | Cell 3 |
Cell A | Cell B | Cell C |
Cell x | Cell y | Cell z |
For details about table cells, see Template:Slink below.
Optional parametersEdit
Template:Anchor 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" highlight=1>
Column header 1 | Column header 2 | Column header 3 |
---|---|---|
Row header 1 | Cell 2 | Cell 3 |
Row header A | Cell B | Cell C |
Produces
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 Template:Tag, Template:Tag, Template:Tag, Template:Tag, and Template:Tag elements are currently not supported in MediaWiki, Template:As of.
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:
{{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; Template:See below
Displaying a pipe characterEdit
Displaying a pipe character as part of the text of a table cell requires a workaround. See Template:Slink.
ScopeEdit
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 "Template:Visible anchor" 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">
Column header 1 | Column header 2 | Column header 3 |
---|---|---|
Row header 1 | Cell 2 | Cell 3 |
Row header A | Cell B | Cell C |
Produces
Column header 1 | Column header 2 | Column header 3 |
---|---|---|
Row header 1 | Cell 2 | Cell 3 |
Row header A | Cell B | Cell C |
ExamplesEdit
Minimalist tableEdit
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 highlight=3,5>
A | B |
C | D |
Produces (note that there are no borders).
A | B |
C | D |
Multiplication tableEdit
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>
× | 1 | 2 | 3 |
---|---|---|---|
1 | 1 | 2 | 3 |
2 | 2 | 4 | 6 |
Produces
× | 1 | 2 | 3 |
---|---|---|---|
1 | 1 | 2 | 3 |
2 | 2 | 4 | 6 |
Whole table operationsEdit
ClassEdit
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 Template:Kbd attribute corresponds directly to the "Template:Kbd" 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:
<syntaxhighlight lang=wikitext class=grid>
breed | ht | wt |
---|---|---|
Labrador Retriever | 23 | 70 |
French Bulldog | 12 | 25 |
German Shepherd | 27 | 78 |
Golden Retriever | 23 | 71 |
English Bulldog | 14 | 45 |
- Template:Kbd – 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.
- Template:Kbd – adds up & down icons to column headers which enable sorting the table on a given column. See sortable tables.
- Template:Kbd – See collapsible tables.Template:Efn
- Template:Kbd – 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.
- Template:Kbd – same as applying
style="float:right; clear:right; margin-left:8px"
to the entire table - Template:Kbd – same as applying
style="float:left; clear:left; margin-right:8px"
to the entire table
Note: Template:Table may also be used to apply classes to the § Begin-table delimiter line.
Captions and summariesEdit
Template:More
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:
- <syntaxhighlight lang="wikitext" inline>|+ Template:Sro</syntaxhighlight>
For current table caption and summary guidelines see the w3.org page: Caption & Summary, in Tables Tutorial. Web Accessibility Initiative (WAI). The summary info in the paragraph below is out of date:
A Template:Em 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="Template:Var"
, 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: <syntaxhighlight lang="wikitext">
Year | Africa | Americas | Asia & Pacific | Europe |
---|---|---|---|---|
2014 | 2,300 | 8,950 | 9,325 | 4,200 |
2015 | 2,725 | 9,200 | 8,850 | 4,775 |
Produces
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 rowspanEdit
Template:Anchor
It is possible to create cells that stretch over two or more columns. For this, one uses |colspan=Template:Var | Template:Var
.
Similarly, one can create cells that stretch over two or more rows. This requires |rowspan=Template:Var | Template:Var
. In the table code, one must Template:Em. The resulting column- and row-counting must fit.
Wikitext <syntaxhighlight lang=wikitext line highlight=8,19>
col1 | col2 | col3 | col4 |
---|---|---|---|
row1 | A | C | |
row2 | AA | BB | CC |
row3 | AAA | BBB | CCC |
row4 | AAAA | 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, Template:Em.
Similar: in the code, cell | rowspan="2" | BBB
spans two rows. Template:Em.
Wikitext <syntaxhighlight lang=wikitext line highlight=14>
Column 1 | Column 2 | Column 3 |
---|---|---|
A | B | |
C | D | |
E | F | |
G | ||
H |
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 <syntaxhighlight lang=wikitext>
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
|
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.
Table styleEdit
A Template:Kbd element may be added to apply to the entire table, to all the cells § in a row, or just to individual cells in the table. Adding a style to a column requires a template such as Template:Tl (for horizontal alignment of text).
To add style to the entire table, add the Template:Kbd element to the § Begin-table delimiter line at the top of the table. Note that in some cases, it may be better to use the style encapsulated in one of the predefined classes; see Template:Slink.
Horizontal text alignmentEdit
Template:More
Text alignment (right/center/left) for the whole table (except the headers in a table using class=wikitable
) can be done at the top line of the table wikitext:
<syntaxhighlight lang=wikitext highlight=1 class=gridbox>
</syntaxhighlight>
Text alignment for a column can be done with Template:Tl.
HeightEdit
The height of the whole table can be specified using standard CSS style properties on the § start table indicator line:
<syntaxhighlight lang=wikitext highlight=1,6 class=gridbox>
. . .</syntaxhighlight>
Note that you may also specify the § height of individual rows, and if they add up to more than the table height you specified or if word wrapping increases row height, the table height you specified will be ignored and the table height increased as needed to accommodate all the rows (except on mobile where the bottom of the table will be cut off).
BordersEdit
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 <syntaxhighlight lang=wikitext>
Left | Center | Right |
---|---|---|
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;"
to match the centering of the stars in their cells.
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
).
Border-collapse: conflicting or double bordersEdit
Wikitext <syntaxhighlight lang=wikitext>
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 <syntaxhighlight lang=wikitext>
A | B | C |
D | E | F |
Produces
A | B | C |
D | E | F |
Float table left or rightEdit
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 <syntaxhighlight lang=wikitext highlight=3,13> This paragraph is before the table.
Header A | Header B | Header C |
---|---|---|
row 1 A | row 1 B | row 1 C |
row 2 A | row 2 B | row 2 C |
Header A | Header B | Header C |
---|---|---|
row 1 A | row 1 B | row 1 C |
row 2 A | row 2 B | row 2 C |
... Lorem text after table </syntaxhighlight>
As it appears in a browser:
This paragraph is before the table.
Header A | Header B | Header C |
---|---|---|
row 1 A | row 1 B | row 1 C |
row 2 A | row 2 B | row 2 C |
Header A | Header B | Header C |
---|---|---|
row 1 A | row 1 B | row 1 C |
row 2 A | row 2 B | row 2 C |
Alternatively, you can use CSS to get the same result:
{| class="wikitable" style="float:left; clear:left; margin-right:8px;"
{| class="wikitable" style="float:right; clear:right; margin-left:8px;"
This paragraph is before the table.
Header A | Header B | Header C |
---|---|---|
row 1 A | row 1 B | row 1 C |
row 2 A | row 2 B | row 2 C |
Header A | Header B | Header C |
---|---|---|
row 1 A | row 1 B | row 1 C |
row 2 A | row 2 B | row 2 C |
Centering tablesEdit
{{#invoke:Shortcut|main}}
To center a table horizontally, use style Template:Kbd, which applies to the left and right margins. Text does not flow around centered tables as it does for floated tables; that is, no text appears to either side:
Wikitext <syntaxhighlight lang=wikitext highlight=2> Text before table...
Duis | aute | irure |
---|---|---|
dolor | in reprehenderit | in voluptate velit |
esse cillum dolore | eu fugiat nulla | pariatur. |
...text after table </syntaxhighlight>
As it appears in a browser:
Text before table...
Duis | aute | irure |
---|---|---|
dolor | in reprehenderit | in voluptate velit |
esse cillum dolore | eu fugiat nulla | pariatur. |
...text after table.
Style Template:Kbd may be combined with top and bottom margin in the standard way for CSS, e.g. <syntaxhighlight lang=wikitext inline>style="margin:1em auto"</syntaxhighlight> defines top and bottom margins of 1em, and automatic (centered) left and right margins.
Tip: For Android Chrome, use:
{| style="margin: auto; border: none;"
Template:Efn
Note: 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
.
Static ('sticky') headersEdit
Headers can be frozen so they're always at the top (or left) as one scrolls through a large table. See Template:Tl and Template:Tl.
Nested tablesEdit
{{#invoke:Hatnote|hatnote}}
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.
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
|
the original table again |
|
|
Wikitext: <syntaxhighlight lang=wikitext line highlight="4-8,11-17,19-24">
α | cell2
|
the original table again |
|
|
WidthEdit
Template:See also
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 or line breaks (<br>
) instead.
Vertical headersEdit
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 Template:Tl template. For example: <syntaxhighlight lang="wikitext">
</syntaxhighlight>
If the text includes an equals sign then replace it with {{=}}
.
Template:Vertical header | Template:Vertical header | Template:Vertical header | Template:Vertical header | Template:Vertical header | Template:Vertical header | Template:Vertical header | Template:Vertical header | Template:Vertical header | Template:Vertical header |
---|---|---|---|---|---|---|---|---|---|
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 Template:Tl.
Line breaks in headersEdit
An editor who is blind says line breaks <br>
are fine in this discussion here. If it is just two words in the column header, then a line break is simplest. Or if you want three words on separate lines in the header. But for longer header text, max-width is sometimes better, because when the viewport is narrow enough the browser may narrow the header to be less than the max-width setting. The browser often does this better than what line breaks allow in a narrow viewport.
Max-widthEdit
Narrowing columns with max-width style is fine; such as style="max-width:Xpx;"
or style="max-width:Xem;"
or other CSS units. The number by itself without units defaults to 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 strict widths (style="width:Xpx;"
or style="width:Xem;"
) 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, portrait view, landscape view, 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 sometimes 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. 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> Template:Static row numbersTemplate:Sticky headerTemplate:Table alignment
Location | Median wealth | Mean wealth
... </syntaxhighlight>Produces Template:Static row numbersTemplate:Sticky headerTemplate:Table alignment
{{sort under}} narrows the table columns further: Wikitext <syntaxhighlight lang=wikitext> Template:Static row numbersTemplate:Sticky headerTemplate:Sort underTemplate:Table alignment </syntaxhighlight>Produces Template:Static row numbersTemplate:Sticky headerTemplate:Sort underTemplate:Table alignment
If using {{sticky header}}, then for cell phones even narrower headers are better: Template:Static row numbersTemplate:Sticky headerTemplate:Table alignmentTemplate:Sort under
Here below is what the table looks like without Template:Static row numbersTemplate:Sticky headerTemplate:Table alignment
Other width methodsEdit{{#invoke:Hatnote|hatnote}}
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 Wikitext <syntaxhighlight lang=wikitext>
Produces
To set column widths in a table without headers, specify the width in the first cell for each column. Wikitext <syntaxhighlight lang=wikitext>
Produces
You can also use percentages, such as 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 <syntaxhighlight lang=wikitext>
Produces
NowrapEditA column will be as wide as its widest cell. To keep a cell from wrapping use If the cell is too wide though this may not be a good idea in cell phones. Part of the table may extend past the screen (especially in portrait view). Without Wikitext <syntaxhighlight lang="wikitext">
Produces
With Wikitext <syntaxhighlight lang=wikitext highlight=7,8>
Produces
Row operationsEditTable rows are added after the § table start indicator, and after any optional § whole table markup such as a caption, table summary, or § column scope attributes. Starting a new rowEditTo start a new table row, type a vertical bar and a hyphen on its own line: " <syntaxhighlight lang=wikitext class=gridbox> row code goes here</syntaxhighlight> Row styleEditRow style (height, width, borders, text alignment, background color, bolding, italics, etc.) may be added to a wikitable row by appending CSS style properties to the row start line after the Here is an example increasing the height of the middle row. The top row has italic text. The last row has bold text and a yellow background: Wikitext <syntaxhighlight lang=wikitext highlight=4,6,8>
Produces
Note that the row also contains an Row headersEditNote that with row headers you need to use a separate row in the wikitext for the row header cell. Here below is what a table 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. Wikitext <syntaxhighlight lang="wikitext" highlight=5,7>
Produces
Here is the table with a separate wikitext row for each row header cell. The data cells have plain unbolded text, and a lighter background. Wikitext <syntaxhighlight lang="wikitext" highlight=5,8>
Produces
Row numbersEditTable rows may be numbered with the assistance of templates provided for the purpose. See Help:Sortable tables sections on row numbers, and these templates: Link directly to a rowEditYou can link directly to a row in a wikicode table, by including an
and then link to it using
Row templateEditRegardless 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.:
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:
Example: Using Template:Tlx Wikitext <syntaxhighlight lang="wikitext" style="display: inline-block;">
Produces
Conditional table rowEdit{{#invoke:Labelled list hatnote|labelledList|Main article|Main articles|Main page|Main pages}} For a conditional row in a table, we can have: Wikitext <syntaxhighlight lang="wikitext" style="display: inline-block;"> {{ #if:1||-
Produces {{ #if:1||-
With comments to explain how it works, where note how the second row is missing: Wikitext <syntaxhighlight lang="wikitext" style="display: inline-block;"> {{ #if:1||-
Produces {{ #if:1||-
Cell operationsEditSetting cell parametersEditAt the start of a cell, add your parameter followed by a single pipe. For example, Wikitext <syntaxhighlight lang=wikitext>
Produces
Vertical alignment in cellsEditTemplate:See also
Note: The By default, text is aligned to the vertical middle of the cell (2nd column in table below). CSS can be used to vertically align individual cells, or single rows.
Relevant wikitext: <syntaxhighlight lang=wikitext highlight=1-3> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Top-aligned | Middle-aligned | Bottom-aligned
</syntaxhighlight>
The example below illustrates the need for top alignment of a row.
To align the text to the top of each cell in the row, apply the Wikitext <syntaxhighlight lang=wikitext highlight=2>
</syntaxhighlight> Produces
Documentation and more options for the CSS Cell content indenting and paddingEditThe contents of a cell can be indented or padded on any side. Also, the text can be aligned. In the third row the text is aligned to the right. See the following examples. Wikitext <syntaxhighlight lang="wikitext">
Produces
Pattern for arguments:
The arguments to This same order is also used elsewhere, such as when specifying § cell borders with Setting default cell padding Use If Wikitext <syntaxhighlight lang="wikitext">
Produces
Using Wikitext <syntaxhighlight lang="wikitext">
Produces
Using Wikitext <syntaxhighlight lang=wikitext>
Produces
Using Wikitext <syntaxhighlight lang=wikitext>
Produces
Individual cell bordersEditThe same § CSS used for tables can be used in a cell's format specifier (enclosed in Wikitext <syntaxhighlight lang=wikitext highlight=6,8,10>
Produces
Note that only the image cells have individual borders, not the text. The lower hex-colors (such as:
The
Wikitext <syntaxhighlight lang=wikitext>
Produces
To set the left, right, bottom, or top border of a single cell, one may use These arguments are ordered according to the § pattern described here.
For instance, Wikitext <syntaxhighlight lang=wikitext>
Produces
Note, however, that in the following table, none of the central cell's (i.e. Wikitext <syntaxhighlight lang=wikitext>
Produces
This happens because the code Wikitext <syntaxhighlight lang=wikitext>
Produces
To remove selected external borders of a wikitable, both remove them from the adjacent cells and begin the whole table with code like Note that replacing Table cell templatesEditTemplate:See also See Template:Tl for a large set of templates to configure text and color in cells in a standard way, producing stock output. For example: "Yes" (Template:Tlx), "No" (Template:Tlx), "—" (Template:Tlx), "N/A" (Template:Tlx), "?" (Template:Tlx), on colored backgrounds.
For example, see Comparison of text editors, which makes frequent use of table cell templates. TooltipsEditYou can add tooltips to columns by using the {{Tooltip}} template. Simply replace the column-title with WorkaroundsEditOverflowing tablesEdit{{#invoke:Hatnote|hatnote}} A horizontal scrollbar for the whole page shows up for any wikitable that is too wide for the screen. To create a scrollbar just for the table the following code may be used. Place the following code above a wikitable initialization: <syntaxhighlight lang="html" highlight="2" class=gridbox> </syntaxhighlight> Then, after the closing bracket for the wikitable, place the following code: <syntaxhighlight lang="html" highlight="2" class=gridbox> </syntaxhighlight> This will prevent the table from causing the entire page to scroll when scrolling through the table horizontally. Non-rectangular tablesEditTemplate:Tlx can be used to diagonally split a header cell, as in the top-left cell in the rendered result below:<ref group=note>A simplified version of Template:Table_of_phase_transitions</ref> Wikitext <syntaxhighlight lang="wikitext" highlight="2">
Produces
Cell borders can be hidden by adding Template:Xt to the style attributes of either the table or the cell,<ref group=note>Applying Wikitext <syntaxhighlight lang="wikitext" highlight="2" style="white-space: pre-wrap;">
Produces
Wikitext <syntaxhighlight lang="wikitext" style="white-space: pre-wrap;">
Produces
Rendering the pipeEditTemplate:Anchor
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 Rendering the first pipe when it is the third pipe in the cell code. Wikitext <syntaxhighlight lang=wikitext>
Produces
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) Template:Tag (preferred) or 2) html: Displaying adjacent pipes Wikitext <syntaxhighlight lang=wikitext>
Produces
Template Template:Tl2, because of the order in which things are parsed, is equivalent to typing in a single | pipe character. The single Common mechanisms that do not work in tables. Wikitext <syntaxhighlight lang=wikitext>
Produces
ClassesEditTemplate:See Also
There are several other CSS classes, besides the basic In the first line of table code, after the
Instead of remembering table parameters, you just include an appropriate class after the Wikitext <syntaxhighlight lang="wikitext">
Produces
becomes this: Wikitext <syntaxhighlight lang="wikitext">
Produces
simply by replacing inline CSS for the table by Wikitext <syntaxhighlight lang=wikitext>
Produces
Notice that the table retains the gray background of the Indenting tablesEditWhile tables should not normally be indented, when their surrounding paragraphs are also indented, you can indent tables using To achieve the same indentation as a colon, use Wikitext <syntaxhighlight lang="wikitext">
Produces
Tables on talk pagesEditNote that indentation applied to only the first line of the table wikitext (the line that begins with "
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 alsoEdit{{#invoke:Hatnote|hatnote}}
TemplatesEdit
Some commonly used table templates using TemplateStyles:
NotesEditExternal linksEdit
Wikimedia sister projectsEdit
Template:Introduction/navigation Template:Help navigation Template:Wikipedia technical help |