User:Hadseys/Formatting/Tables

This page gives information about syntax to build wiki-tables in MediaWiki.

Using the Toolbar


You can use the Mediawiki edit toolbar to create tables.The toolbar is helpful to generate the necessary codings.

Use the first button on the right of the toolbar to insert a table when editing a page.

By default, it includes the following text:

''' {| class="wikitable"

''' |-

''' ! header 1

''' ! header 2

''' ! header 3

''' |-

''' | row 1, cell 1

''' | row 1, cell 2

''' | row 1, cell 3

''' |-

''' | row 2, cell 1

''' | row 2, cell 2

''' | row 2, cell 3

 |} 

See : header, row, cell.

Pipe syntax tutorial
Although HTML table syntax also works, special wikicode can be used as a shortcut to create a table. The pipe (vertical bar) codes function exactly the same as HTML table markup, so a knowledge of HTML table code will help in understanding pipe code. The shortcuts are as follows: which is probably not what you want: However, the format modifier is useful:
 * 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:
 * An optional table caption is included with a line starting with a vertical bar and plus sign " " and the caption after it:
 * 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 will start on the next line.
 * Type the codes for each table cell in the next row, starting with a bar:
 * Cells can be separated with either a new line and new bar, or by a double bar "||" on the same line. Both produce the same output:
 * If you use single bars, then what you think the first cell is in fact a format modifier applied to the cell, and the rest of your "cells" are merged into one:

Just remember: no more than 2 single pipes on a line!

The final table would display like this:  The table parameters and cell parameters are the same as in HTML, see  and Table (HTML). However, the,  ,  ,  , and   elements are currently not supported in MediaWiki.
 * a row of column headings is identified by using "!" instead of "|", and using "!!" instead of "||". Header cells typically render differently than regular cells, depending on the browser.  They are often rendered in a bold font and centered.
 * the first cell of a row is identified as row heading by starting the line with "!" instead of "|", and starting subsequent data cells on a new line.
 * Optional parameters can modify the behavior of cells, rows, or the entire table. For instance, a border could be added to the table:

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 m:Template talk:Square 8x8 pentomino example. An "image" in the form of a table is much more convenient to edit than an uploaded image.

Each row must have the same number of cells as the other rows, so that the number of columns in the table remains consistent (unless there are cells which span several columns or rows, see colspan and rowspan in M&#233;lange example below). For empty cells, use the non-breaking space  as content to ensure that the cells are displayed. To show a visible pipe in a cell, use &lt;nowiki>|&lt;/nowiki> or &amp;#124;.

Vertical text
 .vert{ left:1px; top:5px; width:1px; writing-mode: tb-rl; }

After this "vert" style is implemented also vertical text can be used. Works IE7 not in Opera. http://www.w3.org/TR/2001/WD-css3-text-20010517/#PrimaryTextAdvanceDirection Exsample: this will be displayed vertically now

Vertical test http://tlt.its.psu.edu/suggestions/international/web/tips/verticaltest.html

Simple example
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.

Wiki markup 



What it looks like in your browser 

Multiplication table
Wiki markup 

What it looks like in your browser (see: Help:User_style) 

Color; scope of parameters
Two ways of specifying color of text and background for a single cell are as follows. The first form is preferred:

Wiki markup 

What it looks like in your browser 

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:

Wiki markup 

What it looks like in your browser 

To make the table blend in with the background, use. (Warning:, does not work with some browsers, including IE6!)

Width, height
The width and height of the whole table can be specified, as well as the height of a row. To specify the width of a column one can specify the width of an arbitrary cell in it. If the width is not specified for all columns, and/or the height is not specified for all rows, then there is some ambiguity, and the result depends on the browser.

Wiki markup 

What it looks like in your browser <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

Note that inline CSS  has no effect with some browsers. If compatibility is important, equivalent older constructs like  should work on more browsers.

Setting your column widths
If you wish to force column widths to your own requirements, rather than accepting the width of the widest text element in a column's cells, then follow this example. Note that wrap-around of text is forced (as long as none of the words exceed the width of the column, otherwise the column will be stretched to accommodate the longest word).

<blockquote style="background: white; border: 1px solid black; padding: 1em;">

<blockquote style="background: white; border: 1px solid black; padding: 1em;">

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

<blockquote style="background: white; border: 1px solid black; padding: 1em;">

<blockquote style="background: white; border: 1px solid black; padding: 1em;">

Vertical alignment
By default data in tables is vertically centrally aligned, which results in odd-looking layouts like this:

<blockquote style="background: white; border: 1px solid black; padding: 1em;">

To fix this, apply the valign="top" attribute to the rows (unfortunately it seems to be necessary to apply this individually to every single row). For example:

<blockquote style="background: white; border: 1px solid black; padding: 1em;">

<blockquote style="background: white; border: 1px solid black; padding: 1em;">

Positioning
One can position the table itself, and all contents in a row, and contents in a cell, but not with a single parameter for all contents in the table, see m:Template talk:Table demo. Do not, under any circumstances, use "float" to position a table. It will break page rendering at large font sizes.

M&eacute;lange
Here's a more advanced example, showing some more options available for making up tables. You can play with these settings in your own table to see what effect they have. Not all of these techniques may be appropriate in all cases; just because you can add colored backgrounds, for example, doesn't mean it's always a good idea. Try to keep the markup in your tables relatively simple -- remember, other people are going to be editing the article too! This example should give you an idea of what is possible, though.

Wiki markup <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

What it looks like in your browser <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

Floating table
Wiki markup <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;"> This paragraph is before the table. 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...

Note the floating table to the right.

This paragraph is after the table. 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...

What it looks like in your browser <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

This paragraph is before the table. 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...

Note the floating table to the right.

This paragraph is after the table. 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...

Nested tables
This shows one table (in blue) nested inside another table's cell2. Nested tables have to start on a new line.

Wiki markup <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;"><p style="border: 1px dashed #2f6fab; background-color: #f9f9f9;"><tt> </tt>

What it looks like in your browser <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

Combined use of COLSPAN and ROWSPAN
Wiki markup <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

What it looks like in your browser <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

Note that using  for cell G combined with   for cell F to get another row below G and F won't work, because all (implicit) cells would be empty. Likewise complete columns are not displayed if all their cells are empty. Borders between non-empty and empty cells might be also not displayed (depending on the browser), use  to fill an empty cell with dummy content.

Centering tables
Centered tables can be achieved, but they will not "float"; that is to say, no text will appear to either side. The trick is <tt> {| style="margin: 1em auto 1em auto" </tt>

Wiki markup <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

What it looks like in your browser <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

Setting parameters
At the start of a cell, add your parameter followed by a single pipe. For example <tt>width="300"|</tt> will set that cell to a width of 300 pixels. To set more than one parameter, leave a space between each one.

Wiki markup <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

What it looks like in your browser <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

Decimal point alignment
A method to get columns of numbers aligned at the decimal point is as follows:

Wiki markup <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

What it looks like in your browser <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

If the column of numbers appears in a table with cell padding or cell spacing, one can still align the decimal points without an unsightly gap in the middle. Embed a table in each number's cell and specify its column widths. Make the embedded tables' column widths the same for each cell in the column. (If decimal points are still misaligned using this method, the main table's column may be too narrow. Add a parameter to increase the column's width.)

Wiki markup <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

What it looks like in your browser <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;"> {|border="1" cellpadding="4" cellspacing="2"






 * }

In simple cases one can dispense with the table feature and simply start the lines with a space, and put spaces to position the numbers:

432.1 43.21   4.321

Style classes
Some users have created CSS classes and templates to make table styles easier. Instead of remembering table parameters, you just include an appropriate style 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:

simply by replacing inline CSS for the table by. 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:

Wiki markup <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

What it looks like in your browser <blockquote style="background: white; border: 1px solid rgb(153, 153, 153); padding: 1em;">

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.

Of course this works only for browsers supporting inline CSS, if it's important use XHTML markup like  instead of "font-size:120%", or Wiki markup like   instead of "font-style:italic".

Sorting
For a sortable table (wikitable sortable) see Help:Sorting.

Table row depending on a template parameter
Wiki-syntax for a table row can be made optional using ParserFunctions. To avoid confusion between pipe characters as used in ParserFunctions, and those which are part of the table syntax, the latter are put with a special, see.

Other table syntax
Other types of table syntax that MediaWiki supports:
 * 1) XHTML
 * 2) HTML and wiki  syntax (Do not use)

All three are supported by MediaWiki and create (currently) valid HTML output, but the pipe syntax is the simplest, especially for people who are already familiar with HTML. Also, HTML and wiki  syntax will not necessarily remain browser-supported in the upcoming future, especially on handheld internet-accessible devices.

See also Table (HTML), HTML element#Tables. Note however that the,  ,  ,  , and   elements are currently not supported in MediaWiki.

Comparison of table syntax
Sample table

Pros
 * Can be previewed/debugged with any XHTML editor
 * Can be formatted for easier reading
 * Well-known


 * Can be previewed/debugged with any HTML editor
 * Can be formatted for easier reading
 * Well-known
 * Takes less space than XHTML
 * Easy to write
 * Easy to read
 * Takes little space

Cons
 * Tedious
 * Takes a lot of space
 * Difficult to read quickly


 * Should not be used
 * Confusing, especially for people with little HTML experience
 * Poorly formed
 * Poorly delimited
 * Generally odd looking
 * May not have browser support in future


 * Unfamiliar syntax
 * Rigid structure
 * Cannot be indented
 * Text (as in HTML tags) may be easier for some people to read than series of pipes, plus signs, exclamation marks, etc.
 * Is nothing more than a shortcut for HTML-style tags. Not easily understood by those unfamiliar with HTML table concepts

XHTML HTML &amp; Wiki-td Wiki-pipe

Pipe syntax in terms of the HTML produced
The pipe syntax, developed by Magnus Manske, substitutes pipes ( | ) for HTML. There is an on-line 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
A table is defined by which equals and, or the first parameter gets ignored.

Rows
&lt;tr&gt; tags will be generated automatically for the first row. To start a new row, use |- which results in &lt;tr&gt; Parameters can be added like this: |- params which results in &lt;tr params&gt;

Note:
 * &lt;tr&gt; tags will be automatically opened at the first <td&gt; equivalent
 * &lt;tr&gt; tags will be automatically closed at <tr&gt; and </table&gt; equivalents

Cells
Cells are generated either like this: |cell1 |cell2 |cell3 or like this: |cell1||cell2||cell3 which both equal &lt;td&gt;cell1&lt;/td&gt;&lt;td&gt;cell2&lt;/td&gt;&lt;td&gt;cell3&lt;/td&gt; so "||" equals "newline" + "|"

Parameters in cells can be used like this: |params|cell1||params|cell2||params|cell3 which will result in &lt;td params&gt;cell1&lt;/td&gt; &lt;td params&gt;cell2&lt;/td&gt; &lt;td params&gt;cell3&lt;/td&gt;

Headers
Functions the same way as TD, except "!" is used instead of the opening "|". "!!" can be used instead of "||". Parameters still use "|", though! Example: ! params|cell1

Captions
A &lt;caption&gt; tag is created by |+ Caption which generates &lt;caption&gt;Caption&lt;/caption&gt; You can also use parameters: |+ params|Caption which will generate &lt;caption params&gt;Caption&lt;/caption&gt;

Displaying the table code which generates a table
A simple wiki markup table's code inside a Code box can be seen below.

Above code produces/displays below table:

Below code, generated and displayed the above table's Code box code itself, on the screen and web page, inside a blue colored dashed bordered rectangular box.

&#60;pre>

&#60;/pre>

Note that, HTML tag <tt>&#60;pre></tt> was used to achieve displaying the above code and the Code box.

Other alternatives to display table code
In most cases, when a code line is longer than the web browser window's width, then a scrolling bar appears at bottom, to let the viewer slide to the right side (and also left side) to see the rest of the code, because, the use of <tt>&#60;pre></tt> tag causes code lines to remain intact, unless an EOL (CR/LF) hidden character is reached in that text line. But having to slide or scroll to the right or left to view the full code line is often not comfortable to many users. To solve such problem, using the <tt>&#60;p></tt>, <tt>&#60;tt></tt> and <tt>&#60;br &#47;></tt> HTML tags, are better than using the <tt>&#60;pre></tt> tag, as those will not result in the need to move the scroll-bar to the right (or left) side for viewing, by causing code lines to wrap around so that they don't exceed the length allowed by the web browser window's width. By placing the code inside the <tt>&#60;tt>...&#60;/tt></tt> HTML tags, the code is displayed with a fixed width text/font, (like the <tt>&#60;pre></tt> tag uses) for easier reading. HTML tag <tt>&#60;br &#47;></tt> is used to display (or bring) next line of code, starting from the next line. HTML tag <tt>&#60;p></tt> along with its CSS style properties, is used to create the blue colored dashed bordered rectangular box (Code box) around the codes, (like the HTML <tt>&#60;pre></tt> tag, which gets these properties from the main.css stylesheet file).

An example of table code with a long line is:

<p style="padding: 1em; border: 1px dashed #2f6fab; color: Black; background-color: #f9f9f9; line-height: 1.1em;"> <tt> &#123;&#124; border="5" cellspacing="5" cellpadding="2"

&#124; style="text-align: center;" &#124; &#91;&#91;Image:gnome-system.png]]

&#124;-

&#33; Computer

&#124;-

&#124; style="color: yellow; background-color: green;" &#124; Processor Speed: &#60;span style="color: white;"> 1.8 GHz &#60;/span>'''

