Skip to main content Skip to docs navigation

Getting started with accessibility

Getting Started with Accessibility is a concise series introducing you to the practicalities of accessibility in web development. This guide will cover general best practices and tips for accessibility from the start in detail.

On this page

HTML: A good basis for accessibility

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: 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.

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:

html
                                        
                                            
                                                
                                                    <
                                                    div
                                                
                                                >
                                            
                                            Play video
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                        
                                    

But as you'll see in greater detail later on, it makes sense to use the correct element for the job:

html
                                        
                                            
                                                
                                                    <
                                                    button
                                                
                                                >
                                            
                                            Play video
                                            
                                                
                                                    </
                                                    button
                                                
                                                >
                                            
                                        
                                    

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 Space , 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:

  1. Easier to develop with — as mentioned above, you get some functionality for free, plus it is arguably easier to understand.
  2. Better on mobile — semantic HTML is arguably lighter in file size than non-semantic spaghetti code, and easier to make responsive.
  3. 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.

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:

html
                                        
                                            
                                                
                                                    <
                                                    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, which 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:

  1. The screen reader reads each header out as you progress through the content, notifying you what a heading is, what is a paragraph, etc.
  2. It stops after each element, letting you go at whatever pace is comfortable for you.
  3. You can jump to the next/previous heading in many screen readers.
  4. 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 line breaks and adding HTML elements purely for styling, something like the following:

html
                                        
                                            
                                                
                                                    <
                                                    span
                                                
                                                
                                                    style
                                                    
                                                        =
                                                        "
                                                        
                                                            font-size
                                                            :
                                                            3em
                                                        
                                                        "
                                                    
                                                
                                                >
                                            
                                            My heading
                                            
                                                
                                                    </
                                                    span
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    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
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    span
                                                
                                                
                                                    style
                                                    
                                                        =
                                                        "
                                                        
                                                            font-size
                                                            :
                                                            2.5em
                                                        
                                                        "
                                                    
                                                
                                                >
                                            
                                            My subheading
                                            
                                                
                                                    </
                                                    span
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    br
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    br
                                                
                                                />
                                            
                                            This is the first subsection of my document. I'd love people to be able to find
this content!

                                            
                                                
                                                    <
                                                    br
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    br
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    span
                                                
                                                
                                                    style
                                                    
                                                        =
                                                        "
                                                        
                                                            font-size
                                                            :
                                                            2.5em
                                                        
                                                        "
                                                    
                                                
                                                >
                                            
                                            My 2nd subheading
                                            
                                                
                                                    </
                                                    span
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    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.

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:

  • 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, then use the <abbr > tag to describe them.

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.

Try our example table-layout.html example, which looks something like this:

html
                                        
                                            
                                                
                                                    <
                                                    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"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                >
                                            
                                            Search
      
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    search"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    q"
                                                
                                                placeholder
                                                
                                                    =
                                                    "
                                                    Search query"
                                                
                                                width
                                                
                                                    =
                                                    "
                                                    300"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    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"
                                                
                                                >
                                            
                                            <!-- main content goes here -->
                                            
                                                
                                                    </
                                                    td
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    td
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    aside"
                                                
                                                colspan
                                                
                                                    =
                                                    "
                                                    2"
                                                
                                                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"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    td
                                                
                                                colspan
                                                
                                                    =
                                                    "
                                                    6"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            ©Copyright 1996 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:

html
                                        
                                            
                                                
                                                    <
                                                    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 notice that the layout markup no longer interferes with or causes confusion in the content readout. It is also much leaner and smaller in terms of code size, which means the code is easier to maintain and requires less bandwidth for users to download, making it particularly beneficial for those on slow connections.

Another consideration when creating layouts is using HTML 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.

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. Later articles on WAI-ARIA and multimedia will look at other aspects of UI accessibility.

One key aspect of the accessibility of UI controls is that by default, browsers allow them to be manipulated by the keyboard. You can try this out using our native-keyboard-accessibility.html example (see the source code ). Open this in a new tab, and try pressing the tab key; after a few presses, you should see the tab focus start to move through the different focusable elements. 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.

Three buttons with the text "Click me!", "Click me too!", and "And me!" inside them respectively. The third button has a blue outline around it to indicate current tab focus.

Note: You can enable an overlay that shows the page tabbing order in your developer tools. For more information see: Accessibility Inspector >Show web page tabbing order .

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.

Note: Different browsers may have different keyboard control options available. See Using native keyboard accessibility for more details.

You essentially get this behavior for free, just by using the appropriate elements, e.g.

html
                                        
                                            
                                                
                                                    <
                                                    h1
                                                
                                                >
                                            
                                            Links
                                            
                                                
                                                    </
                                                    h1
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            This is a link to 
                                            
                                                
                                                    <
                                                    a
                                                
                                                href
                                                
                                                    =
                                                    "
                                                    https://www.mozilla.org"
                                                
                                                >
                                            
                                            Mozilla
                                            
                                                
                                                    </
                                                    a
                                                
                                                >
                                            
                                            .
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            Another link, to the

                                            
                                                
                                                    <
                                                    a
                                                
                                                href
                                                
                                                    =
                                                    "
                                                    https://developer.mozilla.org"
                                                
                                                >
                                            
                                            Mozilla Developer Network
                                            
                                                
                                                    </
                                                    a
                                                
                                                >
                                            
                                            .

                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    h2
                                                
                                                >
                                            
                                            Buttons
                                            
                                                
                                                    </
                                                    h2
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    button
                                                
                                                data-message
                                                
                                                    =
                                                    "
                                                    This is from the first button"
                                                
                                                >
                                            
                                            Click me!
                                            
                                                
                                                    </
                                                    button
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    button
                                                
                                                data-message
                                                
                                                    =
                                                    "
                                                    This is from the second button"
                                                
                                                >
                                            
                                            Click me too!
                                            
                                                
                                                    </
                                                    button
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    button
                                                
                                                data-message
                                                
                                                    =
                                                    "
                                                    This is from the third button"
                                                
                                                >
                                            
                                            And me!
                                            
                                                
                                                    </
                                                    button
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    h2
                                                
                                                >
                                            
                                            Form
                                            
                                                
                                                    </
                                                    h2
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    form
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                >
                                            
                                            Fill in your name:
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    text"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    age"
                                                
                                                >
                                            
                                            Enter your age:
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    text"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    age"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    age"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    mood"
                                                
                                                >
                                            
                                            Choose your mood:
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    select
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    mood"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    mood"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Happy
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Sad
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Angry
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Worried
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    select
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    form
                                                
                                                >
                                            
                                        
                                    

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:

