在 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中文网其他相关文章!

重构自己的代码看起来是什么样的?约翰·瑞亚(John Rhea)挑选了他写的一个旧的CSS动画,并介绍了优化它的思维过程。

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatoryand and powerincreatingsmoothcssanimations.keytricksinclude:1)definingsmoothtransitionsbetnestates,2)使用AnimatingmatematingmultationmatingMultationPropertiessimultane,3)使用使用4)使用BombingeNtibalibility,4)使用BombingingWithjavofofofofofoffo

CSSCOUNTERSAREDOMANAGEAUTOMANAMBERINGINWEBDESIGNS.1)他们可以使用forterablesofcontents,ListItems,and customnumbering.2)AdvancedsincludenestednumberingSystems.3)挑战挑战InclassINCludeBrowsEccerCerceribaliblesibility andperformiballibility andperformissises.4)创造性

使用滚动阴影,尤其是对于移动设备,是克里斯以前涵盖的一个微妙的UX。杰夫(Geoff)涵盖了一种使用动画限制属性的新方法。这是另一种方式。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

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

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

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

WebStorm Mac版
好用的JavaScript开发工具