Testing scrollable tables

What's the problem?

Data tables can have lots of columns. On smaller screens, wide tables may not fit within the available space. This can cause content to be cut off or force the entire page to scroll sideways. The same problem can happen on desktop screens when people zoom in.

Possible accessibility issues

When tables are wider than the available space, some users may have difficulty accessing the content:

Solutions

There are several ways to deal with wide tables:

Each approach has advantages and disadvantages.

This page looks at the third option: placing a table inside a container that can scroll horizontally when needed.

Example 1: scrollable table

On a wide screen, this table may fit without scrolling. Use the button below to narrow the page, so the table has to scroll. Then press it again to reset the page width.

Screen reader results
Test NVDA + Chrome NVDA + Firefox JAWS + Chrome JAWS + Edge VoiceOver + Safari VoiceOver + Chrome
Table name announced Pass Pass Pass Pass Pass Pass
Region announced on focus Pass Pass Pass Pass Pass Fail
Row and column count Pass Pass Pass Pass Pass Pass
Column headers announced Pass Pass Pass Pass Pass Pass
Keyboard scrolling Pass Pass Pass Pass Pass Pass

Step 1: Add a wrapper around the table

Start by placing the table inside a div element.

<div class="table-wrap">
  <table>
    ...
  </table>
</div>

The div acts as a container for the table. We can use CSS to allow the table to scroll horizontally without affecting the rest of the page.

.table-wrap {
  overflow-x: auto;
}

The overflow-x: auto property allows horizontal scrolling when the table is wider than its container.

If the table fits, no scrolling is needed.

Step 2: Make the scrolling container accessible

There is one potential problem with this approach.

In some browsers, keyboard users may not be able to scroll the table horizontally unless the container can receive keyboard focus.

We can address this by adding three attributes to the div:

Here is the complete HTML pattern:

<div class="table-wrap"
  tabindex="0"
  role="region"
  aria-labelledby="cap-sales"
>
  <table>
    <caption id="cap-sales">Sales by quarter</caption>
    ...
  </table>
</div>

Let's look at these attributes in more detail.

tabindex="0"

This allows keyboard users to move focus to the container using the Tab key.

Once the container has focus, users can use the arrow keys to scroll horizontally.

Without this attribute, some keyboard users may have no way to reach content that is outside the visible area.

Because the container can now receive focus, it also needs a visible focus indicator, so keyboard users can see where they are.

.table-wrap:focus-visible {
  outline: 2px solid #1565A8;
  outline-offset: 2px;
}

role="region"

This identifies the container as a region.

When a screen reader user moves focus to the container, the role helps them understand what they have reached.

aria-labelledby

This gives the region an accessible name.

In our example, aria-labelledby="cap-sales" points to the table's caption, which has the matching ID.

The region is therefore named "Sales by quarter".

This means we can reuse the existing table caption rather than writing another label.

Captions are a good habit anyway, and this technique gives you one more reason to use them.

Step 3: Only add these attributes when needed

There is a disadvantage to adding these three attributes to every table.

If a table already fits within its container, it doesn't need to scroll. But the tabindex attribute still adds an extra stop when someone navigates through the page using the Tab key.

Screen reader users may also hear additional information about the region, even though it serves no useful purpose.

For this reason, it's better to add these attributes only when a table actually needs to scroll.

We can use JavaScript to do this.

Start with the original HTML:

<div class="table-wrap">
  <table>
    <caption>Sales by quarter</caption>
    ...
  </table>
</div>

The JavaScript can then:

  1. Find every element with the class table-wrap.
  2. Give the table caption a unique ID if it doesn't already have one.
  3. Check whether the table is wider than its container.
  4. If the table needs to scroll, add tabindex and role, plus a name: aria-labelledby pointing to the caption, or a fallback aria-label if there is no caption.
  5. If the table doesn't need to scroll, remove these attributes.
  6. Check again when the container or table changes size.

Here is the complete script:

let captionNumber = 0;

document.querySelectorAll('.table-wrap').forEach((wrap) => {
  const table = wrap.querySelector('table');
  if (!table) return;

  const caption = table.querySelector('caption');

  // Give the caption a unique ID if it needs one
  if (caption && !caption.id) {
    let id;
    do {
      id = 'table-caption-' + (++captionNumber);
    } while (document.getElementById(id));
    caption.id = id;
  }

  const update = () => {
    const scrolls = wrap.scrollWidth > wrap.clientWidth;

    if (scrolls) {
      wrap.setAttribute('tabindex', '0');
      wrap.setAttribute('role', 'region');
      if (caption) {
        wrap.setAttribute('aria-labelledby', caption.id);
      } else {
        // Fallback name if the table has no caption
        wrap.setAttribute('aria-label', 'Scrollable table');
      }
    } else {
      wrap.removeAttribute('tabindex');
      wrap.removeAttribute('role');
      wrap.removeAttribute('aria-labelledby');
      wrap.removeAttribute('aria-label');
    }
  };

  const observer = new ResizeObserver(update);
  observer.observe(wrap);
  observer.observe(table);
});

This means the table can adapt to different screen sizes and zoom levels.

On a wide screen, the table may fit without scrolling, so there is no extra Tab stop.

On a smaller screen, or when someone zooms in, the table may need to scroll. The JavaScript then makes the container keyboard focusable and provides a region role and accessible name.

Step 4: Load the JavaScript

The JavaScript can be saved in a separate file and loaded using a script element.

<script src="table-wrap.js" defer></script>

The defer attribute is important.

It tells the browser to download the JavaScript while the HTML is being processed, but wait until the HTML has been parsed before running the script.

This means the tables will be available when the JavaScript looks for them.

It also avoids blocking HTML parsing while the script downloads.

The same JavaScript file can be used across multiple pages, so there is no need to add separate scripts for individual tables.

Summary

Allowing wide tables to scroll horizontally is one way to make them work on smaller screens and at higher zoom levels.

The basic approach is simple:

This avoids unnecessary Tab stops while allowing keyboard users to reach horizontally scrolling table content.