在 UI 中隐藏元素很常见,但以可访问的方式这样做可以确保您不会无意中排除某些用户。在本文中,我们将介绍三种场景:
- 对屏幕阅读器隐藏元素。
- 仅向屏幕阅读器显示元素。
- 对所有用户隐藏元素,包括屏幕阅读器。
每个部分都包含代码示例和实际用例
对屏幕阅读器隐藏元素
如果您希望某个元素对视力正常的用户可见,但对屏幕阅读器隐藏,则可以使用 aria-hidden="true" 属性或 CSS。
<div aria-hidden="true">This content is hidden from screen readers.</div>
使用案例
- 隐藏背景图片
- 不传达有意义信息的装饰图标或视觉元素。
<button> <span aria-hidden="true">+</span> Add Item </button>
仅向屏幕阅读器显示元素
要使某个元素对屏幕阅读器可见,但对视力正常的用户不可见,您可以使用“视觉隐藏”技术。 Tailwind CSS 为此目的提供了一个预构建的实用程序类,仅限 sr。
<div> <p>Writing the sr-only class in CSS would look like this<br> </p> <pre class="brush:php;toolbar:false">.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
使用案例
-
用于标记交互元素(例如输入),而不直观地显示文本:
<label for="username" class="sr-only">Username</label> <input type="text">
-
Providing additional information for screen reader users using aria-describedby
<div class="sr-only"> <li><p><strong>Hiding Input Elements for Custom Radio Buttons</strong><br> When creating custom radio buttons (or checkboxes), you often hide the native input element and replace it with a visually styled element. To do this accessibly, the hidden input must remain visible to screen readers.</p></li> <h2> Hiding Elements from All Users </h2> <p>To completely hide an element from both sighted users and screen readers, you can use display: none or visibility: hidden.<br> </p> <pre class="brush:php;toolbar:false">.hidden { display: none; /* or visibility: hidden; */ }
申请课程:
<div> <h2> 长话短说 </h2> <p>下表总结了各种 CSS 属性如何影响不同类型用户的可见性:</p> <div><table> <thead> <tr> <th><strong>CSS Attribute</strong></th> <th><strong>Hides for All Users</strong></th> <th><strong>Hides for Sighted Users</strong></th> <th><strong>Hides for Screen Readers</strong></th> <th><strong>Notes</strong></th> </tr> </thead> <tbody> <tr> <td>display: none</td> <td>✅</td> <td>✅</td> <td>✅</td> <td>Completely removes the element from the visual and accessibility tree.</td> </tr> <tr> <td>visibility: hidden</td> <td>✅</td> <td>✅</td> <td>✅</td> <td>Hides the element visually but keeps it in the layout and accessibility tree.</td> </tr> <tr> <td>opacity: 0</td> <td>❌</td> <td>✅</td> <td>❌</td> <td>Makes the element fully transparent but still visible to screen readers and interactive.</td> </tr> <tr> <td>clip: rect(0, 0, 0, 0)</td> <td>✅</td> <td>✅</td> <td>❌</td> <td>Commonly used in the "visually hidden" technique; removes visual rendering but accessible.</td> </tr> <tr> <td>position: absolute; width: 1px; height: 1px;</td> <td>✅</td> <td>✅</td> <td>❌</td> <td>Used with the "visually hidden" technique; keeps elements accessible to screen readers.</td> </tr> <tr> <td>aria-hidden="true"</td> <td>❌</td> <td>❌</td> <td>✅</td> <td>Removes the element from the accessibility tree but leaves it visually present.</td> </tr> <tr> <td>overflow: hidden</td> <td>❌</td> <td>✅ (if out of bounds)</td> <td>❌</td> <td>Hides content visually if it overflows the container but does not affect screen readers.</td> </tr> <tr> <td>height: 0; width: 0;</td> <td>✅</td> <td>✅</td> <td>❌</td> <td>Hides content visually while keeping it accessible to screen readers.</td> </tr> <tr> <td>z-index: -1</td> <td>❌</td> <td>✅</td> <td>❌</td> <td>Moves the element behind others, making it invisible to sighted users but screen reader-accessible.</td> </tr> <tr> <td>opacity: 0; pointer-events: none;</td> <td>✅</td> <td>✅</td> <td>❌</td> <td>Makes an element fully transparent and non-interactive, but accessible to screen readers.</td> </tr> </tbody> </table></div> </div>
以上是在 CSS 中隐藏元素:可访问的方式的详细内容。更多信息请关注PHP中文网其他相关文章!

在这篇文章中,布莱克·莫里(Blackle Mori)向您展示了一些骇客,同时试图推动同位HTML支持的极限。如果您敢于使用这些,以免您也被标记为CSS罪犯。

具有CSS的自定义光标很棒,但是我们可以将JavaScript提升到一个新的水平。使用JavaScript,我们可以在光标状态之间过渡,将动态文本放置在光标中,应用复杂的动画并应用过滤器。

互动CSS动画和元素相互启动的元素在2025年似乎更合理。虽然不需要在CSS中实施乒乓球,但CSS的灵活性和力量的增加,可以怀疑Lee&Aver Lee&Aver Lee有一天将是一场

有关利用CSS背景滤波器属性来样式用户界面的提示和技巧。您将学习如何在多个元素之间进行背景过滤器,并将它们与其他CSS图形效果集成在一起以创建精心设计的设计。

好吧,事实证明,SVG的内置动画功能从未按计划进行弃用。当然,CSS和JavaScript具有承载负载的能力,但是很高兴知道Smil并没有像以前那样死在水中

是的,让#039;跳上文字包装:Safari Technology Preview In Pretty Landing!但是请注意,它与在铬浏览器中的工作方式不同。

此CSS-tricks更新了,重点介绍了年鉴,最近的播客出现,新的CSS计数器指南以及增加了几位新作者,这些新作者贡献了有价值的内容。

在大多数情况下,人们展示了@Apply的@Apply功能,其中包括Tailwind的单个property实用程序之一(会改变单个CSS声明)。当以这种方式展示时,@Apply听起来似乎很有希望。如此明显


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

记事本++7.3.1
好用且免费的代码编辑器

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 英文版
推荐:为Win版本,支持代码提示!