[go: up one dir, main page]

Help:Advanced table formatting

There are several advanced table formatting techniques to improve the display or editing of wikitables in Wikipedia. Most of the tips involve use of standard text-editors. While some special software packages exist, to allow customized editing, they are typically not available when travelling to other computers for wiki-editing.

Some techniques here are beyond the basics described in the Wikipedia help-page "Help:Table" which explains almost all basic options of table formatting, also showing examples of each.

A quick multi-column table with spacing

edit

The quickest way to create multiple columns of text is as follows:

AA BB CC
DD EE FF
{| cellpadding=5
|AA || BB || CC
|-
|DD || EE || FF
|}

However, readers often prefer a vertical listing of data, down each column.
A border can be added with style="border:1px solid #BBB":

AA CC EE
BB DD FF
{| cellpadding=5 style="border:1px solid #BBB"
|AA || CC || EE
|-
|BB || DD || FF
|}

Note that the default background color is lighter, so a shade of light-bluegreen can be specified on each row, using hex-color code "#fafeff" (or "#FAFEFF"), although the color might not matter in large lists:

AA CC EE
BB DD FF
{| cellpadding=5 style="border:1px solid #BBB"
|- bgcolor="#fafeff"
|AA || CC || EE
|- bgcolor="#fafeff"
|BB || DD || FF
|}

Compressing to one-line row format

edit

The wikitext for any one row can be compressed onto a single line by joining columns with double-bars "||" between them and ending each row with "<tr>".

::Example row 1: |''fmtspec''|AA||''fmtspec''|CC||''fmtspec''|EE<tr>
::Example row 2: |''fmtspec''|BB||''fmtspec''|DD||''fmtspec''|FF<tr>
(The fmtspec can be bgcolor=beige or style="font-size:91%" etc.)

This is done sometimes because it can be easier to re-arrange or preview rows that are coded as separate lines of text. Multiple columns are joined by double-bars "||" between them, while each single-bar "|" along a line allows a format-specifier before each data-item which ends at a double-bar. However, the wikitable code for a new row, bar-dash ("|-"), must be omitted by adding "<tr>" at the end of the prior row above it. The final row does not need an ending "<tr>".

Floating images in the center

edit

