Academy / Advanced Level: HTML and CSS
Accessibility and SEO Best Practices
Keyboard Navigation and Focus Management
For many users with disabilities, the keyboard is the primary method of navigating a website. Ensuring that your site is fully keyboard navigable and that focus is managed correctly is critical for accessibility.
Best Practices for Keyboard Navigation:
- Ensure that all interactive elements (buttons, links, forms) can be accessed via the Tab key.
- Use the
:focuspseudo-class to style focusable elements, making them visible when they are in focus.button:focus { outline: 3px solid #3498db; } - Manage focus when modals or dynamic content are triggered to ensure that users can still navigate effectively.
Focus Management Example:
<button id="open-modal">Open Modal</button>
<div id="modal" role="dialog" aria-hidden="true">
<button id="close-modal">Close Modal</button>
</div>
<script>
document.getElementById('open-modal').addEventListener('click', function () {
document.getElementById('modal').setAttribute('aria-hidden', 'false');
document.getElementById('close-modal').focus(); // Move focus to the modal
});
</script>