Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessibility: sr-only or aria-label

Tags:

From MDN:

In the example below, a button is styled to look like a typical "close" button, with an X in the middle. Since there is nothing indicating that the purpose of the button is to close the dialog, the aria-label attribute is used to provide the label to any assistive technologies.

<button aria-label="Close" onclick="myDialog.close()">X</button>

According to the Bootstrap Documentation:

Hide an element to all devices except screen readers with .sr-only

So I guess I could also write:

<button onclick="myDialog.close()"><span class="sr-only">Close</span>X</button>

In a Bootstrap project, how can I choose which one to prefer?

like image 417
CharlesM Avatar asked Aug 30 '16 09:08

CharlesM


People also ask

Should I use title or aria-label?

aria-label should be used in conjunction with the title attribute. Aria for screen readers, and title attribute for other people who want more information about the action of the button.

Should all links have aria labels?

The aria-label attribute on links ( a elements with a href attribute) should only be used when, for whatever reason, it is not possible or not desirable to use perceivable link text. (This includes the alt attribute in image links.)

Do screen readers use aria-label?

Screen readers work with regular HTML, but adding ARIA can provide screen reader users with greater context and interactivity with the content on the page. ARIA has no effect on how elements are displayed or behave in browsers.

What is SR only?

Example. The .sr-only class hides an element to all devices except screen readers: Skip to main content. Combine .sr-only with .sr-only-focusable to show the element again when it is focused (e.g. by a keyboard-only user):


Video Answer


2 Answers

In the MDN example, a screen reader will just speak just the word "close" since aria-label overrides the text in the button. This will work even if you re-use the code without Bootstrap.

In your example, a screen reader will speak "close x" since you are doing nothing to hide the "x" from screen readers. You are also adding a text node to then hiding it with a class.

I would use the example from MDN.

like image 102
aardrian Avatar answered Sep 20 '22 13:09

aardrian


The class sr-only class is for whole blocks of text content which are only useful to those using a screen reader and should be hidden from others.

An example from and app I'm working on provides instructions for using an accessible controller with the web app:

<div class="sr-only">
  When voting with the text-to-speech audio, use the accessible
  controller to navigate your ballot. To navigate through the
  contests, use the left and right buttons. To navigate through
  contest choices, use the up and down buttons. To select or
  unselect a contest choice as your vote, use the select button.
</div>

In your example, you simply want to provide different text content for a screen reader. To answer your specific question, use the MDN example.

I've use aria-labels to hint to a screen reader when to add pauses by suffixing titles with periods or commas as necessary (More about Pausing in a screen reader for accessibility):

<h1 aria-label="Hello World.">
  Hello World
</h1>
like image 37
Beau Smith Avatar answered Sep 20 '22 13:09

Beau Smith