Working with Column Headers
abbr attribute
- The
abbr (abbreviation) attribute can be placed inside <th>, so that as the heading is read aloud again and again (before the associated data cell's content) the abbreviation is read, not the full wording.
- The goal is to reduce the amount of text being spoken / output, especially as it is repeated time and time again.
abbr Example
<table border="1">
<tr>
<th abbr="first">First Name</th>
<th abbr="last">Last Name</th>
<th abbr="age">Years of Age</th>
</tr>
</table>
<thead>
- Defines the row or rows that constitutes the head of the table.
- Must contain at least one
<tr> tag.
- One of the few tags allowed to go between
<table> and <tr>.
- No visual effect on rendering.
- There is only one
<thead> per table.
- Browsers will print the rows within
<thead> at the top of each page, assuming that the table content wraps across a page or pages.
<thead> Example
<table border="1">
<thead>
<tr>
<th abbr="first">First Name</th>
<th abbr="last">Last Name</th>
<th abbr="age">Years of Age</th>
</tr>
</thead>
</table>
Associating Data Cells with Headers
headers and id attributes
- This pair of attributes explicitly tell the user agent (e.g., screen reader, browser) exactly which data cells go with which headers.
- The
id is the unique identifier that goes inside the header cell (the <th>).
- The
headers attribute typically goes inside the data cells (the <td>). Its value is the same value specified for the id.
- The
headers attribute can also be specified in <tr>.
- As the table is linearized, the header is read aloud, then the data, then the next header, the next data, etc.
- Multiple
headers values can be included (all inside the same attribute value), as long as they are each separated by at least one space.
- No visual impact on rendering.
headers and id Attributes Example
<table border="1">
<thead>
<tr>
<th abbr="first" id="fname">First Name</th>
<th abbr="last" id="lname">Last Name</th>
<th abbr="age" id="age">Years of Age</th>
</tr>
</thead>
<tbody>
<tr>
<td headers="fname">xxxxxx</td>
<td headers="lname">xxxxxx</td>
<td headers="age">xx</td>
</tr>
<tr>
<td headers="fname">xxxxxx</td>
<td headers="lname">xxxxxx</td>
<td headers="age">xx</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">Summary: xxxxxxx</td>
</tr>
</tfoot>
</table>
scope attribute
- An alternative to the
headers and id attributes is the scope attribute.
- This can be applied to
<th> and <td>.
- Values of
col, row, colgroup, and rowgroup are accepted.
- Typically this is applied to
<th>, specifying that the header applies to the column of data cells below (or the cells to the right, if the <th> is the first cell in a row within a tbody).
- This approach requires less code than the
headers / id approach.
- We sacrifice the ability to represent complex data relationships, however, and tables are effectively limited to headers across the top or down the left (or right) sides.
- No visual impact on rendering.
scope Attribute Example
<table border="1">
<thead>
<tr>
<th abbr="first" scope="col">First Name</th>
<th abbr="last" scope="col">Last Name</th>
<th abbr="age" scope="col">Years of Age</th>
</tr>
</thead>
<tbody>
<tr>
<td>xxxxxx</td>
<td>xxxxxx</td>
<td>xx</td>
</tr>
<tr>
<td>xxxxxx</td>
<td>xxxxxx</td>
<td>xx</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">Summary: xxxxxxx</td>
</tr>
</tfoot>
</table>
Adding a Caption
<caption>
- This tag provides a brief overview of the purpose and content of the table.
- Its content is rendered in the browser.
- This tag should be located immediately after the
<table> tag.
- The width of the caption is determined by the table width and its default location is above the table and centered.
<caption> Example
<table border="1">
<caption>Patient Demographics</caption>
<thead>
<tr>
<th abbr="first" scope="col">First Name</th>
<th abbr="last" scope="col">Last Name</th>
<th abbr="age" scope="col">Years of Age</th>
</tr>
</thead>
<tbody>
<tr>
<td>xxxxxx</td>
<td>xxxxxx</td>
<td>xx</td>
</tr>
<tr>
<td>xxxxxx</td>
<td>xxxxxx</td>
<td>xx</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">Summary: xxxxxxx</td>
</tr>
</tfoot>
</table>
<caption> Example Rendering
Patient Demographics
| First Name |
Last Name |
Years of Age |
| xxxxxx |
xxxxxx |
xx |
| xxxxxx |
xxxxxx |
xx |
| Summary: xxxxxxx |