colspan (HTML attribute)

Description

Just as the th element uses colspans, so can the td element to allow data to be shared across numerous columns in a table. The concept is best explained by comparing the example HTML above with the below table (where we indicate a person’s availability for Tuesday—when that person is free for the whole day—by spanning the cell across four time slots).

td-colspans

When you’re introducing a colspan attribute, take care to ensure that each row is equivalent to the number of cells that would ordinary appear in the row. Some tables can become quite complicated with a mixture of colspan and rowspan attributes, and this is not something that you’re advised to hand-code—that’s just asking for trouble! It’s much better to use a WYSIWYG editor, such as Dreamweaver, MS Expression, or something similar, which will allow you to merge and unmerge cells very easily, and takes care of these attributes for you.

Note that when you use the rowspan attribute, you can’t span a cell beyond the boundary of its containing rowgroup (namely the thead, tfoot, or tbody elements). So if the tbody comprises ten rows, the rowspan of a cell that’s defined in the first row can’t have a value greater than “10”.

Example

This example shows an extract from a calendar, with a person’s availability apparently spanning the whole day, as shown in the above table:

<tr>
  <th scope="row">Tue</th>
  <td colspan="4">Free</td>
</tr>

Value

This attribute takes a number, which should equal the number of cells that this single td should replace. There’s also a special value of "0", which should inform the browser to span the cell to the end of the current group of columns.

Learn HTML5 Online

Get all SitePoint books and courses with a Learnable membership. Start building future-proof websites that are faster, more powerful, and easier to maintain.

No Reader comments

Comments on this post are closed.