Help:Creating Tables 
From Singapore Hotels & Singapore Lifestyle
|
Contents |
Pipe syntax
Although HTML table syntax also works, special wikicode can be used to create a table, as follows:
- The entire table begins with a line "
{|optional table parameters " and ends with the line "|}". - An optional table caption is included with a line "
|+caption " after "{|". - The code for a table row consists of the line "
|-optional table parameters ", and, starting on a new line, the codes for the cells in the row, separated by newline or "|" - Table data are the codes for the cells; cell code is of the form "| value " or "| cell parameters | value "
- a row of column headings is identified by using "!" instead of "|", except for the separator between a cell parameter and a value; the difference with a normal row depends on the browser, column headings are often rendered in a bold font.
- 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.
The table parameters and cell parameters are the same as in HTML, see [1] and HTML element#Tables. However, the thead, tbody, tfoot, colgroup, and col elements are currently not supported in MediaWiki.
A table can be useful even if none of the cells have content, notably because with the use of background colors of cells the table can be a diagram, see e.g. 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élange example below). For empty cells, use the non-breaking space as content to ensure that the cells are displayed.
Examples
Simple example
{|
| Cell 1, row 1
| Cell 2, row 1
|-
| Cell 1, row 2
| Cell 2, row 2
|}
and
{|
| Cell 1, row 1 || Cell 2, row 1
|-
| Cell 1, row 2 || Cell 2, row 2
|}
both generate
| Cell 1, row 1 | Cell 2, row 1 |
| Cell 1, row 2 | Cell 2, row 2 |
Multiplication table
Source code
{| border="1" cellpadding="2"
|+Multiplication table
|-
! × !! 1 !! 2 !! 3
|-
! 1
| 1 || 2 || 3
|-
! 2
| 2 || 4 || 6
|-
! 3
| 3 || 6 || 9
|-
! 4
| 4 || 8 || 12
|-
! 5
| 5 || 10 || 15
|}
Appearance
Multiplication table × 1 2 3 1 1 2 3 2 2 4 6 3 3 6 9 4 4 8 12 5 5 10 15
Color; scope of parameters
Two ways of specifying color of text and background for a single cell are as follows:
{|
| bgcolor=red | <font color=white> abc
| def
| style="background:red; color:white" | ghi
| jkl
|}
gives
| abc | def | ghi | jkl |
As shown "font" works for one cell only, even without end tag: the end tag is produced by the system.
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:
{| style="background:yellow; color:green"
|-
| abc
| def
| ghi
|- style="background:red; color:white"
| jkl
| mno
| pqr
|-
| stu
| style="background:silver" | vwx
| yz
|}
gives
| abc | def | ghi |
| jkl | mno | pqr |
| stu | vwx | yz |
The HTML 4.01 specification defines sixteen named colors, here shown with hexadecimal values:
| black | #000000 | silver | #c0c0c0 | maroon | #800000 | red | #ff0000 | |||
| navy | #000080 | blue | #0000ff | purple | #800080 | fuchsia | #ff00ff | |||
| green | #008000 | lime | #00ff00 | olive | #808000 | yellow | #ffff00 | |||
| teal | #008080 | aqua | #00ffff | gray | #808080 | white | #ffffff |
See w:en:web colors.
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.
{| style="width:75%; height:200px" border="1"
|-
| abc
| def
| ghi
|- style="height:100px"
| jkl
| style="width:200px" |mno
| pqr
|-
| stu
| vwx
| yz
|}
gives
| abc | def | ghi |
| jkl | mno | pqr |
| stu | vwx | yz |
Positioning
One can position the table itself, and all contents in a row, and contents in a cell, but not with a single parameter 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é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.
Source code
{| border="1" cellpadding="5" cellspacing="0" align="center"
|+'''An example table'''
|-
! style="background:#efefef;" | First header
! colspan="2" style="background:#ffdead;" | Second header
|-
| upper left
|
| rowspan=2 style="border-bottom:3px solid grey;" valign="top" |
right side
|-
| style="border-bottom:3px solid grey;" | lower left
| style="border-bottom:3px solid grey;" | lower middle
|-
| colspan="3" align="center" |
{| border="0"
|+''A table in a table''
|-
| align="center" width="150px" | [[Image:wiki.png]]
| align="center" width="150px" | [[Image:wiki.png]]
|-
| align="center" colspan="2" style="border-top:1px solid red; border-right:1px
solid red; border-bottom:2px solid red; border-left:1px solid red;" |
Two Wikipedia logos
|}
|}
Appearance
An example table First header Second header upper left right side
lower left lower middle
A table in a table Image:Wiki.png Image:Wiki.png Two Wikipedia logos
Advanced example
{| align=right border=1
| Col 1, row 1
|rowspan=2| Col 2, row 1 (and 2)
| Col 3, row 1
|-
| Col 1, row 2
| Col 3, row 2
|}
| Col 1, row 1 | Col 2, row 1 (and 2) | Col 3, row 1 |
| Col 1, row 2 | Col 3, row 2 |
Note the floating table to the right.
Nested table
{| border=1
| α
| align="center" | cell2
{| border=2 style="background-color:#ABCDEF;"
| NESTED
|-
| TABLE
|}
| valign="bottom" | the original table again
|}
gives a nested table
| α | cell2
| the original table again |
Nested tables have to start on a new line.
Combined use of COLSPAN and ROWSPAN
{| border="1" cellpadding="5" cellspacing="0"
|-
! Column 1 || Column 2 || Column 3
|-
| rowspan=2| A
| colspan=2 align="center"| B
|-
| C
| D
|-
| E
| colspan=2 align="center"| F
|-
| rowspan=3| G
| H
| I
|-
| J
| K
|-
| colspan=2 align="center"| L
|}
| Column 1 | Column 2 | Column 3 |
|---|---|---|
| A | B | |
| C | D | |
| E | F | |
| G | H | I |
| J | K | |
| L | ||

![Back to previous page [backspace]](/skins/monobook/back.jpg)



