The last time we discussed tables, we covered some lesser-used child elements. Between that and the first article on tables, we’re set on table elements. But what about table-specific attributes? There are quite a few, some which you may not even be familiar with. These can help non-visual user agents as well as semantics. Read on to learn more.
A good table should generally be visually designed such that a visitor can look at it and use visual cues to understand the most important data, if any, or be guided visually toward whatever data are the focus of the table. Non-visual visitors do not receive those visual cues, however. The majority of these attributes focus on non-visual user agents. They give additional information or auditory cues to allow the table to be better understood.
Please note that when I reference “the attributes of an element” in this article, I’m referring to the element/table
-specific attributes. Like most elements, these can have classes, ids, and other common attributes. No need to cover those here. Additionally, I’m from the school of separate content from presentation, as you should be, and so any attributes that are visual—and thus can be set using CSS—will also be ignored for the purposes of this article. Leave a comment if you want me to cover styling tables with CSS in the future.
table
element attributes
Despite this series of articles focusing on tables, we haven’t really talked about the table
element, have we? It’s pretty straight forward; you put all the important bits inside the <table>
tag, maybe add a class
or id
, and don’t think about the actual table
element again. However, for good table design, we should be putting a little more thought into that. There is at least one table
element-specific attribute that is smart to use.
summary
- The
summary
attribute allows you to provide a summary of the table for non-visual user agents. Visual users may be able to look at a table and understand instantly that such and such lines total a specific amount, thanks to visual design cues, but what about non-visual visitors? By providing that sort of information in thesummary
attribute, you allow them to quickly gain the understanding that visual visitors have. Example:<table summary="The data show that sales are increasing steadily, rising almost $1000 per quarter, with a year end total of $12,000 this year as opposed to $8,000 at the end of last year.">
cellspacing
- This attribute controls the space in between cells—not just padding on the cells, but actual spacing between the cells, borders and all. Modern Web development generally calls for a separation of content and presentation. As such, most display attributes for table are either deprecated or better set though CSS.
cellspacing
is the one remaining attribute that can’t be set through CSS reliably. Truthfully, I’ve never found need for it—I always want a cell spacing of 0, which is easily accomplished through CSS by specifyingborder-collapse: collapse;
on the table. However if you do need spacing between your cells, the closest CSS comes is aborder-spacing
property that is ignored by IE.cellspacing
is not deprecated, so go ahead and throw it in if you really need it. Its sister attributecellpadding
is not neccessary; you can add padding to any or all cells.
Cell attributes (th
and td
)
In How to Use the Table Element Correctly, we explored the difference between the two cell elements, th
and td
. While their use in semantic tables may differ, they share many attributes.
colspan
,rowspan
- The two spanning attributes allow you to create a cell that spans multiple columns or rows. For instance,
<td colspan="5">
would span the entire width of a 5-column table. Likewise,<th rowspan="2">
would create a heading cell that spans two rows.It is important to note that using these two attributes requires that you leave out cells that would overlap. Here is an example:
<tr> <th rowspan="2">Cells</th> <td>data</td> <td>spanning, headers, axis, abbr</td> <td>id, class, etc</td> </tr> <tr> <td>header</td> <td colspan="2">Same as above</td> </tr>
Note that there are only two cells in the second row. The first cell is not declared, as it is spanned from the row above. And the last cell is also left undeclared, because the third (second defined) spans over the final cell.
The value of these two attributes is numeric. A value of “0” is perfectly valid; this value tells the user agent to span from the current location to the end of the row group (
thead
,tbody
,tfoot
), ifrowspan
, or end of the currentcolgroup
, ifcolspan
. axis
- The
axis
attribute is an interesting creature. It allows for categorization of data, which enables user agents to calculate answers to queries. For instance, in a table of expenses, if you label a cell withaxis="entertainment"
, a user can query the user agent with something like “What did I spend on entertainment”. The user agent can ascertain which data are categorized as entertainment and then calculate an answer.
Cell attributes that are mainly useful for th
There are a few attributes that are perfectly legal to use on td
elements, but if used properly, they really make more sense being restricted to th
elements. They are immensely helpful to non-visual user agents, especially when the contents of the table are being read out loud.
scope
- Used primarily on
th
elements, thescope
attribute designates which cells the header refers to. Allowed values are:- row
- col
- rowgroup
- colgroup
Adding the
scope
attribute to the firsttd
in a row will cause it to act like a header with non-visual user agents. But since we’re being semantic, we’d never do that, right? abbr
- This attribute is immensely useful for non-visual agents, especially auditory ones. It allows you to specify an abbreviation of the cell contents for use when reading multiple times. It mostly pertains to
th
elements, which are repeated before data, however it can be used fortd
as well. Example usage:<th abbr="Density">Population Density (1-100)</th>
For non-visual users, the full header would be read on the first encounter, but any repetition would use the abbreviation.
Cell attributes that are mainly useful for td
The headers
attribute is the sibling of scope
. Whereas scope
defines which data a header pertains to, headers
allows for explicit indication of which headers pertain to a given data cell. Its value is a space-separated list of id
s, so you must make sure to use the id
attribute on any headers referenced in this way. An example:
<tr>
<th colspan="0" id="h1">Shared Attributes</th>
</tr>
<tr>
<th rowspan="2">Cells</th>
<td>data cell</td>
<td>spanning, headers, axis, abbr</td>
<td>id, class, etc</td>
</tr>
<tr>
<td>header cell</td>
<td colspan="2" headers="h1">Same as above</td>
</tr>
In the above example, we see that the cell with contents “Same as above” is marked as being headed by the “Shared Attributes” header. According to the W3C, this attribute is to be used by non-visual agents, but can also be useful to visual agents through the use of CSS styling.
Other table element attributes
The attributes for other elements, such as rows, rowgroups and columns have either been covered in the last article or are merely design aids. The latter should be done through CSS rather than HTML attributes. If you want to learn more about those or any of the attributes discussed here, the W3C Recommendation on Tables is a great place to learn virtually everything about them. It includes plenty of examples as well.
And thus concludes the series on tables. Do you have any questions, or care to expand on what I’ve said here?