Ensuring Proper HTML Semantics for Assistive Technologies

Tutorial 2 of 5

Ensuring Proper HTML Semantics for Assistive Technologies

Introduction

In this tutorial, we will be focusing on creating HTML content that is easily accessible and readable by assistive technologies, such as screen readers. We will explore the importance of HTML semantics and how it impacts the interpretation of web content.

You will learn:
- How to ensure your HTML content is semantically correct
- How to use semantic elements and attributes in HTML
- How to test your content for accessibility

Prerequisites:
- Basic understanding of HTML and CSS
- Familiarity with web development tools

Step-by-Step Guide

HTML semantics refer to the meaning of the HTML content. Semantically correct HTML helps assistive technologies like screen readers understand your content better.

Semantic Elements

Semantic elements are HTML elements that clearly describe their content. Examples include <header>, <footer>, <nav>, <main>, and <article>. Always use semantic elements where possible.

<!-- Example of semantic elements -->
<header>
  <h1>My Website</h1>
  <nav>
    <ul>
      <li><a href="#home">Home</a></li>
      <li><a href="#about">About</a></li>
    </ul>
  </nav>
</header>
<main>
  <article>
    <h2>Welcome to my website</h2>
    <!-- Content here -->
  </article>
</main>
<footer>
  Copyright © 2022
</footer>

In the example above, the <header> element contains the heading of our website and navigation menu. The <main> tag contains the main content of our website, represented by the <article> element. The <footer> element contains our copyright notice.

ARIA Attributes

ARIA (Accessible Rich Internet Applications) attributes provide additional semantics to HTML elements and improve accessibility. They provide extra information about an element's role, state, and properties.

<button aria-label="Close dialog">X</button>

In the example above, aria-label attribute is used to provide a description to the button. It tells the screen reader to read "Close dialog" instead of "X".

Code Examples

Below are a few more examples of using semantic HTML and ARIA attributes.

Using alt attribute for images

<!-- Good practice -->
<img src="dog.jpg" alt="A brown dog sitting in the grass.">

<!-- Bad practice -->
<img src="dog.jpg">

In the good practice example, the alt attribute provides a description of the image. In the bad practice example, there is no alt attribute so screen readers can't provide a description of the image.

Using role attribute

<div role="navigation">
  <!-- Navigation menu here -->
</div>

In this example, the role attribute tells screen readers that the <div> is being used as a navigation element.

Summary

We have covered how to use semantic elements and ARIA attributes to create accessible HTML content. The next step is to familiarize yourself with other ARIA attributes and how to use them.

Additional resources:
- ARIA in HTML
- Web Accessibility Guide

Practice Exercises

  1. Create an HTML document using only semantic elements.
  2. Add ARIA attributes to the document from exercise 1.
  3. Test your document for accessibility.

Remember to use alt attributes for images, label attributes for form elements, and role attributes where necessary. Always test your documents with a screen reader to ensure they are accessible.