搜索
首页web前端css教程在 CSS 中隐藏元素:可访问的方式

Hiding Elements in CSS: The Accessible Way

在 UI 中隐藏元素很常见,但以可访问的方式这样做可以确保您不会无意中排除某些用户。在本文中,我们将介绍三种场景:

  1. 对屏幕阅读器隐藏元素。
  2. 仅向屏幕阅读器显示元素。
  3. 对所有用户隐藏元素,包括屏幕阅读器。

每个部分都包含代码示例和实际用例


对屏幕阅读器隐藏元素

如果您希望某个元素对视力正常的用户可见,但对屏幕阅读器隐藏,则可以使用 aria-hidden="true" 属性或 CSS。

<div aria-hidden="true">This content is hidden from screen readers.</div>

使用案例

  1. 隐藏背景图片
  2. 不传达有意义信息的装饰图标或视觉元素。
<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;
}

使用案例

  1. 用于标记交互元素(例如输入),而不直观地显示文本:

    <label for="username" class="sr-only">Username</label>
    <input type="text">
    
    
  2. 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中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
轨道力学(或我如何优化CSS KeyFrames动画)轨道力学(或我如何优化CSS KeyFrames动画)May 09, 2025 am 09:57 AM

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

CSS动画:很难创建它们吗?CSS动画:很难创建它们吗?May 09, 2025 am 12:03 AM

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

@KeyFrames CSS:最常用的技巧@KeyFrames CSS:最常用的技巧May 08, 2025 am 12:13 AM

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

CSS计数器:自动编号的综合指南CSS计数器:自动编号的综合指南May 07, 2025 pm 03:45 PM

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

使用卷轴驱动动画的现代滚动阴影使用卷轴驱动动画的现代滚动阴影May 07, 2025 am 10:34 AM

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

重新访问图像图重新访问图像图May 07, 2025 am 09:40 AM

让我们快速进修。图像地图一直返回到HTML 3.2,首先是服务器端地图,然后使用映射和区域元素通过图像上的单击区域定义了可单击区域。

DEV状态:每个开发人员的调查DEV状态:每个开发人员的调查May 07, 2025 am 09:30 AM

开发委员会调查现已开始参与,并且与以前的调查不同,它涵盖了除法:职业,工作场所,以及健康,爱好等。 

什么是CSS网格?什么是CSS网格?Apr 30, 2025 pm 03:21 PM

CSS网格是创建复杂,响应式Web布局的强大工具。它简化了设计,提高可访问性并提供了比旧方法更多的控制权。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

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

热工具

安全考试浏览器

安全考试浏览器

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

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

mPDF

mPDF

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

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具