A table can be used to wrap an image, so that the table can float towards the center of the page (such as using: style="float: right;"). However, the table margins, border and font-size must be precisely set to match a typical image display. The Image-spec parameter "thumb|" (although auto-thumbnailing to user-preference width) forces a wide left-margin that squeezes the nearby text, so the parameter "center|" can be added to suppress the left-margin padding. However, "center" sometimes shoves the caption to a 2nd line (under a centered box "[]"), so "thumb|" could be omitted and just hard-code the image size, adding a gray (#BBB) border. Using precise parameters to match other images, a floating-image table can be coded as follows:

{| style="float:right; border:1px solid #BBB;margin:.46em 0 0 .2em"
|- style="font-size:86%"
|  style="vertical-align:top" |[[File:DuraEuropos-TempleOfBel.jpg|180px]]<!--
  --><br /> Temple of [[Bel (mythology)|Bel]] (floating).
|}

The text inside the floating-table is sized by style="font-size:86%" (similar in size to the regular image captions). That floating-image table floats a typical image-box, but also allows adjusting the left-hand margin of the image (see temple-example floating below).

Infobox AThis sample infobox shows how the floating image-box aligns toward the center.
 
 Temple of Bel  (floating).
 
This box shows implicit margins of [[File:... |right]]
 
This box shows implicit margins of [[File:... |left]]

The caption-text could be omitted, or just remove the parameter "thumb|" so the caption will be hidden until shown by "mouse-over display". Unfortunately the parameter "thumb|" (used for displaying the caption) also controls the auto-thumbnailing to re-size images by user-preferences size (default thumbnail size was 180px, then after 2009, became 220px). In March 2010, it was not possible to have auto-thumbnail sizing while also concealing the caption: instead, parameter "thumb|" triggers both actions and forces the caption to display underneath the image.

An image set with parameter "left|" will gain a wide right-side margin (opposite margin of parameter "right|"), so floating toward the left would require an image set as "center|" inside a table with style="float:left; margin:0.46em 0.2em".

Recall that, outside an image-table, the parameter "right|" causes an image to align (either) above or below an infobox, but would not float alongside the infobox. For that reason, many images beside an infobox are typically set as "left|" to align along the left-margin, rather than floated into the center of the page.

Note the order of precedence from the right margin: first, come infoboxes or images using "right|", then come the floating-tables, and lastly, any text will wrap that can still fit. If the first text-word is too long, no text will fit to complete the left-hand side, so beware creating a "ragged left margin" when not enough space remains for text to fit alongside floating-tables.

If multiple single image-tables are stacked, they will float to align across the page, depending on page-width. The text will be squeezed to allow as many floating-tables as can fit, as auto-aligned, then wrap whatever text (can still fit) at the left-hand side.

 
...by float:right
 
...images wrap...
 
All these...

That auto-aligning feature can be used to create a "floating-gallery" of images: a set of 20 floating-tables will wrap (backward, right-to-left) as if each table were a word of text to wrap across and down the page. To wrap in the typical direction (wrapping left-to-right) define all those floating-tables, instead, as left-side tables using the top parameter style="float:left; margin:0.46em 0.2em". Multiple floating-images empower more flexible typesetting of images around the text.

Shifting/centering

edit

Images within a wikitable can be shifted by inserting non-breaking spaces (&nbsp;) before or after the image code (e.g., &nbsp;[[File:...]]). However, auto-centering simply requires use of the |center parameter (see WP:Extended image syntax#Location).

In the example below, note how Col2 uses |center, but Col3 uses &nbsp;:

 {| class="wikitable"
 |-
 |<!--Col1-->[[File:Domtoren vanaf Brigittenstraat.jpg|299x125px]]
 |<!--Col2-->[[File:Utrecht 003.jpg|299x125px|center]]
 |<!--Col3-->&nbsp;[[File:Uitzicht--Domtoren.jpg|299x125px]]&nbsp;
 |-
 |<!--Col1-->Dom tower from Brigittenstraat
 |<!--Col2-->Cloister garth of the Utrecht Dom Church
 |<!--Col3-->&nbsp;&nbsp;<small>View from bell tower</small>
 |}

The above coding generates the table below: note the middle garden image is centered (but not the left image), and the right image has 2 spaces before "View...", to give an approximation of centering:

 
 
   
Dom tower from Brigittenstraat Cloister garth of the Utrecht Dom Church   View from bell tower

Also note that the tag <small>...</small> made a smaller-text-size caption. Fonts also can be sized by percent (style="font-size: 87%;"), where the exact percent-size as displayed depends on the various sizes allowed for a particular font; the browser will approximate to the nearest possible size.

 | style="font-size: 87%;" | View from bell tower

The column attribute, above, uses style= to set the font size for the caption, following the second pipe symbol.

A font-size: 65%; is excessively small, while font-size: 87%; is a mid-size font, slightly larger than that produced by the tag <small>.

More complex examples

edit

Mélange

edit

Note: This example is not accessible, and should be avoided as much as possible. For example, nested tables (tables inside tables) should be separated into distinct tables when possible.

Here is a more advanced example, showing some more options available for making up tables.

Users can play with these settings in their own table to see what effect they have. Not all of these techniques may be appropriate in all cases; just because colored backgrounds can be added, for example, does not mean it is always a good idea. Try to keep the markup in tables relatively simple—remember, other people are going to be editing the article too! This example should give an idea of what is possible, though.

Wikitext:

Text before centered table...
{| style="border: 1px solid black; border-spacing: 0; margin: 1em auto;"
|+ '''An example table'''
|-
! style="border: 1px solid black; padding: 5px; background: #efefef;" | First header
! colspan="2" style="border: 1px solid black; padding: 5px; background: #ffdead;" | Second header
|-
| style="border: 1px solid black; padding: 5px;" | Upper left
| style="border: 1px solid black; padding: 5px;" | Upper middle
| rowspan="2" style="border: 1px solid black; border-bottom: 3px solid grey; padding: 5px; vertical-align: top;" |
Right side
|-
| style="border: 1px solid black; border-bottom: 3px solid grey; padding: 5px;" | Lower left
| style="border: 1px solid black; border-bottom: 3px solid grey; padding: 5px;" | Lower middle
|-
| colspan="3" style="border: 1px solid black; text-align: center;" |
Text before a nested table...
{|
|+ ''A table in a table''
|-
| style="text-align: center; width: 150px;" | [[File:Wiki.png]]
| style="text-align: center; width: 150px;" | [[File:Wiki.png]]
|-
| colspan="2" style="text-align:center; border-top: 1px solid red;<!--
  --> border-right: 1px solid red; border-bottom: 2px solid red;<!--
  --> border-left: 1px solid red;" |
Two Wikipedia logos
|}
...text after a nested table
|}
...text after centered table

As it appears in a browser:

Text before centered table...

An example table
First header Second header
Upper left Upper middle

Right side

Lower left Lower middle

Text before a nested table...

A table in a table
   

Two Wikipedia logos

...text after a nested table

...text after centered table

Center-floating images

edit

A table can be used to wrap an image, so that the table can float towards the center of the page (such as using: style="float: right;"). However, the table margins, border and font-size must be precisely set to match a typical image display. The File-spec parameter |thumb (although auto-thumbnailing to user-preference width) forces a wide left-margin that squeezes the nearby text, so the parameter |center can be added to suppress the left-margin padding. However, |center sometimes shoves the caption to a 2nd line (under a centered box "[]"), so |thumb could be omitted and just hard-code the image size, adding a gray (#BBB) border. Using precise parameters to match other images, a floating-image table can be coded as follows:

{| style="float: right; border: 1px solid #BBB; margin: .46em 0 0 .2em;"
|- style="font-size: 86%;"
| style="vertical-align: top;" |[[File:DuraEuropos-TempleOfBel.jpg|180px]]<!--
  --><br> Temple of [[Bel (mythology)|Bel]] (floating)
|}

The text inside the floating table is sized by style="font-size: 86%;". That floating-image table floats a typical image box, but allows adjusting the left-hand margin of the image (see temple-example floating below).

This sample infobox shows how the floating image box aligns toward the center.
 
 Temple of Bel (floating)
 
The dashed border around this table shows the implicit margins of Images using "thumb|right".

The caption text can be omitted, or remove the parameter "thumb|" so the caption is hidden until "mouse-over display". Unfortunately the parameter |thumb (used for displaying the caption) also controls the auto-thumbnailing to re-size images by user-preferences size. To have auto-thumbnail sizing while also concealing the caption, use |frameless|right instead of |thumb.

An image set with parameter |left has a wide right-side margin (opposite margin of parameter |right), so floating toward the left can be achieved with an image set as |center inside a table with style="float:left; margin:0.46em 0.2em;".

Recall that, outside an image-table, the parameter |right causes an image to align (either) above or below an infobox, but would not float alongside the infobox.

Note the order of precedence: first come infoboxes or images using |right, then come the floating tables, and lastly, any text wraps that can still fit. If the first word of the text is too long, no text will fit to complete the left-hand side, so beware creating a "ragged left margin" when not enough space remains for text to fit alongside floating tables.

If multiple single-image tables are stacked, they float to align across the page, depending on page width. The text squeezes to allow as many floating tables as can fit, as auto-aligned, then wrap whatever text can still fit at the left-hand side.

 
...by float: right
 
...images wrap...
 
All these...

That auto-aligning feature can be used to create a "floating gallery" of images: a set of 20 floating tables wrap (backward, right-to-left), as if each table were a word of text to wrap across and down the page. To wrap in the typical direction (wrapping left-to-right) define all those floating tables, instead, as left-side tables using the top parameter style="float:left; margin:0.46em 0.2em;". Multiple floating images empower more flexible typesetting of images around the text.

Undenting/bracketing of text

edit

Real typesetting software, for over 30 years, typically has had simple directives to trigger alignments as left, right, center, or undented (beyond the left-margin line). However, for decades, HTML has had only limited options for easy alignment (one: <center>, which is now deprecated). A method for undenting the first word of a paragraph is to put the paragraph into a text-table, where the first word (or syllable) is (alone) in column 1, while the other text is in column 2.

Wikicode
<table cellspacing=0 cellpadding=0><tr><td valign=top>Bee<td>thoven
                composed the [[Moonlight Sonata]]<br />while he was losing his hearing.</table>
Results
Beethoven composed the Moonlight Sonata
while he was losing his hearing.

Note the use of both "cellspacing=0 cellpadding=0" so as to not separate the spacing between the first syllable "Bee" and "thoven".

A third column can be used to enclose text in outside brackets, then putting the closing-bracket "]" in column 3, as follows:

Wikicode
<table><tr><td valign=top>[<td>This is line 1.<br />Line 2.<td>]</table>
Results
[This is line 1.
Line 2.
]

Tiny tables within a text line

edit

For years in HTML, a table has always forced an implicit line-wrap (or line-break). So, to keep a table within a line, the workaround is to put the whole line into a table, then embed a table within a table, using the outer table to force the whole line to stay together. Consider the following examples:

Wikicode (showing table forces line-break)
* This is a test table here <table style="border:1px solid black"><tr><td>HELLO WORLD</table> followed by this text afterward.
Result
  • This is a test table here
    HELLO WORLD
    followed by this text afterward.
Wikicode (table-within-table)
* <table style="background-color:#fafeff"><tr><td>This is a test table here<td><table height=11px><tr><td style="border:1px solid black; font-size:60%">HELLO WORLD</table></td><td>followed by this text afterward.</table> This line is more text after the outer table.
Result
  • This is a test table here
    HELLO WORLD
    followed by this text afterward.
    This line is more text after the outer table.

Use style="font-size:60%" to shrink the text within the box. However, the small text could be replaced with small images (aligned inside the inner table). The outer table is for 1 line only, so to make a 2nd line appear even, the exact length of line 1 must be pre-determined, to match the length of other lines.

Moving or exchanging columns

edit

Sometimes columns of data need to be listed in a different order, such as different contents in the 2nd column. Yet, because a wikitable is coded in markup language, the columns cannot simply be dragged across the screen as with a column-oriented editor. Instead, the harsh reality is that the tedious hand-editing of each cell, within a row, is often required as the fastest solution, in the long run.

However, some text editors do allow a repetition-loop to be defined to locate and shift every 7th line or such, as a repeated pattern that could re-arrange the columns in a large table. In another method sometimes used, every data-item is first prefixed with an alphabetic code, hand-coded for the eventual sequence, then those lines are sorted, and afterward all the leading-text prefixes are removed. Sorting can be done in separate files, such as using a DOS-prompt command: SORT myfile.DAT > myfile2.DAT, or else use a text-editor such as NoteTab, which has a modify-lines-sort option. Edit-tricks are most useful when multiple tables must be changed, then the time needed to develop complex edit-patterns can be applied to each table. For each table, insert an alpha-prefix on each column (making each row-token "|-" to sort as column zero, like prefix "Row124col00"), then sort into a new file, and then de-prefix the column entries.

Again, bear in mind, the tedious hand-editing of items in each row is often faster than the potential delay of automated edits gone awry. If just 2 columns are being swapped within 1 table, then cut/paste editing (of those column entries) is typically faster than column-prefixing, sorting and de-prefixing.

Another alternative is to copy the entire table from the displayed page, paste the text into a spreadsheet, move the columns as you will. Then reconstruct the table lines with a formula. This formula handles a three column table, reconstructing a single line. Editors can copy the line for each line of the table, then copy the formula rows back to the editing page.

 =A1&" || "&A2&" || "&A3&"<tr>"

A non-bug in HTML

edit

If you use tables for two-dimensional graphics you might discover a "feature" in HTML that promotes grey hair. It can affect both rows and columns, depending on the use of either rowspan or colspan.

In this 7-row table three cells have the height of 3 rows, but the total is 6 rows. Where is row 4? There is a row 5-4!

reference
row one
row two
row three
row four
row five
row six
row seven
example
row one row one row one
row two
3 rows
row two row two
row three
3 rows
row three
row four
3 rows
row five
row six row six
row seven row seven row seven

The answer is that when the table has a row without containing any rowspan=1 cell, this row is "compressed" upwards and disappears.

Solution: divide one of the tall cells so that the row gets one rowspan=1 cell (and don't mind the eventual loss of text-centering). Then kill the border between them. Don't forget to fill the cell with nothing ({{zwsp}}). This being the only solution that correctly preserves the cell height, matching that of the reference seven row table.

reference
row one
row two
row three
row four
row five
row six
row seven
example
row one row one row one
row two
3 rows
row two row two
row three
3 rows
row three
row four
3 rows
row five
row six row six
row seven row seven row seven

Or, in this case, you can simply add the third line of text (filling up the 3 rows of available space) to the "row two/3 rows" cell, preserving at the same time the text-centering availability:

reference
row one
row two
row three
row four
row five
row six
row seven
example
row one row one row one
row two
3 rows
row two row two
row three
3 rows
row three
row four
3 rows
row five
row six row six
row seven row seven row seven

A bug in class wikitable

edit

It is possible to remove borders between adjacent cells in tables, for example when you want a non-rectangular super-cell.

In this table the yellow cells are set up for exactly that. But, why are there borders anyway?

aaaaa bbbbb to this
cccccc connected
this cell ddddd
eeeee ffffffff


aaaaa bbbbb to this
cccccc connected
this cell ddddd
eeeee ffffffff



If you remove class=wikitable it is possible to see that the table is correctly set up:

aaaaa bbbbb to this
cccccc connected
this cell ddddd
eeeee ffffffff

Class wikitable demands that the border between two cells ONLY disappears when the cells begin on the SAME row (or column for vertical effect).

Solution: cut them up into pieces! And forget the centering of the text. Don't forget to fill the new cells with nothing (nbsp). Here with darker yellow:

aaaaa bbbbb to this
cccccc connected  
this cell   ddddd
eeeee ffffffff

Lists in multi-column tables

edit
For long lists of data, the quick way is to use asterisk-bullets "*" on separate lines, with a vertical-bar "|" between groups, and then separate the data-columns with spacer-columns, such as for width="30px":
  • Aardvark
  • Badger
  • Coyote
 
  • Dingo
  • Emu
  • Flounder
Again, any table can have a border using style="border:1px solid #BBB". The color #BBB matches image-boxes, but another color could be used, such as "blue" or #AA6633 (brownish):
This is a notebox
to show border color.
{| cellpadding=5 style="border:1px solid #AA6633"
|- bgcolor="#fafeff"
| This is a notebox
to show border color.
|}

Even though the coding might seem a little awkward, the results are easily controlled for spacing and alignment. The width of the table will, by default, remain the same for wider or narrowed windows, retaining the alignment with the left-side text (or section titles) outside the table.

See also

edit
edit