30
loading...
This website collects cookies to deliver better user experience
Enter
on that hidden link - the window's focus skips past the navigation header and puts focus into the hero section. In the case of GitHub, after you click "Skip to content" the next Tab
will focus on the email signup form rather than the logo in their navigation header.<a>
that is the hidden link itself. We want this to work whether JavaScript is enabled or not, so let's make sure to progressively enhance this feature.<body>
<a href="#start-of-content" class="sr-only sr-only-focusable"
>Skip to content</a
>
<header>
<!-- Your header content goes here -->
</header>
<div id="start-of-content" class="sr-only"></div>
<section>
<!-- Your awesome hero section -->
</section>
</body>
div
immediately after the header.<a>
doesn't necessarily have to be the first child of the page's body
, but it's important that it is the first focusable element in the DOM..sr-only {
border: 0;
padding: 0;
margin: 0;
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
clip: rect(1px 1px 1px 1px); /* IE6, IE7 - a 0 height clip, off to the bottom right of the visible 1px box */
clip: rect(1px, 1px, 1px, 1px); /*maybe deprecated but we need to support legacy browsers */
clip-path: inset(50%); /*modern browsers, clip-path works inwards from each corner*/
white-space: nowrap; /* added line to stop words getting smushed together (as they go onto seperate lines and some screen readers do not understand line feeds as a space */
}
.sr-only.sr-only-focusable:focus {
width: auto;
height: auto;
padding: 0;
margin: 0;
overflow: visible;
clip: auto;
clip-path: initial;
white-space: normal;
}
Update: .sr-only
class updated based on some excellent feedback from Inclusivity Hub on dev.to
sr-only-focusable
before, that's just an extra helper class that allows the element to be visible only when it has :focus
.Update: Looking for a no-JS solution? Skip the JS logic and point your "Skip to Content" link straight to your <main id="main">
block!
href="#start-of-content"
will update the URL in the address bar. That's not always ideal and could even break your page if you are using hash routing.function onSkipToContent(event) {
// Stop the event's default behavior
// In this case, don't let it actually change the page's URL
event.preventDefault();
// Find the hidden target div
const target = document.getElementById("start-of-content");
if (!target) {
return;
}
// Find the next element in the DOM
const content = target.nextElementSibling;
if (content instanceof HTMLElement) {
// Make sure the content div can't be tabbed to again, the give it focus
content.setAttribute("tabindex", "-1");
content.focus();
}
}
// Find the hidden "Skip to content" link and hook up tje click event
const link = document.querySelector('a[href="#start-of-content"]');
if (link) {
link.addEventListener("click", onSkipToContent());
}
nextElementSibling
? Well it may not be necessary, but we added that div
as a bookmark and don't want it to actually take focus in the window. Instead, we want to find that bookmark and give focus to whatever comes next in the DOM.30