使用 .visually-hidden
在视觉上隐藏元素,同时仍允许其暴露于辅助技术(例如屏幕阅读器)。默认情况下,使用 .visually-hidden-focusable
在视觉上隐藏元素,但在获得焦点时显示它(例如,由仅使用键盘的用户)。.visually-hidden-focusable
也可以应用于容器 - 感谢 :focus-within
,当容器的任何子元素获得焦点时,容器就会被显示。
¥Visually hide an element while still allowing it to be exposed to assistive technologies (such as screen readers) with .visually-hidden
. Use .visually-hidden-focusable
to visually hide an element by default, but to display it when it’s focused (e.g. by a keyboard-only user). .visually-hidden-focusable
can also be applied to a container–thanks to :focus-within
, the container will be displayed when any child element of the container receives focus.
<h2 class="visually-hidden">Title for screen readers</h2>
<a class="visually-hidden-focusable" href="#content">Skip to main content</a>
<div class="visually-hidden-focusable">A container with a <a href="#">focusable element</a>.</div>
visually-hidden
和 visually-hidden-focusable
都可以用作 mixin。
¥Both visually-hidden
and visually-hidden-focusable
can also be used as mixins.
// Usage as a mixin
.visually-hidden-title {
@include visually-hidden;
}
.skip-navigation {
@include visually-hidden-focusable;
}