HTML Table Advanced Features and Accessibility - Learn Web Development - MDN
HTML Table Advanced Features and Accessibility - Learn Web Development - MDN
…
</table>
As you can infer from the brief example above, the caption is meant to contain a description
of the table contents. This is useful for all readers wishing to get a quick idea of whether the
table is useful to them as they scan the page, but particularly for blind users. Rather than
have a screen reader read out the contents of many cells just to find out what the table is
about, the user can rely on a caption and then decide whether or not to read the table in
greater detail.
https://developer.mozilla.org/en-US/docs/Learn/HTML/Tables/Advanced 1/11
11/30/23, 4:16 PM HTML table advanced features and accessibility - Learn web development | MDN
Note: The summary attribute can also be used on the <table> element to provide a
description — this is also read out by screen readers. We'd recommend using the
<caption> element instead, however, as summary is deprecated and can't be read by
Note: You can find our version on GitHub — see timetable-caption.html (see it live
also ).
To use them:
The <thead> element must wrap the part of the table that is the header — this is usually
the first row containing the column headings, but this is not necessarily always the case.
If you are using <col> / <colgroup> element, the table header should come just below
those.
The <tfoot> element needs to wrap the part of the table that is the footer — this might
be a final row with items in the previous rows summed, for example. You can include the
table footer right at the bottom of the table as you'd expect, or just below the table
header (the browser will still render it at the bottom of the table).
The <tbody> element needs to wrap the other parts of the table content that aren't in the
table header or footer. It will appear below the table header or sometimes footer,
depending on how you decided to structure it.
Note: <tbody> is always included in every table, implicitly if you don't specify it in
your code. To check this, open up one of your previous examples that doesn't
include <tbody> and look at the HTML code in your browser developer tools — you
will see that the browser has added this tag for you. You might wonder why you
ought to bother including it at all — you should, because it gives you more control
over your table structure and styling.
4. Save and refresh, and you'll see that adding the <tfoot> element has caused the "SUM"
row to go down to the bottom of the table.
5. Next, add a colspan attribute to make the "SUM" cell span across the first four columns,
so the actual number appears at the bottom of the "Cost" column.
6. Let's add some simple extra styling to the table, to give you an idea of how useful these
elements are for applying CSS. Inside the head of your HTML document, you'll see an
empty <style> element. Inside this element, add the following lines of CSS code:
CSS
tbody {
font-size: 95%;
font-style: italic;
}
tfoot {
font-weight: bold;
}
7. Save and refresh, and have a look at the result. If the <tbody> and <tfoot> elements
weren't in place, you'd have to write much more complicated selectors/rules to apply the
same styling.
Note: We don't expect you to fully understand the CSS right now. You'll learn more
about this when you go through our CSS modules (Introduction to CSS is a good
place to start; we also have an article specifically on styling tables).
Your finished table should look something like the following:
https://developer.mozilla.org/en-US/docs/Learn/HTML/Tables/Advanced 4/11
11/30/23, 4:16 PM HTML table advanced features and accessibility - Learn web development | MDN
My spending record
How I chose to spend my money
SUM 118
Nesting Tables
It is possible to nest a table inside another one, as long as you include the complete
structure, including the <table> element. This is generally not really advised, as it makes the
markup more confusing and less accessible to screen reader users, and in many cases you
might as well just insert extra cells/rows/columns into the existing table. It is however
sometimes necessary, for example if you want to import content easily from other sources.
The following markup shows a simple nested table:
HTML Play
<table id="table1">
<tr>
<th>title1</th>
<th>title2</th>
<th>title3</th>
https://developer.mozilla.org/en-US/docs/Learn/HTML/Tables/Advanced 5/11
11/30/23, 4:16 PM HTML table advanced features and accessibility - Learn web development | MDN
</tr>
<tr>
<td id="nested">
<table id="table2">
<tr>
<td>cell1</td>
<td>cell2</td>
<td>cell3</td>
</tr>
</table>
</td>
<td>cell2</td>
<td>cell3</td>
</tr>
<tr>
<td>cell4</td>
<td>cell5</td>
<td>cell6</td>
</tr>
</table>
https://developer.mozilla.org/en-US/docs/Learn/HTML/Tables/Advanced 6/11
11/30/23, 4:16 PM HTML table advanced features and accessibility - Learn web development | MDN
To understand its information we make visual associations between the data in this table and
its column and/or row headers.
Note: There are around 253 Million people living with Visual Impairment according to
WHO data in 2017 .
This section of the article provides further techniques for making tables as accessible as
possible.
Using column and row headers
Screen readers will identify all headers and use them to make programmatic associations
between those headers and the cells they relate to. The combination of column and row
https://developer.mozilla.org/en-US/docs/Learn/HTML/Tables/Advanced 7/11
11/30/23, 4:16 PM HTML table advanced features and accessibility - Learn web development | MDN
headers will identify and interpret the data in each cell so that screen reader users can
interpret the table similarly to how a sighted user does.
We already covered headers in our previous article — see Adding headers with <th>
elements.
The scope attribute
A new topic for this article is the scope attribute, which can be added to the <th> element to
tell screen readers exactly what cells the header is a header for — is it a header for the row it
is in, or the column, for example? Looking back to our spending record example from earlier
on, you could unambiguously define the column headers as column headers like this:
HTML
<thead>
<tr>
<th scope="col">Purchase</th>
<th scope="col">Location</th>
<th scope="col">Date</th>
<th scope="col">Evaluation</th>
<th scope="col">Cost (€)</th>
</tr>
</thead>
And each row could have a header defined like this (if we added row headers as well as
column headers):
HTML
<tr>
<th scope="row">Haircut</th>
<td>Hairdresser</td>
<td>12/09</td>
<td>Great idea</td>
<td>30</td>
</tr>
https://developer.mozilla.org/en-US/docs/Learn/HTML/Tables/Advanced 8/11
11/30/23, 4:16 PM HTML table advanced features and accessibility - Learn web development | MDN
screen readers will recognize markup structured like this, and allow their users to read out the
entire column or row at once, for example.
scope has two more possible values — colgroup and rowgroup . These are used for headings
that sit over the top of multiple columns or rows. If you look back at the "Items Sold August
2016" table at the start of this section of the article, you'll see that the "Clothes" cell sits
above the "Trousers", "Skirts", and "Dresses" cells. All of these cells should be marked up as
headers ( <th> ), but "Clothes" is a heading that sits over the top and defines the other three
subheadings. "Clothes" therefore should get an attribute of scope="colgroup" , whereas the
others would get an attribute of scope="col" .
The id and headers attributes
An alternative to using the scope attribute is to use id and headers attributes to create
associations between headers and cells. The way they are used is as follows:
1. You add a unique id to each <th> element.
2. You add a headers attribute to each <td> element. Each headers attribute has to contain
a list of the id s of all the <th> elements that act as a header for that cell, separated by
spaces.
This gives your HTML table an explicit definition of the position of each cell in the table,
defined by the header(s) for each column and row it is part of, kind of like a spreadsheet. For
it to work well, the table really needs both column and row headers.
Returning to our spending costs example, the previous two snippets could be rewritten like
this:
HTML
<thead>
<tr>
<th id="purchase">Purchase</th>
<th id="location">Location</th>
<th id="date">Date</th>
<th id="evaluation">Evaluation</th>
https://developer.mozilla.org/en-US/docs/Learn/HTML/Tables/Advanced 9/11
11/30/23, 4:16 PM HTML table advanced features and accessibility - Learn web development | MDN
…
</tbody>
Note: This method creates very precise associations between headers and data
cells but it uses a lot more markup and does not leave any room for errors. The
scope approach is usually enough for most tables.
Note: You can check your work against our finished examples — see items-sold-
scope.html (also see this live ) and items-sold-headers.html (see this live too
).
Summary
https://developer.mozilla.org/en-US/docs/Learn/HTML/Tables/Advanced 10/11
11/30/23, 4:16 PM HTML table advanced features and accessibility - Learn web development | MDN
There are a few other things you could learn about tables in HTML, but this is all you need to
know for now. Next, you can test yourself with our HTML tables assessment. Have fun!
If you are already learning CSS and have done well on the assessment, you can move on and
learn about styling HTML tables — see Styling tables.
If you want to get started with learning CSS, check out the CSS Learning Area!
https://developer.mozilla.org/en-US/docs/Learn/HTML/Tables/Advanced 11/11