html
                                        
                                            
                                                
                                                    <
                                                    div
                                                
                                                data-message
                                                
                                                    =
                                                    "
                                                    This is from the first button"
                                                
                                                >
                                            
                                            Click me!
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    div
                                                
                                                data-message
                                                
                                                    =
                                                    "
                                                    This is from the second button"
                                                
                                                >
                                            
                                            Click me too!
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    div
                                                
                                                data-message
                                                
                                                    =
                                                    "
                                                    This is from the third button"
                                                
                                                >
                                            
                                            And me!
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                        
                                    

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. In the rare to non-existent case when you need to use a non-button element for a button, use the button role and implement all the default button behaviors, including keyboard and mouse button support.

Building keyboard accessibility back in

Adding such advantages back in takes a bit of work (you can see an example in our fake-div-buttons.html example — also see the source code ). Here we've given our fake <div > buttons the ability to be focused (including via tab) by giving each one the attribute tabindex="0" . We also include role="button" so screen reader users know they can focus on and interact with the element:

html
                                        
                                            
                                                
                                                    <
                                                    div
                                                
                                                data-message
                                                
                                                    =
                                                    "
                                                    This is from the first button"
                                                
                                                tabindex
                                                
                                                    =
                                                    "
                                                    0"
                                                
                                                role
                                                
                                                    =
                                                    "
                                                    button"
                                                
                                                >
                                            
                                            Click me!

                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    div
                                                
                                                data-message
                                                
                                                    =
                                                    "
                                                    This is from the second button"
                                                
                                                tabindex
                                                
                                                    =
                                                    "
                                                    0"
                                                
                                                role
                                                
                                                    =
                                                    "
                                                    button"
                                                
                                                >
                                            
                                            Click me too!

                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    div
                                                
                                                data-message
                                                
                                                    =
                                                    "
                                                    This is from the third button"
                                                
                                                tabindex
                                                
                                                    =
                                                    "
                                                    0"
                                                
                                                role
                                                
                                                    =
                                                    "
                                                    button"
                                                
                                                >
                                            
                                            And me!

                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                        
                                    

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 for tabindex :

  • 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:

js
                                        
                                            document.
                                            onkeydown
                                            =
                                            (
                                            e
                                            )
                                            =>
                                            {
                                            // The Enter/Return key
                                            if
                                            (
                                            e.
                                            key ===
                                            "Enter"
                                            )
                                            {
                                            document.
                                            activeElement.
                                            click
                                            (
                                            )
                                            ;
                                            }
                                            }
                                            ;
                                        
                                    

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.

Meaningful text labels

UI control text labels are very useful to all users, but getting them right is particularly important to users with disabilities.

You should make sure that your button and link text labels are understandable and distinctive. Don't just use "Click here" for your labels, as screen reader users sometimes get up a list of buttons and form controls. The following screenshot shows our controls being listed by VoiceOver on Mac.

List of form input labels being listed by VoiceOver software on Mac. This list contains meaningless labels like 'happy menu button` given to various form controls like button, textfield and link

Make sure your labels make sense out of context, read on their own, as well as in the context of the paragraph they are in. For example, the following shows an example of good link text:

html
                                        
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            Whales are really awesome creatures.

                                            
                                                
                                                    <
                                                    a
                                                
                                                href
                                                
                                                    =
                                                    "
                                                    whales.html"
                                                
                                                >
                                            
                                            Find out more about whales
                                            
                                                
                                                    </
                                                    a
                                                
                                                >
                                            
                                            .

                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                        
                                    

but this is bad link text:

html
                                        
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            Whales are really awesome creatures. To find out more about whales,

                                            
                                                
                                                    <
                                                    a
                                                
                                                href
                                                
                                                    =
                                                    "
                                                    whales.html"
                                                
                                                >
                                            
                                            click here
                                            
                                                
                                                    </
                                                    a
                                                
                                                >
                                            
                                            .

                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                        
                                    

Note: You can find a lot more about link implementation and best practices in our Creating hyperlinks article. You can also see some good and bad examples at good-links.html and bad-links.html .

Form labels are also important for giving you a clue about what you need to enter into each form input. The following seems like a reasonable enough example:

html
                                        
                                            Fill in your name: 
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    text"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                />
                                            
                                        
                                    

However, this is not so useful for disabled users. There is nothing in the above example to associate the label unambiguously with the form input and make it clear how to fill it in if you cannot see it. If you access this with some screen readers, you may only be given a description along the lines of "edit text."

The following is a much better example:

html
                                        
                                            
                                                
                                                    <
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                >
                                            
                                            Fill in your name:
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    text"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                        
                                    

With code like this, the label will be clearly associated with the input; the description will be more like "Fill in your name: edit text."

A good form label that reads 'Fill in your name' is given to a text input form control.

As an added bonus, in most browsers associating a label with a form input means that you can click the label to select or activate the form element. This gives the input a bigger hit area, making it easier to select.

Note: You can see some good and bad form examples in good-form.html and bad-form.html .

You can find a nice explanation of the importance of proper text labels, and how to investigate text label issues using the Firefox Accessibility Inspector , in the following video:

Accessible data tables

A basic data table can be written with very simple markup, for example:

html
                                        
                                            
                                                
                                                    <
                                                    table
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    tr
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    td
                                                
                                                >
                                            
                                            Name
                                            
                                                
                                                    </
                                                    td
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    td
                                                
                                                >
                                            
                                            Age
                                            
                                                
                                                    </
                                                    td
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    td
                                                
                                                >
                                            
                                            Pronouns
                                            
                                                
                                                    </
                                                    td
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    tr
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    tr
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    td
                                                
                                                >
                                            
                                            Gabriel
                                            
                                                
                                                    </
                                                    td
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    td
                                                
                                                >
                                            
                                            13
                                            
                                                
                                                    </
                                                    td
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    td
                                                
                                                >
                                            
                                            he/him
                                            
                                                
                                                    </
                                                    td
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    tr
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    tr
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    td
                                                
                                                >
                                            
                                            Elva
                                            
                                                
                                                    </
                                                    td
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    td
                                                
                                                >
                                            
                                            8
                                            
                                                
                                                    </
                                                    td
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    td
                                                
                                                >
                                            
                                            she/her
                                            
                                                
                                                    </
                                                    td
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    tr
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    tr
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    td
                                                
                                                >
                                            
                                            Freida
                                            
                                                
                                                    </
                                                    td
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    td
                                                
                                                >
                                            
                                            5
                                            
                                                
                                                    </
                                                    td
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    td
                                                
                                                >
                                            
                                            she/her
                                            
                                                
                                                    </
                                                    td
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    tr
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    table
                                                
                                                >
                                            
                                        
                                    

But this has problems — there is no way for a screen reader user to associate rows or columns together as groupings of data. To do this, you need to know what the header rows are and if they are heading up rows, columns, etc. This can only be done visually for the above table (see bad-table.html and try the example out yourself).

Now have a look at our punk bands table example — you can see a few accessibility aids at work here:

  • Table headers are defined using <th > elements — you can also specify if they are headers for rows or columns using the scope attribute. This gives you complete groups of data that can be consumed by screen readers as single units.
  • The <caption > element and the <table > element's summary attribute both do similar jobs — they act as alt text for a table, giving a screen reader user a useful quick summary of the table's contents. The <caption > element is generally preferred as it makes it's content accessible to sighted users too, who might also find it useful. You don't really need both.

Note: See our HTML table advanced features and accessibility article for more details about accessible data tables.

Text alternatives

Whereas textual content is inherently accessible, the same cannot necessarily be said for multimedia content — image and video content cannot be seen by visually-impaired people, and audio content cannot be heard by hearing-impaired people. We cover video and audio content in detail in the Accessible multimedia , but for this article we'll look at accessibility for the humble <img > element.

We have a simple example written up, accessible-image.html , which features four copies of the same image:

html
                                        
                                            
                                                
                                                    <
                                                    img
                                                
                                                src
                                                
                                                    =
                                                    "
                                                    dinosaur.png"
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    img
                                                
                                                src
                                                
                                                    =
                                                    "
                                                    dinosaur.png"
                                                
                                                alt
                                                
                                                    =
                                                    "
                                                    A red Tyrannosaurus Rex: A two legged dinosaur standing upright like a human, with small arms, and a large head with lots of sharp teeth."
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    img
                                                
                                                src
                                                
                                                    =
                                                    "
                                                    dinosaur.png"
                                                
                                                alt
                                                
                                                    =
                                                    "
                                                    A red Tyrannosaurus Rex: A two legged dinosaur standing upright like a human, with small arms, and a large head with lots of sharp teeth."
                                                
                                                title
                                                
                                                    =
                                                    "
                                                    The Mozilla red dinosaur"
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    img
                                                
                                                src
                                                
                                                    =
                                                    "
                                                    dinosaur.png"
                                                
                                                aria-labelledby
                                                
                                                    =
                                                    "
                                                    dino-label"
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    dino-label"
                                                
                                                >
                                            
                                            The Mozilla red Tyrannosaurus Rex: A two legged dinosaur standing upright like
a human, with small arms, and a large head with lots of sharp teeth.

                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                        
                                    

The first image, when viewed by a screen reader, doesn't really offer the user much help — VoiceOver for example reads out "/dinosaur.png, image". It reads out the filename to try to provide some help. In this example the user will at least know it is a dinosaur of some kind, but often files may be uploaded with machine-generated file names (e.g. from a digital camera) and these file names would likely provide no context to the image's content.

Note: This is why you should never include text content inside an image — screen readers can't access it. There are other disadvantages too — you can't select it and copy/paste it. Just don't do it!

When a screen reader encounters the second image, it reads out the full alt attribute — "A red Tyrannosaurus Rex: A two legged dinosaur standing upright like a human, with small arms, and a large head with lots of sharp teeth.".

This highlights the importance of not only using meaningful file names in case so-called alt text is not available, but also making sure that alt text is provided in alt attributes wherever possible.

Note that the contents of the alt attribute should always provide a direct representation of the image and what it conveys visually. The alt should be brief and concise and include all the information conveyed in the image that is not duplicated in the surrounding text.

The content of the alt attribute for a single image differs based on the context. For example, if the photo of Fluffy is an avatar next to a review for Yuckymeat dog food, alt="Fluffy" is appropriate. If the photo is part of Fluffy's adoption page for the animal rescue society, information conveyed in the image that is relevant for a prospective dog parent that is not duplicated in the surrounding text should be included. A longer description, such as alt="Fluffy, a tri-color terrier with very short hair, with a tennis ball in her mouth." is appropriate. As the surrounding text likely has Fluffy's size and breed, that is not included in the alt . However, as the dog's biography likely doesn't include hair length, colors, or toy preferences, which the potential parent needs to know, it is included. Is the image outdoors, or does Fluffy have a red collar with a blue leash? Not important in terms of adopting the pet and therefore not included. All information image conveys that a sited user can access and is relevant to the context is what needs to be conveyed; nothing more. Keep it short, precise, and useful.

Any personal knowledge or extra description shouldn't be included here, as it is not useful for people who have not seen the image before. If the ball is Fluffy's favorite toy or if the sited user can't know that from the image, then don't include it.

One thing to consider is whether your images have meaning inside your content, or whether they are purely for visual decoration, and thus have no meaning. If they are decorative, it is better to write an empty text as a value for alt attribute (see Empty alt attributes ) or to just include them in the page as CSS background images.

Note: Read Images in HTML and Responsive images for a lot more information about image implementation and best practices.

If you do want to provide extra contextual information, you should put it in the text surrounding the image, or inside a title attribute, as shown above. In this case, most screen readers will read out the alt text, the title attribute, and the filename. In addition, browsers display title text as tooltips when moused over.

Screenshot of a red Tyrannosaurus Rex with the text "The mozilla red dinosaur" displayed as tooltip on mouseover.

Let's have another quick look at the fourth method:

html
                                        
                                            
                                                
                                                    <
                                                    img
                                                
                                                src
                                                
                                                    =
                                                    "
                                                    dinosaur.png"
                                                
                                                aria-labelledby
                                                
                                                    =
                                                    "
                                                    dino-label"
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    dino-label"
                                                
                                                >
                                            
                                            The Mozilla red Tyrannosaurus…
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                        
                                    

In this case, we are not using the alt attribute at all — instead, we have presented our description of the image as a regular text paragraph, given it an id , and then used the aria-labelledby attribute to refer to that id , which causes screen readers to use that paragraph as the alt text/label for that image. This is especially useful if you want to use the same text as a label for multiple images — something that isn't possible with alt .

Note: aria-labelledby is part of the WAI-ARIA spec, which allows developers to add in extra semantics to their markup to improve screen reader accessibility where needed. To learn more about how it works, read our WAI-ARIA Basics article.

Figures and figure captions

HTML includes two elements — <figure > and <figcaption > — which associate a figure of some kind (it could be anything, not necessarily an image) with a figure caption:

html
                                        
                                            
                                                
                                                    <
                                                    figure
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    img
                                                
                                                src
                                                
                                                    =
                                                    "
                                                    dinosaur.png"
                                                
                                                alt
                                                
                                                    =
                                                    "
                                                    The Mozilla Tyrannosaurus"
                                                
                                                aria-describedby
                                                
                                                    =
                                                    "
                                                    dinodescr"
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    figcaption
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    dinodescr"
                                                
                                                >
                                            
                                            A red Tyrannosaurus Rex: A two legged dinosaur standing upright like a
human, with small arms, and a large head with lots of sharp teeth.

                                            
                                                
                                                    </
                                                    figcaption
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    figure
                                                
                                                >
                                            
                                        
                                    

While there is mixed screen reader support of associating figure captions with their figures, including aria-labelledby or aria-describedby creates the association if none is present. That said, the element structure is useful for CSS styling, plus it provides a way to place a description of the image next to it in the source.

Empty alt attributes

html
                                        
                                            
                                                
                                                    <
                                                    h3
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    img
                                                
                                                src
                                                
                                                    =
                                                    "
                                                    article-icon.png"
                                                
                                                alt
                                                
                                                    =
                                                    "
                                                    "
                                                
                                                />
                                            
                                            Tyrannosaurus Rex: the king of the dinosaurs

                                            
                                                
                                                    </
                                                    h3
                                                
                                                >
                                            
                                        
                                    

There may be times where an image is included in a page's design, but its primary purpose is for visual decoration. You'll notice in the code example above that the image's alt attribute is empty — this is to make screen readers recognize the image, but not attempt to describe the image (instead they'd just say "image", or similar).

The reason to use an empty alt instead of not including it is because many screen readers announce the whole image URL if no alt is provided. In the above example, the image is acting as a visual decoration to the heading it's associated with. In cases like this, and in cases where an image is only decoration and has no content value, you should include an empty alt in your img elements. Another alternative is to use the aria role attribute role="presentation" as this also stops screen readers from reading out alternative text.

Note: If possible you should use CSS to display images that are only decorative.

Links (the <a > element with an href attribute), depending on how they are used, can help or harm accessibility. By default, links are accessible in appearance. They can improve accessibility by helping a user quickly navigate to different sections of a document. They can also harm accessibility if their accessible styling is removed or if JavaScript causes them to behave in unexpected ways.

By default, links are visually different from other text in both color and text-decoration , with links being blue and underlined by default, purple and underlined if visited, and with a focus-ring when they receive keyboard focus.

Color should not be used as the sole method of distinguishing links from non-linking content. Link text color, like all text, has to be significantly different from the background color (a 4.5:1 contrast ). In addition, links should visually be significantly different from non-linking text, with a minimum contrast requirement of 3:1 between link text and surrounding text and between default, visited, and focus/active states and a 4.5:1 contrast between all those state colors and the background color.

onclick events

Anchor tags are often abused with the onclick event to create pseudo-buttons by setting href to "#" or "javascript:void(0)" to prevent the page from refreshing.

