CSS Grid and accessibility

Further front-end learnings on the power and responsibility that come with CSS Grid, plus more resources for keeping things accessibile.

Preface

It is in the very nature of falling down “rabbit holes” that learning one thing leads to another, and another ad infinitum. It’s only fitting, then, that the final section of Down the front-end rabbit hole (on CSS Grid) began to suffer from some serious informational bloat, and minor topical drift. So, in an effort to factor things out, my notes on CSS Grid and accessibility continue here.

All prior caveats apply. These are merely the notes of a new learner, and I can only promise to direct you to the sources of information (all of which are infinitely more comprehensive and reliable than the notes themselves). In fact, if you want to learn about digital accessibility, you should probably jump ship and head to The A11y Project now.

CSS Grid: Power and responsibility

A quick reminder as to how we got here: Quarto’s HTML theming is powered by Bootstrap—specifically, Bootstrap 5.1 with CSS Grid enabled. Quarto has plenty of built-in article-layout and page-layout features, but, for page-layout: custom you’d want to get to know CSS Grid a bit better.

Note: From here on out, if I use the term “Grid” it will be in reference to CSS Grid, despite the fact that other grid systems exist (such as Bootstrap’s native grid).

I should note, I think CSS Grid is great, and I’ve had a blast playing around with it in Quarto (e.g. in my Quarto page-layout demo)! It also has a lot of really great documentation and guides, such as A Complete Guide to Grid (House 2021), which makes it all the more fun to learn about. And, because Grid is a tool for responsive design, it can be used to enhance accessibility across devices.

Accessibility is a huge topic, and CSS Grid is by no means immune to the challenges inherent in it. The aforementioned A11y Project and the W3C Web Accessibility Initiative are two great places for finding general resources—Developing for Web Accessibility - Tips for Getting Started (White, Abou-Zahra, and Henry 2019) is a good starting point if this is new to you. There are two broad areas, though, where Grid (if not wielded properly) can yield accessibility problems: content re-ordering, and “markup flattening.”

Source vs. visual order

Part of what makes Grid so powerful, the ability to re-flow and rearrange content, can also make it dangerously easy to create a disconnect between the logical and visual ordering of content. In fact, it’s right in the W3C Grid spec:

Grid layout gives authors great powers of rearrangement over the document. However, these are not a substitute for correct ordering of the document source (Etemad, Atanassov, and Jr. 2020).

Luckily, there are also many resources, such as CSS Grid Layout and Accessibility: Re-ordering content in CSS Grid Layout (MDN Contributors 2021a), addressing this topic. And, to be clear, this challenge isn’t endemic to CSS Grid, or particularly new. CSS experts, such as Rachel Andrew, have been writing about this for a long time.

In Modern CSS layout, power and responsibility, she writes:

With Grid and Flexbox you no longer need to describe your layout in markup, they give us a way to truly separate content and presentation…With this power comes great responsibility. For just as it will be possible for a developer to start out with a beautifully semantic, well structured document and use Grid and Flexbox to meet the design requirements, it will be possible for them to stop caring about the document structure at all (Andrew 2015).

Why does this matter so much? From a more recent post by Andrew, Content reordering:

The order of content in your document is important for the accessibility of your site. A screen reader will read out content based on the document order, using the HTML elements that you have selected to give meaning to that content (Andrew 2020).

In The Dark Side of the Grid (Part 2), Manuel Matuzović concisely describes what CSS can and cannot change when it comes to order:

  1. Both tab order and the order in which screen readers read content follow DOM order.
  2. Changing visual order with CSS has no effect on DOM order.

No matter where we place items with CSS, keyboard users will still encounter elements in the order in which they appear in the HTML document (Matuzović 2019b).

Markup flattening

MDN’s HTML: A good basis for accessibility opens with the following statement:

A great deal of web content can be made accessible just by making sure the correct Hypertext Markup Language [HTML] elements are used for the correct purpose at all times (MDN Contributors 2022).

Using semantic HTML (or semantic markup) is, essentially, using the right HTML element for the job. For example, using <h1>, <h2>, etc. for headers gives your document structure for assistive technologies (which we’ll discuss in more detail in a subsequent section).

Note: Given my focus on Quarto and R Markdown, it’s worth noting here that, generally speaking, markdown lends itself to good semantic markup (which isn’t to say that there aren’t limitations). Check out Improving the Accessibility of your Markdown (Bailey 2021) for some helpful tips.

However, there are certain features of Grid (namely the fact that items need to be direct children of the grid container in order to be treated as grid items) that can lead to “compromising on semantics,” as described in The Dark Side of the Grid (Part 1) (Matuzović 2019a). There are solutions in the pipeline (several of which are discussed in The Dark Side of the Grid Part 1), but it’s something to keep in mind as you start using Grid.

Getting it right

Official specs and guidelines

The accessibility tree

Understanding the big picture (e.g. how assistive technologies1 interact with web content) makes inclusive, accessible development much easier. I strongly recommend reading these brief background appendices from The Accessibility Object Model (AOM) - Explainer (Boxhall et al. 2021):

The appendices, above, are the source of the notes and figures that follow in this section.

Assistive technologies interact with web pages and applications through platform-specific accessibility APIs that expose a tree of objects referred to as the accessibility tree.

Flow from application UI to accessibility tree to assistive technology to user.

