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
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 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 (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".
Below are a few more examples of using semantic HTML and ARIA attributes.
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.
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.
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
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.