Responsive table
A table that not only scrolls horizontally when the content exceeds screen width, but also collapses to display cells vertically at smaller viewports.
Example
The following comparison table may be squeezed by a handle on the bottom right.
This example uses both media queries (screen width) and container queries to adapt content.
Can be used if you: | Carers Card | Third party access | General Power of Attorney | Lasting Power of Attorney | Court of Protection |
---|---|---|---|---|---|
Just need help with the small things, like shopping or getting cash. |
|||||
Are physically ill, injured or disabled. |
|||||
Need a little help with looking after your day-to-day banking, for a shorter period of time, but you don't want someone to make decisions for you. |
|||||
You will be abroad, and won't have access to your account (for example, travelling). |
|||||
Are preparing for when you'll be unable to make decisions, or want someone to start looking after all of your finances. |
|||||
No longer have the mental capacity to make decisions, and someone needs to do this for you. |
Features
- Doesn't require JavaScript.
- Works perfectly with screen-reader software.
- Handles extreme font sizing responsibly.
- Minimum width of row heading definable for desktop and mobile using CSS variables.
The code
HTML
CSS
Settings
- Cell padding desktop: 16px
- Cell padding mobile and tablet: 8px
- Breakpoints: 40em (640px) for column view, and 50em (800px) for padding.
- If Santander red (#ec0000) is used to colour text, then the background color must be white.
- If using a non-white background colour, ensure the text has adequate colour contrast, especially when choosing a shade of red.
- Text in left column has a line-height of 1.5, while the column headers are 1.3
- Tick and dash svgs are created in the same size viewbox (30 x 25 px).
Guidelines
Data tables are a rare example of when it is better to code the desktop view first, then adjust for mobile.
- When coding, use real HTML table elements, not div grids. There's less to do/consider from both an accessibility and a coding perspective.
- Use a figure element, or a div role="region", to wrap a table for horizontal scrolling at large page zooms.
- If possible avoid setting a width on the table element.
- Avoid spanning table columns and rows (colspan, rowspan).
- Avoid complex tables.
- Add a caption which describes the table content. Alternatively, use aria-labelledby to point to relevant content.
- Use inline SVG image icons - They scale with font-size.
- Use inline SVG, rather than PNG, for icons, colours may be changed by the CSS. For instance, when using dark mode.
-
Roles are explicitly stated on table elements because at mobile sizes the elements have their default roles overwritten by CSS.
Hard code each table, tbody, tbody th, and tbody td element with it's ARIA role:
Table element role equivalents HTML element ARIA role table table caption caption thead, tbody, tfoot rowgroup tr row th[scope=col] columnheader th[scope=row] rowheader td cell - Add a data attribute to each tbody cell. E.G. data-header="Lasting Power of Attorney" - This becomes a row title on mobile viewports.
- Ensure every th is assigned a scope of col or row.
- Left align text, right align numbers, center align icons.
- Table needs to work on a 320px screen width, with a font scaled to 200%. This can be emulated by setting the browser width to 1280px then scaling the font size up to 400%.
- Consider reducing horizontal margin and padding on mobile views. The standard 20px margin plus a 20px padding will consume 25% of the already limited minimum screen width (320px).