简体   繁体   English

如何隐藏文本并使屏幕阅读器可以访问它?

[英]How to hide a text and make it accessible by screen reader?

I have a simple text that gets updated on an action and I want that to be announced by the screen reader.我有一个简单的文本,它会根据某个动作进行更新,我希望屏幕阅读器能够宣布它。 But I don't want that text to be visible on the web page.但我不希望该文本显示在 web 页面上。 I tried display: none and visibility: hidden , but seems like they are not accessible by the screen reader softwares.我试过display: nonevisibility: hidden ,但屏幕阅读器软件似乎无法访问它们。 I found a way to make this work - that is by absolute positioning the element all the way with negative 999999 value which will make it off screen and hidden from the webpage.我找到了一种方法来完成这项工作 - 即通过绝对定位元素一直使用负 999999 值,这将使它离开屏幕并隐藏在网页中。 I am not really a fan of this solution.我不是这个解决方案的忠实粉丝。 Is there a more elegant way to achieve this?有没有更优雅的方法来实现这一目标?

<span class="aria-invisible" aria-live="polite">5 selections have been made.</span>

.aria-invisible {
   display: none; //either of these two attributes
   visibility: hidden;
}

A better solution to the bootstrap "sr-only" class.引导程序“仅限 sr”class 的更好解决方案。

There are numerous problems with the Bootstrap "sr-only" class.引导程序“仅限 sr”class 存在许多问题。

  1. First of all you will see from this discussion that a negative margin can cause issues on VoiceOver.首先,您将从这个讨论中看到,负边距可能会导致 VoiceOver 出现问题。

  2. Secondly you must account for words wrapping one per line as screen readers do not read line breaks其次,您必须考虑每行换行一个,因为屏幕阅读器不会读取换行符

  3. Finally clip has been deprecated .最后clip已被弃用

To fix point 1 simply don't add a negative margin.要解决第 1 点,只需不要添加负边距。

To fix point 2 add white-space: no-wrap to ensure words do not end up 'one per line' and cause words to get smushed together.要修复第 2 点,请添加white-space: no-wrap以确保单词不会以“每行一个”结尾并导致单词混淆在一起。

To fix point 3 we add clip-path: inset(50%) as this clips to a 0px square, we keep clip as at the moment this has great coverage, clip-path is used to future-proof your solution.为了解决第 3 点,我们添加clip-path: inset(50%) ,因为这个剪辑到一个 0px 的正方形,我们保留clip ,因为它有很大的覆盖范围, clip-path用于未来证明您的解决方案。

Please find below a much more robust class, as of yet I have not found a screen reader / browser combo that does not work as expected with this.请在下面找到更强大的 class,到目前为止,我还没有找到无法按预期工作的屏幕阅读器/浏览器组合。

I have this class on a few different forums being tested, so far so good but if someone can find a problem with it please let me know as I will be submitting it everywhere.我在几个不同的论坛上有这个 class 正在测试,到目前为止一切都很好,但是如果有人能找到它的问题,请告诉我,因为我会在任何地方提交它。

 .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 */ }
 <p>visible text <span class="visually-hidden">hidden text</span></p>

I did encounter this problem in the past.我过去确实遇到过这个问题。 Bootstrap has this sweet class sr-only that actually hides the content on the webpage but is accessible by the screen readers. Bootstrap 有这个可爱的 class sr-only ,它实际上隐藏了网页上的内容,但屏幕阅读器可以访问。 You can check this link你可以检查这个链接

Moreover, if you are not using bootstrap, you can simply implement the class yourself in your code.此外,如果您不使用引导程序,您可以简单地在您的代码中自己实现 class。

 .aria-invisible { border: 0; clip: rect(0 0 0 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px; }
 <span class="aria-invisible">5 selections have been made. </span>

I hope this helps.我希望这有帮助。

Using aria-label attributes is the way to do (example below)使用aria-label属性是一种方法(下面的示例)

Is there a more elegant way to achieve this?有没有更优雅的方法来实现这一点?

Do not hide the element.不要隐藏元素。 Yes.是的。 I am not answering your question, but I am addressing the problem.我不是在回答你的问题,而是在解决问题。

Screenreaders are only a subpart of assistive technologies used by a small part of people targeted by accessibility guidelines.屏幕阅读器只是辅助技术的一部分,被无障碍指南所针对的一小部分人使用。

Imagine using a screen magnifier for instance where you do not have a global view on the full screen.想象一下使用屏幕放大镜,例如在全屏上没有全局视图的情况。 Imagine having some cognitive disabilities which makes difficult for you to count or remember elements.想象一下,有一些认知障碍使您难以计算或记住元素。

If you do consider that an information is important for blind people, then it's surely is for them AND for other people.如果您确实认为某个信息对盲人很重要,那么它肯定对他们和其他人都是如此。

Now, instead of it being a long text, it can be a small counter with appropriate aria labelling:现在,它可以是一个带有适当咏叹调标签的小计数器,而不是一个长文本:

<div role="status" aria-live="polite" aria-label="5 selections have been made.">
  5 selections
</div>

I had the same problem with the text being out of position with the visually hidden class mentioned above.我遇到了同样的问题,文本超出了 position,上面提到的视觉隐藏 class。 Some small changes to the class fixed this issue for me class 的一些小改动为我解决了这个问题

.visually-hidden {
  border: 0;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  height: auto;
  margin: 0;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
  white-space: nowrap;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM