The introduction of smartphones increasingly led web designers to avoid traditional "bulky" HTML tables - because these would exceed the limited screen dimensions. Others tried to find ways to change the layout of tables so they would not need so much horizontal space anymore. Responsive tables were born. But to make them accessible, the use of some ARIA is essential.
Tables - a relic of days gone by?
Tables exist since the very early days of the internet. In and by themselves, their layout is meant to have a lot of horizontal space available.
Since portable devices like smartphones have become increasingly popular, screens have tended to become smaller and smaller. Alas, the use of traditional tables tends to be avoided by many modern websites (to prevent the need of horizontal scrolling).
On the other side, there have also been attempts to change the layout of traditional tables so they would fit these new requirements. Sadly, most of these attempts left accessibility behind. So we show you to change a table's visual appearance while keeping accessibility intact.
Saving space
Instead of transforming a traditional table's full layout into a responsive one (as will be explained below), it can be useful already to simply hide some of its elements on smaller screens.
Hiding negligible data
The following table about hobbies displays columns for a "Name", a "Description" and a link to "Additional resources".
As the description in fact simply is taken from the linked additional resources, we can easily hide it on narrow view to save horizontal space (if you haven't done this yet, go back and read Hiding elements from all devices). In the following example, please resize your browser to trigger narrow view.
<table><caption>My Hobbies</caption><thead><tr><th>
Name
</th><thclass="hidden-narrow">
Description
</th><th>
Additional Resources
</th></tr></thead><tbody><tr><th>
Playing Soccer
</th><tdclass="hidden-narrow">
Soccer is a team sport played between two teams of eleven players with a spherical ball.
</td><td><ahref="https://en.wikipedia.org/wiki/Association_football">Wikipedia</a></td></tr><tr><th>
Dancing
</th><tdclass="hidden-narrow">
Dance is a performing art form consisting of purposefully selected sequences of human movement.
</td><td><ahref="https://en.wikipedia.org/wiki/Dance">Wikipedia</a></td></tr><tr><th>
Gardening
</th><tdclass="hidden-narrow">
Gardening is the practice of growing and cultivating plants as part of horticulture.
</td><td><ahref="https://en.wikipedia.org/wiki/Gardening">Wikipedia</a></td></tr></tbody></table>
<table><caption>My Hobbies</caption><thead><tr><th>
Name
</th><th>
Description
</th><th>
Additional Resources
</th></tr></thead><tbody><tr><th>
Playing Soccer
</th><td>
Soccer is a team sport played between two teams of eleven players with a spherical ball.
</td><td><ahref="https://en.wikipedia.org/wiki/Association_football"><spanclass="hidden-narrow">Wikipedia</span><imgalt="Wikipedia"class="hidden-wide"src="wiki.png" /></a></td></tr><tr><th>
Dancing
</th><td>
Dance is a performing art form consisting of purposefully selected sequences of human movement.
</td><td><ahref="https://en.wikipedia.org/wiki/Dance"><spanclass="hidden-narrow">Wikipedia</span><imgalt="Wikipedia"class="hidden-wide"src="wiki.png" /></a></td></tr><tr><th>
Gardening
</th><td>
Gardening is the practice of growing and cultivating plants as part of horticulture.
</td><td><ahref="https://en.wikipedia.org/wiki/Gardening"><spanclass="hidden-narrow">Wikipedia</span><imgalt="Wikipedia"class="hidden-wide"src="wiki.png" /></a></td></tr></tbody></table>
Please do not forget to set a proper alternative text to those icons.
Changing the visual layout
Sometimes, the visual layout of a table needs to be changed completely to fit small screens.
As we already know: to alter a table's visual appearance, the display property can be changed, and some ARIA needs to be added (if you haven't done this yet, go back and read Changing a table's visual layout). Take a look at the following example of a responsive table: when resizing the browser, you will see all elements stack on top of each other.
<tablerole="grid"><caption>My Hobbies</caption><theadrole="presentation"><trrole="row"><throle="columnheader">
Name
</th><throle="columnheader">
Description
</th><throle="columnheader">
Additional Resources
</th></tr></thead><tbodyrole="presentation"><trrole="row"><throle="rowheader">
Playing Soccer
</th><tdrole="gridcell">
Soccer is a team sport played between two teams of eleven players with a spherical ball.
</td><tdrole="gridcell"><ahref="https://en.wikipedia.org/wiki/Association_football">Wikipedia</a></td></tr><trrole="row"><throle="rowheader">
Dancing
</th><tdrole="gridcell">
Dance is a performing art form consisting of purposefully selected sequences of human movement.
</td><tdrole="gridcell"><ahref="https://en.wikipedia.org/wiki/Dance">Wikipedia</a></td></tr><trrole="row"><throle="rowheader">
Gardening
</th><tdrole="gridcell">
Gardening is the practice of growing and cultivating plants as part of horticulture.
</td><tdrole="gridcell"><ahref="https://en.wikipedia.org/wiki/Gardening">Wikipedia</a></td></tr></tbody></table>
As the table is enhanced using ARIA, screen reader users are very happy with this result.
Optimisation for visual users
Visually though, our table is not fully appealing yet, because in narrow view, the table headers' position on top of the table feels wrong.
Hiding table headers visually
In a first attempt, we can hide them visually in narrow view (if you haven't done this yet, go back and read Hiding elements visually by moving them off-screen). This way, they keep working for screen readers.
<tablerole="grid"><caption>My Hobbies</caption><theadrole="presentation"><trrole="row"><throle="columnheader">
Name
</th><throle="columnheader">
Description
</th><throle="columnheader">
Additional Resources
</th></tr></thead><tbodyrole="presentation"><trrole="row"><throle="rowheader">
Playing Soccer
</th><tdrole="gridcell">
Soccer is a team sport played between two teams of eleven players with a spherical ball.
</td><tdrole="gridcell"><ahref="https://en.wikipedia.org/wiki/Association_football">Wikipedia</a></td></tr><trrole="row"><throle="rowheader">
Dancing
</th><tdrole="gridcell">
Dance is a performing art form consisting of purposefully selected sequences of human movement.
</td><tdrole="gridcell"><ahref="https://en.wikipedia.org/wiki/Dance">Wikipedia</a></td></tr><trrole="row"><throle="rowheader">
Gardening
</th><tdrole="gridcell">
Gardening is the practice of growing and cultivating plants as part of horticulture.
</td><tdrole="gridcell"><ahref="https://en.wikipedia.org/wiki/Gardening">Wikipedia</a></td></tr></tbody></table>
It would be even more beautiful if the table headers could be displayed visually next to each table cell. For this, we have to add them in each cell, but display them only in narrow view.
But this is redundant information for screen readers, so we use aria-hidden="true", trying to hide those additional table headers again. This works great with NVDA, while JAWS keeps announcing them (but we can live with that).
<tablerole="grid"><caption>My Hobbies</caption><theadrole="presentation"><trrole="row"><throle="columnheader">
Name
</th><throle="columnheader">
Description
</th><throle="columnheader">
Additional Resources
</th></tr></thead><tbodyrole="presentation"><trrole="row"><throle="rowheader"><spanaria-hidden="true"class="narrow-th">Name: </span>Playing Soccer
</th><tdrole="gridcell"><spanaria-hidden="true"class="narrow-th">Description: </span>Soccer is a team sport played between two teams of eleven players with a spherical ball.
</td><tdrole="gridcell"><spanaria-hidden="true"class="narrow-th">Additional Resources: </span><ahref="https://en.wikipedia.org/wiki/Association_football">Wikipedia</a></td></tr><trrole="row"><throle="rowheader"><spanaria-hidden="true"class="narrow-th">Name: </span>Dancing
</th><tdrole="gridcell"><spanaria-hidden="true"class="narrow-th">Description: </span>Dance is a performing art form consisting of purposefully selected sequences of human movement.
</td><tdrole="gridcell"><spanaria-hidden="true"class="narrow-th">Additional Resources: </span><ahref="https://en.wikipedia.org/wiki/Dance">Wikipedia</a></td></tr><trrole="row"><throle="rowheader"><spanaria-hidden="true"class="narrow-th">Name: </span>Gardening
</th><tdrole="gridcell"><spanaria-hidden="true"class="narrow-th">Description: </span>Gardening is the practice of growing and cultivating plants as part of horticulture.
</td><tdrole="gridcell"><spanaria-hidden="true"class="narrow-th">Additional Resources: </span><ahref="https://en.wikipedia.org/wiki/Gardening">Wikipedia</a></td></tr></tbody></table>