User:Mrjulesd/sandbox/3: Difference between revisions
How tables are formed |
attribute=value |
||
Line 138: | Line 138: | ||
<pre style="width:30%"> |
<pre style="width:30%"> |
||
| |
| Attribute="value" | row1cell1 |
||
| |
| Attribute="value" | row1cell2 |
||
| |
| Attribute="value" | row1cell3 |
||
</pre> |
</pre> |
||
Where <code> |
Where <code>Attribute="value"</code> indicates one or more HTML attribute, for example <code>style="background: yellow"</code>. |
||
If the cells are onthe same line, HTML attributes can be added like this: <code>| |
If the cells are onthe same line, HTML attributes can be added like this: <code>| Attribute="value" | row1cell1 || Attribute="value" | row1cell2 || Attribute="value" | row1cell3 </code> |
||
Adding to the ''header'' can be done like this: |
Adding to the ''header'' can be done like this: |
||
<pre style="width:30%"> |
<pre style="width:30%"> |
||
! |
! Attribute="value" | header1 |
||
! |
! Attribute="value" | header2 |
||
! |
! Attribute="value" | header3 |
||
</pre> |
</pre> |
||
Or like this: <code>! |
Or like this: <code>! Attribute="value" | header1 !! Attribute="value" | header2 !! Attribute="value" | header3 </code> |
||
Adding to the ''caption'' is done like this: <code> |+ |
Adding to the ''caption'' is done like this: <code> |+ Attribute="value" | Caption Title </code> |
||
For example, lets say we wanted to make some cell backgrounds yellow colored. The HTML attribute to do so is <code>style="background: yellow"</code>. It could be added to some cells in a table like this: |
For example, lets say we wanted to make some cell backgrounds yellow colored. The HTML attribute to do so is <code>style="background: yellow"</code>. It could be added to some cells in a table like this: |
||
Line 223: | Line 223: | ||
Tables and rows use the following markup: <code>{|</code>, <code>|-</code> and <code>|}</code>. HTML attributes can be added to <code>{|</code> and <code>|-</code>. They do not directly hold content. Because of this, they should ''not'' have an added pipe (<code>|</code>) after any HTML attributes. |
Tables and rows use the following markup: <code>{|</code>, <code>|-</code> and <code>|}</code>. HTML attributes can be added to <code>{|</code> and <code>|-</code>. They do not directly hold content. Because of this, they should ''not'' have an added pipe (<code>|</code>) after any HTML attributes. |
||
HTML attributes can be added to the table markup like this: <code>{| |
HTML attributes can be added to the table markup like this: <code>{| Attribute="value"</code>, where <code>Attribute="value"</code> indicates one or more attributes, e.g. <code>class="wikitable"</code>. |
||
HTML attributes can be added to the row markup like this: <code>|- |
HTML attributes can be added to the row markup like this: <code>|- Attribute="value"</code> |
||
For example, to turn a table's content yellow, the following text could be used: |
For example, to turn a table's content yellow, the following text could be used: |
Revision as of 16:44, 20 December 2015
Introduction
The markup for a basic table is:
{| class="wikitable" |+ Caption: example table |- ! header1 ! header2 ! header3 |- | row1cell1 | row1cell2 | row1cell3 |- | row2cell1 | row2cell2 | row2cell3 |}
This would produce:
header1 | header2 | header3 |
---|---|---|
row1cell1 | row1cell2 | row1cell3 |
row2cell1 | row2cell2 | row2cell3 |
{|
opens a table, and |}
closes it. class="wikitable"
is used with a lot of tables on Wikipedia, and adds some standard formatting, and follows on the same line as {|
.
|+ Caption: example table
adds the caption "Caption: example table" to the top of the table. The caption is optional.
Columns headers are created by ! column name
. Cells are created by | cell text
. The divider between rows is |-
. You can add an extra column by just sticking ! new column name
at the end of the last column name. To fill the cells in that column, you need to add another cell to each row: fill it by typing | cell text
. If you want to add an extra row, just add |-
to the table, and fill it with however many cells are appropriate.
Blank spaces at the beginning of a line are ignored. Thus |row1cell1
and | row1cell1
are identical.
To add a pipe (|
) character into cell contents, you need to use the <nowiki>
markup.
To recap, the markup is as follow. All this markup must start on a new line, except for double marks.
{| |
Table start: it is required. |
|+ |
Table caption: it is optional. Only between table start and first table row |
! |
Table header cell: it is optional. Cells may start on new lines, each beginning with its own single mark. Cell contents may also be put on the next line. |
!! |
Consecutive table header cell: it is optional. Cells may be added on same lines separated by double marks. |
|- |
Table row: it is optional on the first row, but otherwise required. The wiki engine assumes the first row. |
| |
Table data cell: it is required for data cells on new lines. Cells may start on new lines, each beginning with its own single mark. Cell contents may also be put on the next line. It is also used to separate HTML attributes from cell contents (both data or header), or caption contents. |
|| |
Consecutive table data cell: it is required for data cells on the same line. Cells may be added on same lines separated by double marks. |
|} |
Table end: it is required. |
Using double marks with tables
The above markup must start on a new line. This includes the cell markup (|
and !
). But the double cell markup (||
and !!
) can optionally be used for adding consecutive cells to a single line.
For example, this would produce the same table as above:
{| class="wikitable" |+ Caption: example table |- ! header1 !! header2 !! header3 |- | row1cell1 || row1cell2 || row1cell3 |- | row2cell1 || row2cell2 || row2cell3 |}
HTML attributes
HTML attributes are often needed to be added to tables for various reasons. HTML attributes take the basic form:
Attribute="value"
Multiple attributes can be applied by repeating this, for example Attribute1="value1" Attribute2="value2"
Commonly included attributes in tables:
class
attribute, for exampleclass="wikitable"
. The class attribute is normally applied to a complete table.style
attribute, for examplestyle="background: yellow"
. It is used for CSS (Cascading Style Sheets) styling, and can be applied to whole tables, table captions, table rows and individual cells.rowspan
attribute, for examplerowspan="2"
. Used for formatting cells so they extend beyond their normal extent of one row.colspan
attribute, for examplecolspan="2"
. Used for formatting cells so they extend beyond their normal extent of one column.border
attributes, for exampleborder="1"
. Used for applying a borders to whole tables, but not needed if usingclass="wikitable"
.
Other attributes are used with tables, for example scope
, but might not be supported by HTML5. See table at w3schools.com and td at w3schools.com for some examples.
HTML attribute summary
- All table markup, except table end (
|}
), optionally accepts one or more HTML attributes on the same line. - Cell markup (
|
,||
,!
,!!
) and caption markup (|+
) hold content. So separate any attributes from content with a single pipe (|
), even when cell content is on a new line, which is permissible. - Cell content may follow on the same line after its cell markup (which may include HTML attributes); or on lines below the cell markup. Cell content that uses its own markup may need to start on a new line; this can happen with things like lists, headings, or nested tables.
- Table and row markup (
{|
and|-
) do not directly hold content. Therefore, do not add a pipe (|
) after any HTML attributes.
Adding HTML attributes to cells and captions
This is how to cell contents, including header cell contents, and also captions.
Cells hold content, e.g. row1cell1
. The markup for cells and captions are: |
, ||
, !
, !!
and |+
. HTML attributes need to be kept separate from cell content or captions with a single pipe (|
). Cell content or captions may follow on the same line, or on a following line.
So HTML attributes can be inserted like this:
| Attribute="value" | row1cell1 | Attribute="value" | row1cell2 | Attribute="value" | row1cell3
Where Attribute="value"
indicates one or more HTML attribute, for example style="background: yellow"
.
If the cells are onthe same line, HTML attributes can be added like this: | Attribute="value" | row1cell1 || Attribute="value" | row1cell2 || Attribute="value" | row1cell3
Adding to the header can be done like this:
! Attribute="value" | header1 ! Attribute="value" | header2 ! Attribute="value" | header3
Or like this: ! Attribute="value" | header1 !! Attribute="value" | header2 !! Attribute="value" | header3
Adding to the caption is done like this: |+ Attribute="value" | Caption Title
For example, lets say we wanted to make some cell backgrounds yellow colored. The HTML attribute to do so is style="background: yellow"
. It could be added to some cells in a table like this:
{| class="wikitable" |+ Caption: some cells yellow. |- ! header1 ! header2 ! header3 |- | style="background: yellow" | row1cell1 | row1cell2 | style="background: yellow" | row1cell3 |- | row2cell1 | style="background: yellow" | row2cell2 | row2cell3 |}
Then it would produce this:
header1 | header2 | header3 |
---|---|---|
row1cell1 | row1cell2 | row1cell3 |
row2cell1 | row2cell2 | row2cell3 |
As you can see, a yellow background color has been added to some of the cells. Note that, optionally, cell contents could be added to new lines if desired.
Cell contents on new lines
The following markup applies a yellow background to three cells:
| style="background: yellow" | row1cell1 | style="background: yellow" | row1cell2 | style="background: yellow" | row1cell3
Cell contents can be added to new lines if so desired. So therefore the following markup would have a similar effect to the above:
| style="background: yellow" | row1cell1 | style="background: yellow" | row1cell2 | style="background: yellow" | row1cell3
Adding HTML attributes to whole tables and rows
Tables and rows use the following markup: {|
, |-
and |}
. HTML attributes can be added to {|
and |-
. They do not directly hold content. Because of this, they should not have an added pipe (|
) after any HTML attributes.
HTML attributes can be added to the table markup like this: {| Attribute="value"
, where Attribute="value"
indicates one or more attributes, e.g. class="wikitable"
.
HTML attributes can be added to the row markup like this: |- Attribute="value"
For example, to turn a table's content yellow, the following text could be used:
{| class="wikitable" style="background: yellow" |+ Caption: yellow contents. |- ! header1 ! header2 ! header3 |- | row1cell1 | row1cell2 | row1cell3 |- | row2cell1 | row2cell2 | row2cell3 |}
In this example, two HTML attributes (class and style) are applied to the table. The result would be:
header1 | header2 | header3 |
---|---|---|
row1cell1 | row1cell2 | row1cell3 |
row2cell1 | row2cell2 | row2cell3 |
How tables are formed
If you use the following wiki markup for a table:
{| class="wikitable" |+ Caption: example table |- ! header1 ! header2 ! header3 |- | row1cell1 | row1cell2 | row1cell3 |- | row2cell1 | row2cell2 | row2cell3 |}
The MediaWiki software translates it into the following HTML:
<table class="wikitable"> <caption>Caption: example table</caption> <tr> <th>header1</th> <th>header2</th> <th>header3</th> </tr> <tr> <td>row1cell1</td> <td>row1cell2</td> <td>row1cell3</td> </tr> <tr> <td>row2cell1</td> <td>row2cell2</td> <td>row2cell3</td> </tr> </table>
The table
tag opens and closes the table. The caption
tag adds a caption. The tr
tag opens and closes table rows. The th
tag adds a header cell. The td
tag adds a data cell.
Although you can add a table using HTML coding rather than wiki markup, it is discouraged due to readability reasons.
See also
For further help with tables, see: Help:Table; Editing Wikitext/Tables at Wikibooks; and Wikipedia:Advanced table formatting.