Sticky Navigation with Proper Focus Management #
Description:
A sticky navigation menu remains fixed at the top of the viewport as the user scrolls, improving accessibility by keeping key navigation options always within reach. However, sticky navigation requires careful focus management, particularly when implementing elements like skip links, keyboard navigation, and clear focus indicators.
This example provides an accessible sticky navigation menu with proper focus management for both keyboard and screen reader users.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Accessible Sticky Navigation with Focus Management</title>
<style>
/* Basic reset and styling for body and main content */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
main {
padding: 20px;
margin-top: 60px; /* Offset for sticky nav */
}
/* Skip link styling */
.skip-link {
position: absolute;
top: -40px;
left: 10px;
background-color: #005fcc;
color: #ffffff;
padding: 8px;
border-radius: 5px;
z-index: 1000;
text-decoration: none;
font-size: 1em;
}
.skip-link:focus {
top: 10px;
outline: 2px solid #ffbf00;
}
/* Sticky navigation styles */
.sticky-nav {
position: fixed;
top: 0;
width: 100%;
background-color: #333;
color: #fff;
display: flex;
justify-content: space-around;
padding: 15px 0;
z-index: 100;
}
.sticky-nav a {
color: #ffffff;
text-decoration: none;
padding: 8px 12px;
font-size: 1em;
}
.sticky-nav a:focus, .sticky-nav a:hover {
background-color: #555;
outline: 2px solid #ffffff;
}
</style>
</head>
<body>
<!-- Skip to Main Content Link -->
<a href="#main-content" class="skip-link">Skip to Main Content</a>
<!-- Sticky Navigation Bar -->
<nav class="sticky-nav" aria-label="Primary Navigation">
<a href="#home" tabindex="0">Home</a>
<a href="#about" tabindex="0">About</a>
<a href="#services" tabindex="0">Services</a>
<a href="#contact" tabindex="0">Contact</a>
</nav>
<!-- Main Content Area -->
<main id="main-content">
<section id="home">
<h2>Home</h2>
<p>Welcome to our website. We offer a variety of services and products tailored to your needs.</p>
</section>
<section id="about">
<h2>About Us</h2>
<p>Learn more about our company and our team of dedicated professionals.</p>
</section>
<section id="services">
<h2>Our Services</h2>
<p>Explore the services we offer to help you achieve your goals.</p>
</section>
<section id="contact">
<h2>Contact Us</h2>
<p>Get in touch with us to learn more about our offerings and to discuss your requirements.</p>
</section>
</main>
</body>
</html>
Explanation #
- Skip Link for Quick Access to Main Content:
- The skip link (
<a href="#main-content" class="skip-link">Skip to Main Content</a>) is positioned at the top of the page. - When focused (e.g., by pressing
Tab), it becomes visible, allowing keyboard users to jump directly to the main content area, bypassing the sticky navigation. - This helps users avoid repeatedly navigating through the sticky navigation links every time they visit a new page section.
- The skip link (
- Sticky Navigation Bar with Proper Focus Management:
- The navigation bar is fixed at the top using
position: fixed, ensuring it remains visible as users scroll down the page. - Links in the navigation bar have a clear focus style, making them distinguishable when navigated by keyboard. The background color changes on focus, and an outline appears, making it clear which link is active.
aria-label="Primary Navigation"on the<nav>element clarifies the purpose of this section for screen readers, improving accessibility.
- The navigation bar is fixed at the top using
- Content Structure with Anchor Links:
- Each section in the main content area has an
idthat corresponds to the navigation links (#home,#about,#services,#contact). This allows users to jump directly to each section by clicking or pressingEnteron the navigation links. - The
maintag withid="main-content"acts as the primary content area, making it a natural target for the skip link.
- Each section in the main content area has an
- Keyboard Navigation and Focus Management:
- The navigation links are focusable by default, allowing keyboard users to access each link.
- When navigating with
Tab, the focus moves from the skip link to each navigation link, then to the main content area, maintaining a logical focus order. - Focus styles provide visual feedback, allowing users to track their position within the sticky navigation bar.
Key Takeaways #
- Use a Skip Link: Provide a skip link to allow users to bypass sticky navigation and jump directly to the main content.
- Fixed Positioning with Accessible Focus Styles: Use
position: fixedfor the sticky navigation and provide visible focus indicators for each link to support keyboard navigation. - ARIA Labeling for Screen Readers: Use
aria-labelto clarify the navigation’s purpose, making it more accessible for screen reader users. - Logical Focus Order: Ensure that the navigation links and skip link follow a logical tab order, making it intuitive for keyboard-only users.
Screen reader: NVDA
Video: