【发布时间】:2021-06-08 20:01:25
【问题描述】:
我想让一些内容对屏幕阅读器可用,但在页面上不可见。将其从页面上移除会导致屏幕阅读器不会宣布它,因此我使用了一种常见的 hacky 解决方法,sr-only 类,例如 Bootstrap 使用的。这是Kitty Giraudel 的版本:
.sr-only {
border: 0 !important;
clip: rect(1px, 1px, 1px, 1px) !important;
-webkit-clip-path: inset(50%) !important;
clip-path: inset(50%) !important;
height: 1px !important;
overflow: hidden !important;
padding: 0 !important;
position: absolute !important;
width: 1px !important;
white-space: nowrap !important;
}
问题在于position: absolute 规则,它需要具有position: relative 的父级。没有它,不可见的内容可能会出现在页面底部并导致浏览器添加滚动条。我不喜欢必须在 sr-only 类之上添加 position: relative 规则。它很容易被遗忘或意外删除。如果添加类是唯一需要的步骤,这将更容易,只需将top: 0 添加到这些规则中即可。但我对这种古老的智慧有点紧张。有没有理由不经常这样做?我错过了top: 0 的潜在问题吗?
【问题讨论】:
-
针对这个问题提交了一个铬错误,它更详细地描述了这个问题:bugs.chromium.org/p/chromium/issues/detail?id=1154640
标签: css frontend accessibility web-frontend