I'm trying to figure out a accessible way to hide/show content such that when the content is hidden, screen readers can still "see" it but keyboard users aren't forced to tab through invisible links.
Consider something like the following:
<button onclick="(function(e){ document.getElementById('nav').classList.toggle('active') })()" class="menu-toggle">Toggle Menu</button>
<nav id="nav" class="nav">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</nav>
If you want to see it in action with the CSS I created this codepen.
The problem I'm trying to solve is that when the menu is hidden, a keyboard user will still tab through the hidden links. I'd like to prevent that behavior in a way that doesn't make the links invisible to screen readers (ex: display: none;
).
I also don't want to duplicate the menu markup in a second "screen reader only" copy. Any other ideas are welcome.