&#124;&#125; </tt>

producing the below table:

The Code box above the table has the auto line wrapping feature enabled. Note the long line of code (the sixth line from top), which is wrapped inside the Code box. This Code box and the code inside it, can be displayed by using the below code in the edit box.

<p style="padding: 1em; border: 1px dashed #2f6fab; color: Black; background-color: #f9f9f9; line-height: 1.1em;"> <tt> &#60;p style="padding: 1em; border: 1px dashed #2f6fab; color: Black; background-color: #f9f9f9; line-height: 1.1em;"> &#60;tt>

&#60;nowiki> {| &#60;/nowiki> border="5" cellspacing="5" cellpadding="2" &#60;br &#47;>


 * style="text-align: center;" | &#60;nowiki> '''& &#60;br &#47;>


 * - &#60;br &#47;>

! Computer &#60;br &#47;>


 * - &#60;br &#47;>

| style="color: yellow; background-color: green;" | Processor Speed: &#60;nowiki> &#60;/nowiki> 1.8 GHz &#60;nowiki>   &#60;/nowiki>  &#60;br &#47;>

&#60;nowiki> |} &#60;/nowiki> &#60;br &#47;>

&#60;/tt> &#60;/p> </tt>

See the above codes, note that, <tt>&#60;nowiki></tt>...<tt>&#60;/nowiki></tt> tags were used to disable wiki markup codes for beginning a table (&#123;&#124;), ending a table (&#124;&#125;), start of an image displaying (&#91;&#91;), or a hyperlink, etc. All wiki &amp; HTML markup codes need to be disabled by enclosing them inside the <tt>&#60;nowiki></tt>...<tt>&#60;/nowiki></tt> tags. If these codes were to be displayed inside another table, then, each &#124; (pipe) &amp; ! (Exclamation mark) symbol also needed to be enclosed inside the <tt>&#60;nowiki></tt> tags. Note that, the longer line is automatically wrapped according to the width of the web browser's window, inside the Code box.

