Accessibility: sr-only or aria-label

雨燕双飞 提交于 2021-02-06 14:53:31

问题


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?


回答1:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/39223952/accessibility-sr-only-or-aria-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!