How to hide a text and make it accessible by screen reader?(如何隐藏文本并使其可由屏幕阅读器访问?)
问题描述
我有一个简单的文本,它会在某个操作上得到更新,我希望屏幕阅读器会宣布它.但我不希望该文本在网页上可见.我尝试了 display: none
和 visibility: hidden
,但似乎屏幕阅读器软件无法访问它们.我找到了一种方法来完成这项工作 - 即通过将元素绝对定位到负 999999 值,这将使其脱离屏幕并隐藏在网页之外.我不是这个解决方案的粉丝.有没有更优雅的方法来实现这一点?
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. I tried display: none
and visibility: hidden
, but seems like they are not accessible by the screen reader softwares. 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. 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;
}
推荐答案
我以前确实遇到过这个问题.Bootstrap 有这个甜蜜的类 sr-only
,它实际上隐藏了网页上的内容,但屏幕阅读器可以访问.您可以查看此链接
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. You can check this link
此外,如果您不使用引导程序,您可以简单地自己在代码中实现该类.
Moreover, if you are not using bootstrap, you can simply implement the class yourself in your code.
.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>
我希望这会有所帮助.
这篇关于如何隐藏文本并使其可由屏幕阅读器访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何隐藏文本并使其可由屏幕阅读器访问?


- 从原点悬停时触发 translateY() 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01