Navigation Setup

Tutorial 4 of 4

1. Introduction

In this tutorial, we will tackle the topic of setting up keyboard navigation in your HTML documents. Keyboard navigation is a critical aspect of web accessibility, and it allows users to navigate your website using only their keyboard. This feature is especially significant for users with motor disabilities, but it also benefits all users by providing an alternative navigation method.

Upon completion of this tutorial, you will be able to:
- Understand the importance of keyboard navigation
- Implement keyboard navigation in your HTML documents
- Test your website to ensure it is fully keyboard navigable

Prerequisites:
- Basic understanding of HTML and CSS
- Basic knowledge of JavaScript is helpful but not necessary

2. Step-by-Step Guide

Keyboard navigation relies on the use of the 'tabindex' attribute in HTML. By default, interactive elements like links and form inputs are in the keyboard navigation flow. Non-interactive elements can be included by assigning a positive 'tabindex'.

Here's a detailed step-by-step guide:

  1. Determine the Navigation Order: The order in which elements receive focus should make sense to the user. Generally, this is the order in which elements appear in the HTML.

  2. Assigning Tabindex: Assign a 'tabindex' of 0 to elements you want to include in the keyboard navigation. These elements will be focused in the order they appear in the HTML.

  3. Provide Visual Feedback: Use CSS to style the :focus pseudo-class for your elements. This provides a visual indication of the currently focused element.

  4. Test Your Navigation: Use the Tab key to navigate through your site. Make sure the order makes sense and all necessary elements can receive focus.

3. Code Examples

  1. Simple Navigation
<!-- This link is in the keyboard navigation flow by default -->
<a href="#">Default Link</a>

<!-- This div is not focusable by default -->
<div>Some text</div>

<!-- We can make it focusable by adding tabindex="0" -->
<div tabindex="0">Focusable text</div>
  1. Styling the Focus
/* Style the :focus pseudo-class */
:focus {
    outline: 2px solid blue;
}

4. Summary

In this tutorial, we covered the basics of setting up keyboard navigation in HTML documents. We learned about the 'tabindex' attribute and how to use it to include elements in the keyboard navigation flow. We also discussed the importance of providing visual feedback for the focused element.

As a next step, consider learning more about accessibility, such as ARIA roles and attributes, and how to make your site more accessible to screen readers. MDN and W3Schools offer great resources for this.

5. Practice Exercises

  1. Exercise 1: Create a simple web page with various elements. Ensure all interactive elements can be navigated using the keyboard.

  2. Exercise 2: Add non-interactive elements to the navigation flow using the 'tabindex' attribute.

Solutions:

  1. For Exercise 1, you should have a HTML file with interactive elements such as <a>, <button>, <input>, etc. Test your page by using the Tab key to navigate.

  2. For Exercise 2, add tabindex="0" to any non-interactive elements you want to include in the navigation flow, such as <div> or <span>.

Remember, practice is key to mastering these concepts. So, keep creating and testing your web pages. Happy coding!