Replies: 1 comment
-
Here is a quick guide when deciding which HTML element to use in your implementation: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
A great deal of web content can be made accessible just by making sure the correct HTML elements are used for the correct purpose at all times.
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.
You might wonder why this is so important. After all, you can use a combination of CSS and JavaScript to make just about any HTML element behave in whatever way you want. For example, a control button to play a video on your site could be marked up like this:
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:
<div>
s, etc., so your documents will be more findable by customers.Let's get on and look at accessible HTML in more detail.
Good semantics
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.
Text content
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:
If you try navigating through this, you'll see that this is pretty easy to navigate:
People sometimes write headings, paragraphs, etc. using presentational HTML and line breaks, something like the following:
If you try this on a screen reader, you'll not have a very good experience — the screen reader hasn't got anything to use as reference, 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.
There are other issues too beyond accessibility — it is harder to style the content using CSS, or manipulate it with JavaScript, for example, because there are no elements to use as selectors.
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:
Page Layouts
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.
If you try a more modern structure 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).
But a better way when creating layouts is to use HTML5 semantic elements as seen here:
By leveraging semantic HTML5 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.UI controls
By UI controls, we mean the main parts of web documents that users interact with — most commonly buttons, links, and form controls. In this section, we'll look at the basic accessibility concerns to be aware of when creating such controls.
One key aspect of the accessibility of UI controls is that by default, browsers allow them to be manipulated by the keyboard. The focused elements are given a highlighted default style in every browser (it differs slightly between different browsers) so that you can tell what element is focused.
You can then press Enter/Return to follow a focused link or press a button (we've included some JavaScript to make the buttons alert a message), or start typing to enter text in a text input. Other form elements have different controls; for example, the
<select>
element can have its options displayed and cycled between using the up and down arrow keys.You essentially get this behavior for free, just by using the appropriate elements, e.g.
This means using links, buttons, form elements, and labels appropriately (including the
<label>
element for form controls).However, it is again the case that people sometimes do strange things with HTML. For example, you sometimes see buttons marked up using
<div>
s, for example:But using such code is not advised — you immediately lose the native keyboard accessibility you would have had if you'd just used
<button>
elements, plus you don't get any of the default CSS styling that buttons get.Building keyboard accessibility back in
Adding such advantages back in takes a bit of work. Here we've given our fake
tabindex="0"
:Basically, the
tabindex
attribute is primarily intended to allow tabbable elements to have a custom tab order (specified in positive numerical order), instead of just being tabbed through in their default source order. This is nearly always a bad idea, as it can cause major confusion. Use it only if you really need to, for example, if the layout shows things in a very different visual order to the source code, and you want to make things work more logically. There are two other options fortabindex
:tabindex="0"
— as indicated above, this value allows elements that are not normally tabbable to become tabbable. This is the most useful value of tabindex.tabindex="-1"
— this allows not normally tabbable elements to receive focus programmatically, e.g., via JavaScript, or as the target of links.While the above addition allows us to tab to the buttons, it does not allow us to activate them via the Enter/Return key. To do that, we had to add the following bit of JavaScript trickery:
Here we add a listener to the document object to detect when a button has been pressed on the keyboard. We check what button was pressed via the event object's key property; if the key pressed is Enter/Return, we run the function stored in the button's onclick handler using document.activeElement.click(). activeElement which gives us the element that is currently focused on the page.
This is a lot of extra hassle to build the functionality back in. And there's bound to be other problems with it. Better to just use the right element for the right job in the first place.
Summary
You should now be well-versed in writing accessible HTML for most occasions. The WAI-ARIA basics article will help to fill gaps in this knowledge, but this article has taken care of the basics.
This document is a slimmed down version of Mozilla's HTML: A good basis for accessibility.
Beta Was this translation helpful? Give feedback.
All reactions