In web browsers, the structure and content of a web page’s markup is represented by a Document Object Model (DOM) tree (MDN Contributors 2021b). The DOM tree is, then, translated into both the visual representation (what you see in the browser), and the accessibility tree, which can be accessed by platform-specific accessibility APIs, which bring the information to the user via assistive technologies.

HTML translated into DOM tree translated into visual UI and accessibility tree.

Each node of the accessibility tree (referred to as an accessibility node) has a role2, which indicates its semantic purpose. Native HTML elements are implicitly mapped to accessibility notes with their semantic roles—e.g. an <img> element becomes an accessibility node with the role of “image”.

<img> node translated into an image on the page and an accessibility node

Some nodes have a grouping role, helping users to meaningfully navigate the page. Others, such as <summary>, have a state (in the case of <summary>, collapsed or expanded). For more on Accessible Rich Internet Application (ARIA) role types, see MDN’s Using ARIA: Roles, states, and properties.

For more formal definitions of the accessibility tree see:

I also recommend How accessibility trees inform assistive tech by accessibility expert, Hidde de Vries (de Vries 2019).

References

Andrew, Rachel. 2015. “Modern CSS Layout, Power and Responsibility.” Rachel Andrew’s Blog. July 28, 2015. http://rachelandrew.co.uk/archives/2015/07/28/modern-css-layout-power-and-responsibility/.
———. 2018. “Best Practices With CSS Grid Layout.” Smashing Magazine. April 16, 2018. https://www.smashingmagazine.com/2018/04/best-practices-grid-layout/.
———. 2020. “Content Reordering.” web.dev. May 29, 2020. https://web.dev/content-reordering/.
Bailey, Eric. 2021. “Improving The Accessibility Of Your Markdown.” Smashing Magazine. September 28, 2021. https://www.smashingmagazine.com/2021/09/improving-accessibility-of-markdown/.
Boxhall, Alice, James Craig, Dominic Mazzoni, and Alexander Surkov. 2021. “The Accessibility Object Model: Explainer.” The Accessibility Object Model (AOM). WICG. https://wicg.github.io/aom/explainer.html.
Cooper, Michael, Zoë Bijl, James Nurthen, Matthew King, and JaEun Jemma Ku. 2021. WAI-ARIA Authoring Practices 1.2.” W3C note. W3C. https://www.w3.org/TR/2021/NOTE-wai-aria-practices-1.2-20211129/.
de Vries, Hidde. 2019. “How Accessibility Trees Inform Assistive Tech.” Mozilla Hacks. June 26, 2019. https://hacks.mozilla.org/2019/06/how-accessibility-trees-inform-assistive-tech.
———. 2022. “More to Give Than Just the Div: Semantics and How to Get Them Right.” hiddedevries.nl. January 23, 2022. https://hiddedevries.nl/en/blog/2022-01-23-more-to-give-than-just-the-div-semantics-and-how-to-get-them-right.
Etemad, Elika, Rossen Atanassov, and Tab Atkins Jr. 2020. CSS Grid Layout Module Level 2.” Candidate Recommendation. W3C. https://www.w3.org/TR/2020/CRD-css-grid-2-20201218/.
House, Chris. 2021. “A Complete Guide to Grid.” CSS-Tricks. May 12, 2021. https://css-tricks.com/snippets/css/complete-guide-grid/.
Kearney, Meggin, Dave Gash, and Alice Boxhall. 2020. “The Accessibility Tree.” Google Developers Web Fundamentals. https://developers.google.com/web/fundamentals/accessibility/semantics-builtin/the-accessibility-tree.
Matuzović, Manuel. 2019a. “The Dark Side of the Grid (Part 1).” Web Development Blog. February 7, 2019. https://www.matuzo.at/blog/the-dark-side-of-the-grid/.
———. 2019b. “The Dark Side of the Grid (Part 2).” Web Development Blog. May 11, 2019. https://www.matuzo.at/blog/the-dark-side-of-the-grid-part-2/.
MDN Contributors. 2021a. CSS Grid Layout and Accessibility.” MDN Web Docs. https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/CSS_Grid_Layout_and_Accessibility.
———. 2021b. “Introduction to the DOM - Web APIs.” MDN Web Docs. https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction.
———. 2021c. “Accessibility Tree.” MDN Web Docs Glossary. https://developer.mozilla.org/en-US/docs/Glossary/Accessibility_tree.
———. 2022. HTML: A Good Basis for Accessibility.” MDN Web Docs. https://developer.mozilla.org/en-US/docs/Learn/Accessibility/HTML.
O’Connor, Joshue, Michael Cooper, Andrew Kirkpatrick, and Alastair Campbell. 2018. “Web Content Accessibility Guidelines (WCAG) 2.1.” W3C recommendation. W3C. https://www.w3.org/TR/WCAG21/.
White, Kevin, Shadi Abou-Zahra, and Shawn Lawton Henry. 2019. “Developing for Web Accessibility Tips for Getting Started.” https://www.w3.org/WAI/tips/developing/.

Footnotes

  1. As defined in the WCAG 2.1 Standard, assistive technology is: “hardware and/or software that acts as a user agent, or along with a mainstream user agent, to provide functionality to meet the requirements of users with disabilities that go beyond those offered by mainstream user agents” (O’Connor et al. 2018).↩︎

  2. See MDN docs on WAI-ARIA roles for details.↩︎