I'm currently focusing on accessibility for the sidebar I made for an App. It behaves as a "slide-out" meaning the content is still inside the dom, even when the sidebar is not visible. To ensure SR will not go through the sidebar when it's closed, I added a aria-hidden. Now, while adding aria-controls
and aria-expanded
to the button, i noticed this in the chrome DevTools (Elements > Accessibility
):
As the sidebar is aria-hiddened, the controls
attribute is not linking correctly (which makes sense)
Following this logic, it links well when the sidebar is opened
Is this a sign that something is off ? This missing link is worrying me. If so, how should I fix this ?
Here's a demo of what i have (excuse the poor code, it's just for demo purpuse)
const control = document.querySelector("#control")
control.addEventListener('click', () => {
const aside = document.querySelector("#sidebar")
let isSidebarHidden = aside.getAttribute('aria-hidden') === 'true'
aside.setAttribute("aria-hidden", !isSidebarHidden);
aside.classList.toggle("hidden")
control.setAttribute("aria-expanded", isSidebarHidden)
control.innerText = isSidebarHidden ? "close sidebar" : "Open sidebar"
})
aside {
height: 100px;
background: lightblue;
}
body {
display: flex;
}
.hidden {
visibility: hidden;
}
<body>
<aside aria-hidden="false" id="sidebar">
<div>Here comes the sidebar</div>
</aside>
<main>
<button id="control" aria-controls="sidebar" aria-expanded="true">
close sidebar
</button>
</main>
<body>
If the element isn't visible for anybody, you should use display:none rather than aria-hidden.
The attribute aria-hidden should be used when an element has to be visible on screen, but hidden to screen readers and other assistive tools. This normally occurrs in relatively rare and precises circunstances only, for relatively small/short HTML code portions. If the element is hidden for everybody, regardless of the context, screen reader or not, then you should use CSS display:none or visibility:hidden.
When using aria-hidden instead of display:none, the element is hidden to screen readers and other assistive tools, but technically still visible on screen and operating as normal. This in particular means that focusable children continue to get the focus when navigating with tab.
Incorrect usage of aria-hidden is the root of many other problems.
I have answered a similar question recently. I'm wondering why the tendance is to use aria-hidden instead of display:none, while it's a lot simpler and safer ?