These values cause unexpected behavior when copying or dragging links, opening links in a new tab or window, bookmarking, and when JavaScript is still downloading, errors out, or is disabled. This also conveys incorrect semantics to assistive technologies (e.g., screen readers). In these cases, it is recommended to use a <button > instead. In general you should only use an anchor for navigation using a proper URL.

Links that open in a new tab or window via the target="_blank" declaration and links to whose href value points to a file resource should include an indicator about the behavior that will occur when the link is activated.

People experiencing low vision conditions, who are navigating with the aid of screen reading technology, or who have cognitive concerns may become confused when the new tab, window, or application is opened unexpectedly. Older versions of screen reading software may not even announce the behavior.

html
                                        
                                            
                                                
                                                    <
                                                    a
                                                
                                                target
                                                
                                                    =
                                                    "
                                                    _blank"
                                                
                                                href
                                                
                                                    =
                                                    "
                                                    https://www.wikipedia.org/"
                                                
                                                >
                                            
                                            Wikipedia (opens in a new window)
                                            
                                                
                                                    </
                                                    a
                                                
                                                >
                                            
                                        
                                    
html
                                        
                                            
                                                
                                                    <
                                                    a
                                                
                                                target
                                                
                                                    =
                                                    "
                                                    _blank"
                                                
                                                href
                                                
                                                    =
                                                    "
                                                    2017-annual-report.ppt"
                                                
                                                >
                                            
                                            2017 Annual Report (PowerPoint)
                                            
                                                
                                                    </
                                                    a
                                                
                                                >
                                            
                                        
                                    

If an icon is used in place of text to signify this kind of links behavior, make sure it includes an alternate description .

A skip link, also known as skipnav, is an a element placed as close as possible to the opening <body > element that links to the beginning of the page's main content. This link allows people to bypass content repeated throughout multiple pages on a website, such as a website's header and primary navigation.

Skip links are especially useful for people who navigate with the aid of assistive technology such as switch control, voice command, or mouth sticks/head wands, where the act of moving through repetitive links can be a laborious task.

Proximity

Large amounts of interactive content—including anchors—placed in close visual proximity to each other should have space inserted to separate them. This spacing is beneficial for people who suffer from fine motor control issues and may accidentally activate the wrong interactive content while navigating.

Spacing may be created using CSS properties such as margin .

Test your skills!

You've reached the end of this article, but can you remember the most important information? See Test your skills: HTML Accessibility to verify that you've retained this information before you move on.

Summary

You should now be well-versed in writing accessible HTML for most occasions. Our WAI-ARIA basics article will help to fill gaps in this knowledge, but this article has taken care of the basics. Next up we'll explore CSS and JavaScript, and how accessibility is affected by their good or bad use.

CSS and JavaScript accessibility best practices

CSS and JavaScript, when used properly, also have the potential to allow for accessible web experiences, or they can significantly harm accessibility if misused. This article outlines some CSS and JavaScript best practices that should be considered to ensure even complex content is as accessible as possible.

Prerequisites: A basic understanding of HTML, CSS, and JavaScript, and understanding of what accessibility is .
Objective: To gain familiarity with using CSS and JavaScript appropriately in your web documents to maximize accessibility and not detract from it.

CSS and JavaScript are accessible?

CSS and JavaScript don't have the same immediate importance for accessibility as HTML, but they are still able to help or damage accessibility, depending on how they are used. To put it another way, it is important that you consider some best practice advice to make sure that your use of CSS and JavaScript doesn't ruin the accessibility of your documents.

CSS

Let's start off by looking at CSS.

Correct semantics and user expectation

It is possible to use CSS to make any HTML element look like anything , but this doesn't mean that you should. As we frequently mentioned in our HTML: A good basis for accessibility article, you should use the appropriate semantic element for the job, whenever possible. If you don't, it can cause confusion and usability issues for everyone, but particularly users with disabilities. Using correct semantics has a lot to do with user expectations — elements look and behave in certain ways, according to their functionality, and these common conventions are expected by users.

As an example, a screen reader user can't navigate a page via heading elements if the developer hasn't appropriately used heading elements to markup the content. By the same token, a heading loses its visual purpose if you style it so it doesn't look like a heading.

Bottom line, you can update the styling of a page feature to fit in your design, but don't change it so much that it no longer looks or behaves as expected. The following sections summarize the main HTML features to consider.

"Standard" text content structure

Headings, paragraphs, lists — the core text content of your page:

html
                                        
                                            
                                                
                                                    <
                                                    h1
                                                
                                                >
                                            
                                            Heading
                                            
                                                
                                                    </
                                                    h1
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            Paragraph
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    ul
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    li
                                                
                                                >
                                            
                                            My list
                                            
                                                
                                                    </
                                                    li
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    li
                                                
                                                >
                                            
                                            has two items.
                                            
                                                
                                                    </
                                                    li
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    ul
                                                
                                                >
                                            
                                        
                                    

Some typical CSS might look like this:

css
                                        
                                            h1
                                            {
                                            font-size
                                            :
                                            5rem;
                                            }
                                            p,
li
                                            {
                                            line-height
                                            :
                                            1.5;
                                            font-size
                                            :
                                            1.6rem;
                                            }
                                        
                                    

You should:

  • Select sensible font sizes, line heights, letter spacing, etc. to make your text logical, legible, and comfortable to read.
  • Make sure your headings stand out from your body text, typically big and bold like the default styling. Your lists should look like lists.
  • Your text color should contrast well with your background color.

See HTML text fundamentals and Styling text for more information.

Emphasized text

Inline markup that confers specific emphasis to the text that it wraps:

