Why it's worth reading: Inspecting the accessibility tree lets designers and developers predict what a screen reader will announce, and check names, roles and states against WCAG 4.1.2.
Summary by Mosaic, Designed's AI · may contain mistakes
There's a common misconception that when browsing the web using a screen reader, it's reading the actual screen. This is not really surprising, as the name suggests that's exactly what it's doing. And, this is what early screen readers did, so, we can't really blame anybody for thinking that!
There's also a slightly more informed misconception, that screen readers read the DOM (Document Object Model). Again, once upon a time they did, and some might still try to as a fallback. But with modern screen readers, they actually read something called the accessibility tree. And, using the browser's built-in developer tools, you can read it too.
Once you know what the accessibility tree is, and understand how to inspect it, a lot of the mystery around accessibility goes away. Screen readers stop being an unpredictable black box, and start being something that is a lot more predictable.
In this post we explore what the accessibility tree is, how the browser builds it, and how you can use it to better predict what a screen reader will actually announce.
How the accessibility tree works
When a browser loads a page, it builds several models which allow it to display, style, and track the states of the various elements you've defined in your code.
It takes your HTML and it builds the DOM. This acts as the source of truth, and the blueprint for everything else.
The browser also takes your CSS and builds the CSSOM (CSS Object Model). It uses this to decide how elements should be styled, for example what colours or fonts to apply.
Then, it combines the DOM and the CSSOM to create two more models:
The layout tree: the elements which it needs to visibly show on the page
The accessibility tree: the elements it needs to make available to assistive technologies, like screen readers
Both the layout tree and the accessibility tree are just a translation of the DOM, but filtered for a particular context.