我有一个在线课程,有一个困难的评级,显示为以下变化:
<div class="course-difficulty">
<label>Difficulty: </label>
<span class="fas fa-star"></span>
<span class="fas fa-star"></span>
</div>
给屏幕阅读器贴上标签的最佳做法是什么?我可以计算星级,所以标签逻辑上可以应用于父div作为“难点2”,或者这应该是针对单个图标吗?
发布于 2021-05-18 02:43:42
有几件事,首先,在这里使用<label>
并不十分有效。<label>
被设计成链接到交互元素(输入)。
如果这是只显示(即不能对困难进行表决),那么答案很简单:
.visually-hidden {
border: 0;
padding: 0;
margin: 0;
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
clip: rect(1px 1px 1px 1px); /* IE6, IE7 - a 0 height clip, off to the bottom right of the visible 1px box */
clip: rect(1px, 1px, 1px, 1px); /*maybe deprecated but we need to support legacy browsers */
clip-path: inset(50%); /*modern browsers, clip-path works inwards from each corner*/
white-space: nowrap; /* added line to stop words getting smushed together (as they go onto seperate lines and some screen readers do not understand line feeds as a space */
}
<!-- option 1 invisible text (screen reader only text) -->
<div class="course-difficulty">
<p>Difficulty: </p>
<div aria-hidden="true">
<span class="fas fa-star"></span>
<span class="fas fa-star"></span>
</div>
<span class="visually-hidden">2 out of 5</span>
</div>
<!-- option 2 aria-label -->
<div class="course-difficulty">
<p aria-label="Difficulty: 2 out of 5">Difficulty: </p>
<div aria-hidden="true">
<span class="fas fa-star"></span>
<span class="fas fa-star"></span>
</div>
</div>
有两种变体。
在这两个选项中,我们都将星星封装在一个aria-hidden
div中,以将它们隐藏在屏幕读取器中(因为一些屏幕阅读器可能试图读取图标)。我们也更改了一个段落的标签,因为它更有意义。
在第一个选项中,我们使用visually-hidden text在视觉上隐藏文本,但将其提供给屏幕阅读器,解释其困难之处(以及有多少“明星”)。
在第二个选项中,我们使用aria-label
覆盖文本,以包括难度等级。
我个人更喜欢选项1,因为它与屏幕阅读器/浏览器组合具有最好的兼容性,并且只在文本浏览器上工作(有些盲文用户更喜欢使用这种浏览器)。
然而,使用aria-label
没有什么问题,因为现在99%的屏幕读取器/浏览器组合都可以使用这种方法,而且边缘情况是最小的。
如果系统允许投票,那么您将需要使用单选按钮等,如果是这样的话,请告诉我,我可以帮助您。
https://stackoverflow.com/questions/67584457
复制相似问题