html
                                        
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            The water is 
                                            
                                                
                                                    <
                                                    em
                                                
                                                >
                                            
                                            very hot
                                            
                                                
                                                    </
                                                    em
                                                
                                                >
                                            
                                            .
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            Water droplets collecting on surfaces is called 
                                            
                                                
                                                    <
                                                    strong
                                                
                                                >
                                            
                                            condensation
                                            
                                                
                                                    </
                                                    strong
                                                
                                                >
                                            
                                            .

                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                        
                                    

You might want to add some simple coloring to your emphasized text:

css
                                        
                                            strong,
em
                                            {
                                            color
                                            :
                                            #a60000;
                                            }
                                        
                                    

You will however rarely need to style emphasis elements in any significant way. The standard conventions of bold and italic text are very recognizable, and changing the style can cause confusion. For more on emphasis, see Emphasis and importance .

Abbreviations

An element that allows an abbreviation, acronym, or initialization to be associated with its expansion:

html
                                        
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            Web content is marked up using Hypertext Markup Language, or

                                            
                                                
                                                    <
                                                    abbr
                                                
                                                >
                                            
                                            HTML
                                            
                                                
                                                    </
                                                    abbr
                                                
                                                >
                                            
                                            .

                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                        
                                    

Again, you might want to style it in some simple way:

css
                                        
                                            abbr
                                            {
                                            color
                                            :
                                            #a60000;
                                            }
                                        
                                    

The recognized styling convention for abbreviations is a dotted underline, and it is unwise to significantly deviate from this. For more on abbreviations, see Abbreviations .

Hyperlinks — the way you get to new places on the web:

html
                                        
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            Visit the 
                                            
                                                
                                                    <
                                                    a
                                                
                                                href
                                                
                                                    =
                                                    "
                                                    https://www.mozilla.org"
                                                
                                                >
                                            
                                            Mozilla homepage
                                            
                                                
                                                    </
                                                    a
                                                
                                                >
                                            
                                            .
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                        
                                    

Some very simple link styling is shown below:

css
                                        
                                            a
                                            {
                                            color
                                            :
                                            #ff0000;
                                            }
                                            a:hover,
a:visited,
a:focus
                                            {
                                            color
                                            :
                                            #a60000;
                                            text-decoration
                                            :
                                            none;
                                            }
                                            a:active
                                            {
                                            color
                                            :
                                            #000000;
                                            background-color
                                            :
                                            #a60000;
                                            }
                                        
                                    

The standard link conventions are underlined and a different color (default: blue) in their standard state, another color variation when the link has previously been visited (default: purple), and yet another color when the link is activated (default: red). In addition, the mouse pointer changes to a pointer icon when links are moused over, and the link receives a highlight when focused (e.g. via tabbing) or activated. The following image shows the highlight in both Firefox (a dotted outline) and Chrome (a blue outline):

Screenshot of a list of links in Firefox browser. The list contains 4 items. The second list item is highlighted using a blue dotted outline when it is focussed via tabbing.

Screenshot of a list of links in Chrome browser. The list contains 4 items. The third list item is highlighted using a blue outline when it is focussed via tabbing.

You can be creative with link styles, as long as you keep giving users feedback when they interact with the links. Something should definitely happen when states change, and you shouldn't get rid of the pointer cursor or the outline — both are very important accessibility aids for those using keyboard controls.

Form elements

Elements to allow users to input data into websites:

html
                                        
                                            
                                                
                                                    <
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                >
                                            
                                            Enter your name
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    text"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                        
                                    

You can see some good example CSS in our form-css.html example (see it live also).

Most of the CSS you'll write for forms will be for sizing the elements, lining up labels and inputs, and getting them looking neat and tidy.

You shouldn't however deviate too much from the expected visual feedback form elements receive when they are focused, which is basically the same as links (see above). You could style form focus/hover states to make this behavior more consistent across browsers or fit in better with your page design, but don't get rid of it altogether — again, people rely on these clues to help them know what is going on.

Tables

Tables for presenting tabular data.

You can see a good, simple example of table HTML and CSS in our table-css.html example (see it live also ).

Table CSS generally serves to make the table fit better into your design and look less ugly. It is a good idea to make sure the table headers stand out (normally using bold), and use zebra striping to make different rows easier to parse.

Color and color contrast

When choosing a color scheme for your website, make sure that the text (foreground) color contrasts well with the background color. Your design might look cool, but it is no good if people with visual impairments like color blindness can't read your content.

There is an easy way to check whether your contrast is large enough to not cause problems. There are a number of contrast checking tools online that you can enter your foreground and background colors into, to check them. For example WebAIM's Color Contrast Checker is simple to use, and provides an explanation of what you need to conform to the WCAG criteria around color contrast.

Note: A high contrast ratio will also allow anyone using a smartphone or tablet with a glossy screen to better read pages when in a bright environment, such as sunlight.

Another tip is to not rely on color alone for signposts/information, as this will be no good for those who can't see the color. Instead of marking required form fields in red, for example, mark them with an asterisk and in red.

Hiding things

There are many instances where a visual design will require that not all content is shown at once. For example, in our Tabbed info box example (see source code ) we have three panels of information, but we are positioning them on top of one another and providing tabs that can be clicked to show each one (it is also keyboard accessible — you can alternatively use Tab and Enter/Return to select them).

Three tab interface with Tab 1 selected and only its contents are displayed. The contents of other tabs are hidden. If a tab is selected, then it's text-color changes from black to white and the background-color changes from orange-red to saddle brown.

Screen reader users don't care about any of this — they are happy with the content as long as the source order makes sense, and they can get to it all. Absolute positioning (as used in this example) is generally seen as one of the best mechanisms of hiding content for visual effect because it doesn't stop screen readers from getting to it.

