Accessibility - HTML - A good basis for accessibility - 1
Information drawn from
A great deal of web content can be made accessible just by making sure the correct Hypertext Markup Language elements are used for the correct purpose at all times. This article looks in detail at how HTML can be used to ensure maximum accessibility.
Prerequisites: Basic computer literacy, a basic understanding of HTML (see Introduction to HTML), and an understanding of what accessibility is. Objective: To gain familiarity with the features of HTML that have accessibility benefits and how to use them appropriately in your web documents.
HTML and accessibility
As you learn more about HTML — read more resources, look at more examples, etc. — you’ll keep seeing a common theme: the importance of using semantic HTML (sometimes called POSH, or Plain Old Semantic HTML). This means using the correct HTML elements for their intended purpose as much as possible.
But as you’ll see in greater detail later on, it makes sense to use the correct element for the job:
Not only do HTML
<button> s have some suitable styling applied by default (which you will probably want to override), they also have built-in keyboard accessibility — users can navigate between buttons using the Tab key and activate their selection using Return or Enter.
Semantic HTML doesn’t take any longer to write than non-semantic (bad) markup if you do it consistently from the start of your project. Even better, semantic markup has other benefits beyond accessibility:
- Easier to develop with — as mentioned above, you get some functionality for free, plus it is arguably easier to understand.
- Better on mobile — semantic HTML is arguably lighter in file size than non-semantic spaghetti code, and easier to make responsive.
- Good for SEO — search engines give more importance to keywords inside headings, links, etc. than keywords included in non-semantic <div>s, etc., so your documents will be more findable by customers.
Let’s get on and look at accessible HTML in more detail.
Note: It is a good idea to have a screen reader set up on your local computer so that you can do some testing of the examples shown below. See our Screen readers guide for more details.
We’ve already talked about the importance of proper semantics, and why we should use the right HTML element for the job. This cannot be ignored, as it is one of the main places that accessibility is badly broken if not handled properly.
Out there on the web, the truth is that people do some very strange things with HTML markup. Some abuses of HTML are due to legacy practices that have not been completely forgotten, and some are just plain ignorance. Whatever the case, you should replace such bad code.
Sometimes you are not in the position to get rid of lousy markup — your pages might be generated by some kind of server-side framework over which you don’t have full control, or you might have third party content on your page (such as ad banners) over which you have no control.
The goal isn’t “all or nothing”; every improvement you can make will help the cause of accessibility.
One of the best accessibility aids a screen reader user can have is an excellent content structure with headings, paragraphs, lists, etc. An excellent semantic example might look something like the following:
<h1>My heading</h1> <p>This is the first section of my document.</p> <p>I'll add another paragraph here too.</p> <ol> <li>Here is</li> <li>a list for</li> <li>you to read</li> </ol> <h2>My subheading</h2> <p>This is the first subsection of my document. I'd love people to be able to find this content!</p> <h2>My 2nd subheading</h2> <p>This is the second subsection of my content. I think is more interesting than the last one.</p>
We’ve prepared a version with longer text for you to try out with a screen reader (see good-semantics.html). If you try navigating through this, you’ll see that this is pretty easy to navigate:
- The screen reader reads each header out as you progress through the content, notifying you what a heading is, what is a paragraph, etc.
- It stops after each element, letting you go at whatever pace is comfortable for you.
- You can jump to the next/previous heading in many screen readers.
- You can also bring up a list of all headings in many screen readers, allowing you to use them as a handy table of contents to find specific content.
People sometimes write headings, paragraphs, etc. using presentational HTML and line breaks, something like the following:
<font size="7">My heading</font> <br><br> This is the first section of my document. <br><br> I'll add another paragraph here too. <br><br> 1. Here is <br><br> 2. a list for <br><br> 3. you to read <br><br> <font size="5">My subheading</font> <br><br> This is the first subsection of my document. I'd love people to be able to find this content! <br><br> <font size="5">My 2nd subheading</font> <br><br> This is the second subsection of my content. I think is more interesting than the last one.
If you try our longer version out with a screen reader (see bad-semantics.html), you’ll not have a very good experience — the screen reader hasn’t got anything to use as signposts, so you can’t retrieve a useful table of contents, and the whole page is seen as a single giant block, so it is just read out in one go, all at once.
Using clear language
The language you use can also affect accessibility.In general, you should use clear language that is not overly complex and doesn’t use unnecessary jargon or slang terms. This not only benefits people with cognitive or other disabilities; it benefits readers for whom the text is not written in their first language, younger people … everyone, in fact! Apart from this, you should try to avoid using language and characters that don’t get read out clearly by the screen reader. For example:
- Don’t use dashes if you can avoid it. Instead of writing 5–7, write 5 to 7.
- Expand abbreviations — instead of writing Jan, write January.
- Expand acronyms, at least once or twice. Instead of writing HTML in the first instance, write Hypertext Markup Language.
In the bad old days, people used to create page layouts using HTML tables — using different table cells to contain the header, footer, sidebar, main content column, etc. This is not a good idea because a screen reader will likely give out confusing readouts, especially if the layout is complex and has many nested tables.
Try our example table-layout.html example, which looks something like this:
<table width="1200"> <!-- main heading row --> <tr id="heading"> <td colspan="6"> <h1 align="center">Header</h1> </td> </tr> <!-- nav menu row --> <tr id="nav" bgcolor="#ffffff"> <td width="200"> <a href="#" align="center">Home</a> </td> <td width="200"> <a href="#" align="center">Our team</a> </td> <td width="200"> <a href="#" align="center">Projects</a> </td> <td width="200"> <a href="#" align="center">Contact</a> </td> <td width="300"> <form width="300"> <input type="search" name="q" placeholder="Search query" width="300"> </form> </td> <td width="100"> <button width="100">Go!</button> </td> </tr> <!-- spacer row --> <tr id="spacer" height="10"> <td> </td> </tr> <!-- main content and aside row --> <tr id="main"> <td id="content" colspan="4" bgcolor="#ffffff"> <!-- main content goes here --> </td> <td id="aside" colspan="2" bgcolor="#ff80ff" valign="top"> <h2>Related</h2> <!-- aside content goes here --> </td> </tr> <!-- spacer row --> <tr id="spacer" height="10"> <td> </td> </tr> <!-- footer row --> <tr id="footer" bgcolor="#ffffff"> <td colspan="6"> <p>©Copyright 2050 by nobody. All rights reversed.</p> </td> </tr> </table>
If you try to navigate this using a screen reader, it will probably tell you that there’s a table to be looked at (although some screen readers can guess the difference between table layouts and data tables). You’ll then likely (depending on which screen reader you’re using) have to go down into the table as an object and look at its features separately, then get out of the table again to carry on navigating the content.
Table layouts are a relic of the past — they made sense back when CSS support was not widespread in browsers, but now they just create confusion for screen reader users. Additionally, their source code requires more markup, which makes them less flexible and more difficult to maintain. You can verify these claims by comparing your previous experience with a more modern website structure example, which could look something like this:
<header> <h1>Header</h1> </header> <nav> <!-- main navigation in here --> </nav> <!-- Here is our page's main content --> <main> <!-- It contains an article --> <article> <h2>Article heading</h2> <!-- article content in here --> </article> <aside> <h2>Related</h2> <!-- aside content in here --> </aside> </main> <!-- And here is our main footer that is used across all the pages of our website --> <footer> <!-- footer content in here --> </footer>
If you try our more modern structure example with a screen reader, you’ll see that the layout markup no longer gets in the way and confuses the content readout. It is also much leaner and smaller in terms of code size, which means easier to maintain code, and less bandwidth for the user to download (particularly prevalent for those on slow connections).
Another consideration when creating layouts is using HTML5 semantic elements as seen in the above example (see content sectioning) — you can create a layout using only nested
<div> elements, but it is better to use appropriate sectioning elements to wrap your main navigation (
<nav>), footer (
<footer>), repeating content units (
<article>), etc. These provide extra semantics for screen readers (and other tools) to give users extra clues about the content they are navigating (see Screen Reader Support for new HTML5 Section Elements for an idea of what screen reader support is like).
Note: In addition to having good semantics and an attractive layout, your content should make logical sense in its source order — you can always place it where you want using CSS later on, but you should get the source order right to start with, so what screen reader users get read out to them will make sense.
Last update on 27 Jan 2022