Alternatively, we can replace each &#124; (pipe symbol) character with <tt>&amp;#124;</tt> (HTML decimal entity code), replace each &#33; (exclamation mark) with <tt>&amp;#33;</tt> code, replace &#123; (beginning curly/second bracket) with <tt>&amp;#123;</tt> and we may replace &#125; (closing curly/second bracket) with <tt>&amp;#125;</tt> code. Also replace the < (less than sign, or beginning angle bracket) with <tt>&amp;#60;</tt> numeric entity code or, replace it with <tt>&amp;lt;</tt> (HTML symbol entity code). For more on HTML decimal or hexadecimal numeric entity codes, please see Windows Alt codes. To display the wiki image markup code, we should replace the [ (beginning square/third bracket) with <tt>&amp;#91;</tt> and we may replace &#93; (closing square/third bracket) with <tt>&amp;#93;</tt>. When we are replacing characters with their numeric enitity codes, we are actually disabling their normal functionality, so we can display them on the web page(s).

{| border="0" width="100%"
 * By using the numeric entity codes, mentioned in above paragraph, below codes can display the above Code box in another alternative (better) way. Note that, the longer line is automatically wrapped, without exceeding the browser window's width, inside the below Code box.
 * width="33%" |
 * width="33%" |


 * }

<p style="padding: 1em; border: 1px dashed #2f6fab; color: Black; background-color: #f9f9f9; line-height: 1.1em;"> <tt> &#60;p style="padding: 1em; border: 1px dashed #2f6fab; color: Black; background-color: #f9f9f9; line-height: 1.1em;"> &#60;tt>

&amp;#123;&amp;#124; border="5" cellspacing="5" cellpadding="2" &#60;br &#47;>

&amp;#124; style="text-align: center;" &amp;#124; &amp;#91;&amp;#91;Image:gnome-system.png]] &#60;br &#47;>

&amp;#124;- &#60;br &#47;>

&amp;#33; Computer &#60;br &#47;>

&amp;#124;- &#60;br &#47;>

&amp;#124; style="color: yellow; background-color: green;" &amp;#124; Processor Speed: &amp;#60;span style="color: red;"> 1.8 GHz &amp;#60;/span> &#60;br &#47;>

&amp;#124;&amp;#125; &#60;br &#47;>

&#60;/tt> &#60;/p> </tt>

See also disabling wikitext interpretation and/or reformatting.