On the other hand, you shouldn't use visibility :hidden or display :none , because they do hide content from screen readers. Unless of course, there is a good reason why you want this content to be hidden from screen readers.

Note: Invisible Content Just for Screen Reader Users has a lot more useful detail surrounding this topic.

Accept that users can override styles

It is possible for users to override your styles with their own custom styles, for example:

Users might do this for a variety of reasons. A visually impaired user might want to make the text bigger on all websites they visit, or a user with severe color deficiency might want to put all websites in high contrast colors that are easy for them to see. Whatever the need, you should be comfortable with this, and make your designs flexible enough so that such changes will work in your design. As an example, you might want to make sure your main content area can handle bigger text (maybe it will start to scroll to allow it all to be seen), and won't just hide it, or break completely.

JavaScript

JavaScript can also break accessibility, depending on how it is used.

Modern JavaScript is a powerful language, and we can do so much with it these days, from simple content and UI updates to fully-fledged 2D and 3D games. There is no rule that says all content has to be 100% accessible to all people — you just need to do what you can, and make your apps as accessible as possible.

Simple content and functionality is arguably easy to make accessible — for example text, images, tables, forms and push button that activate functions. As we looked at in our HTML: A good basis for accessibility article, the key considerations are:

  • Good semantics: Using the right element for the right job. For example, making sure you use headings and paragraphs, and <button > and <a > elements
  • Making sure content is available as text, either directly as text content, good text labels for form elements, or text alternatives , e.g. alt text for images.

We also looked at an example of how to use JavaScript to build in functionality where it is missing — see Building keyboard accessibility back in . This is not ideal — really you should just use the right element for the right job — but it shows that it is possible in situations where for some reason you can't control the markup that is used. Another way to improve accessibility for non-semantic JavaScript-powered widgets is to use WAI-ARIA to provide extra semantics for screen reader users. The next article will also cover this in detail.

Complex functionality like 3D games are not so easy to make accessible — a complex 3D game created using WebGL will be rendered on a <canvas > element, which has no facility at this time to provide text alternatives or other information for severely visually impaired users to make use of. It is arguable that such a game doesn't really have this group of people as a part of its main target audience, and it would be unreasonable to expect you to make it 100% accessible to blind people. However, you could implement keyboard controls so it is usable by non-mouse users, and make the color scheme contrasting enough to be usable by those with color deficiencies.

The problem with too much JavaScript

The problem often comes when people rely on JavaScript too much. Sometimes you'll see a website where everything has been done with JavaScript — the HTML has been generated by JavaScript, the CSS has been generated by JavaScript, etc. This has all kinds of accessibility and other issues associated with it, so it is not advised.

As well as using the right element for the right job, you should also make sure you are using the right technology for the right job! Think carefully about whether you need that shiny JavaScript-powered 3D information box, or whether plain old text would do. Think carefully about whether you need a complex non-standard form widget, or whether a text input would do. And don't generate all your HTML content using JavaScript if at all possible.

Keeping it unobtrusive

You should keep unobtrusive JavaScript in mind when creating your content. The idea of unobtrusive JavaScript is that it should be used wherever possible to enhance functionality, not build it in entirely — basic functions should ideally work without JavaScript, although it is appreciated that this is not always an option. But again, a large part of it is using built-in browser functionality where possible.

Good example uses of unobtrusive JavaScript include:

  • Providing client-side form validation, which alerts users to problems with their form entries quickly, without having to wait for the server to check the data. If it isn't available, the form will still work, but validation might be slower.
  • Providing custom controls for HTML <video > s that are accessible to keyboard-only users, along with a direct link to the video that can be used to access it if JavaScript is not available (the default <video > browser controls aren't keyboard accessible in most browsers).

As an example, we've written a quick and dirty client-side form validation example — see form-validation.html (also see the demo live ). Here you'll see a simple form; when you try to submit the form with one or both fields left empty, the submit fails, and an error message box appears to tell you what is wrong.

This kind of form validation is unobtrusive — you can still use the form absolutely fine without the JavaScript being available, and any sensible form implementation will have server-side validation active as well, because it is too easy for malicious users to bypass client-side validation (for example, by turning JavaScript off in the browser). The client-side validation is still really useful for reporting errors — users can know about mistakes they make instantly, rather than having to wait for a round trip to the server and a page reload. This is a definite usability advantage.

Note: Server-side validation has not been implemented in this simple demo.

We've made this form validation pretty accessible too. We've used <label > elements to make sure the form labels are unambiguously linked to their inputs, so screen readers can read them out alongside:

html
                                        
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                >
                                            
                                            Enter your name:
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    text"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                />
                                            
                                        
                                    

We only do the validation when the form is submitted — this is so that we don't update the UI too often and potentially confuse screen reader (and possibly other) users:

js
                                        
                                            form.
                                            onsubmit =
                                            validate;
                                            function
                                            validate
                                            (
                                            e
                                            )
                                            {
                                            errorList.
                                            innerHTML =
                                            ""
                                            ;
                                            for
                                            (
                                            let
                                            i =
                                            0
                                            ;
                                            i <
                                            formItems.
                                            length;
                                            i++
                                            )
                                            {
                                            const
                                            testItem =
                                            formItems[
                                            i]
                                            ;
                                            if
                                            (
                                            testItem.
                                            input.
                                            value ===
                                            ""
                                            )
                                            {
                                            errorField.
                                            style.
                                            left =
                                            "360px"
                                            ;
                                            createLink
                                            (
                                            testItem)
                                            ;
                                            }
                                            }
                                            if
                                            (
                                            errorList.
                                            innerHTML !==
                                            ""
                                            )
                                            {
                                            e.
                                            preventDefault
                                            (
                                            )
                                            ;
                                            }
                                            }
                                        
                                    

Note: In this example, we are hiding and showing the error message box using absolute positioning rather than another method such as visibility or display, because it doesn't interfere with the screen reader being able to read content from it.

Real form validation would be much more complex than this — you'd want to check that the entered name actually looks like a name, the entered age is actually a number and is realistic (e.g. nonnegative and less than 4 digits). Here we've just implemented a simple check that a value has been filled in to each input field (if (testItem.input.value === '') ).

When the validation has been performed, if the tests pass then the form is submitted. If there are errors (if (errorList.innerHTML !== '') ) then we stop the form submitting (using preventDefault() ), and display any error messages that have been created (see below). This mechanism means that the errors will only be shown if there are errors, which is better for usability.

For each input that doesn't have a value filled in when the form is submitted, we create a list item with a link and insert it in the errorList .

js
                                        
                                            function
                                            createLink
                                            (
                                            testItem
                                            )
                                            {
                                            const
                                            listItem =
                                            document.
                                            createElement
                                            (
                                            "li"
                                            )
                                            ;
                                            const
                                            anchor =
                                            document.
                                            createElement
                                            (
                                            "a"
                                            )
                                            ;
                                            const
                                            name =
                                            testItem.
                                            input.
                                            name;
                                            anchor.
                                            textContent =
                                            
                                                `
                                                
                                                    ${
                                                    name}
                                                
                                                field is empty: fill in your 
                                                
                                                    ${
                                                    name}
                                                
                                                .
                                                `
                                            
                                            ;
                                            anchor.
                                            href =
                                            
                                                `
                                                #
                                                
                                                    ${
                                                    name}
                                                
                                                `
                                            
                                            ;
                                            listItem.
                                            appendChild
                                            (
                                            anchor)
                                            ;
                                            errorList.
                                            appendChild
                                            (
                                            listItem)
                                            ;
                                            }
                                        
                                    

Each link serves a dual purpose — it tells you what the error is, plus you can click on it/activate it to jump straight to the input element in question and correct your entry.

In addition, the errorField is placed at the top of the source order (although it is positioned differently in the UI using CSS), meaning that users can find out exactly what's wrong with their form submissions and get to the input elements in question by going back up to the start of the page.

As a final note, we have used some WAI-ARIA attributes in our demo to help solve accessibility problems caused by areas of content constantly updating without a page reload (screen readers won't pick this up or alert users to it by default):

html
                                        
                                            
                                                
                                                    <
                                                    div
                                                
                                                class
                                                
                                                    =
                                                    "
                                                    errors"
                                                
                                                role
                                                
                                                    =
                                                    "
                                                    alert"
                                                
                                                aria-relevant
                                                
                                                    =
                                                    "
                                                    all"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    ul
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    ul
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                        
                                    

We will explain these attributes in our next article, which covers WAI-ARIA in much more detail.

Note: Some of you will probably be thinking about the fact that HTML forms have built-in validation mechanisms like the required , min /minlength , and max /maxlength attributes (see the <input > element reference for more information). We didn't end up using these in the demo because cross-browser support for them is patchy (for example IE10 and above only).

Note: WebAIM's Usable and Accessible Form Validation and Error Recovery provides some further useful information about accessible form validation.

Other JavaScript accessibility concerns

There are other things to be aware of when implementing JavaScript and thinking about accessibility. We will add more as we find them.

mouse-specific events

As you will be aware, most user interactions are implemented in client-side JavaScript using event handlers, which allow us to run functions in response to certain events happening. Some events can have accessibility issues. The main example you'll come across is mouse-specific events like mouseover , mouseout , dblclick , etc. Functionality that runs in response to these events will not be accessible using other mechanisms, like keyboard controls.

To mitigate such problems, you should double up these events with similar events that can be activated by other means (so-called device-independent event handlers) — focus and blur would provide accessibility for keyboard users.

Let's look at an example that highlights when this could be useful. Maybe we want to provide a thumbnail image that shows a larger version of the image when it is moused over or focused (like you'd see on an e-commerce product catalog.)

We've made a very simple example, which you can find at mouse-and-keyboard-events.html (see also the source code ). The code features two functions that show and hide the zoomed-in image; these are run by the following lines that set them as event handlers:

js
                                        
                                            imgThumb.
                                            onmouseover =
                                            showImg;
                                            imgThumb.
                                            onmouseout =
                                            hideImg;
                                            imgThumb.
                                            onfocus =
                                            showImg;
                                            imgThumb.
                                            onblur =
                                            hideImg;
                                        
                                    

The first two lines run the functions when the mouse pointer hovers over and stops hovering over the thumbnail, respectively. This won't allow us to access the zoomed view by keyboard though — to allow that, we've included the last two lines, which run the functions when the image is focused and blurred (when focus stops). This can be done by tabbing over the image, because we've included tabindex="0" on it.

The click event is interesting — it sounds mouse-dependent, but most browsers will activate onclick event handlers after Enter/Return is pressed on a link or form element that has focus, or when such an element is tapped on a touchscreen device. This doesn't work by default however when you allow a non-default-focusable event to have focus using tabindex — in such cases you need to detect specifically when that exact key is pressed (see Building keyboard accessibility back in ).

Test your skills!

You've reached the end of this article, but can you remember the most important information? You can find some further tests to verify that you've retained this information before you move on — see Test your skills: CSS and JavaScript accessibility .

Summary

We hope this article has given you a good amount of detail and understanding about the accessibility issues surrounding CSS and JavaScript use on web pages.

Next up, WAI-ARIA!

Updated on April 20, 2024 by Datarist.