简体   繁体   English

我的 sr-only 替代方案对您来说是否正确?

[英]Does my sr-only alternative seem correct to you?

display:none or visibility:hidden are not good ideas to hide a focusable element, because screen readers cannot see them. display:nonevisibility:hidden不是隐藏可聚焦元素的好主意,因为屏幕阅读器看不到它们。

This is the "traditional" way to hide an element from view but keep it accessible:这是从视图中隐藏元素但保持可访问性的“传统”方式:

.sr-only {
  border: 0;
  clip: rect(0,0,0,0);
  clip-path: inset(50%);
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
}

I started to use my own like this for check boxes, radio buttons that I style with their labels:我开始像这样使用自己的复选框,用标签设置样式的单选按钮:

[type=radio], [type=checkbox] {
  position: absolute;    /* keeps it out of flow */
  opacity: 0;            /* makes it invisible */
  pointer-events: none;  /* being able to click the content below */
}
[type=radio] + label , [type=checkbox] + label {
  /* custom styling */
}
[type=radio]:checked + label , [type=checkbox]:checked + label {
  /* custom styling */
}

Do you see any big issue with that idea?你觉得这个想法有什么大问题吗?

I would suggest that the problem here is that you are applying .sr-only styling to style focusable form elements .我建议这里的问题是您将.sr-only样式应用于样式可聚焦的表单元素 If you look at a library like Bootstrap, they have a set of utility classes for focusable and non-focusable screen reader text .如果您查看像 Bootstrap 这样的库,它们有一组用于可聚焦和不可聚焦屏幕阅读器文本的实用程序类 You'll note that in their example they are applying the "focusable" variant to anchor text.您会注意到,在他们的示例中,他们将“可聚焦”变体应用于文本。

Bootstrap indeed has custom styled radio buttons and other form elements. Bootstrap 确实有自定义样式的单选按钮和其他表单元素。 However, you'll note that they are not extending or otherwise leveraging their screen reader text styling.但是,您会注意到它们没有扩展或以其他方式利用其屏幕阅读器文本样式。 Instead, they are utilizing appearance: none to remove platform-specific styles and allow for custom styling of the form element.相反,他们正在利用appearance: none来删除特定于平台的 styles 并允许自定义表单元素的样式。 This is the idiomatic and accessible manner in which to achieve custom styled form elements.是实现自定义样式表单元素的惯用且易于访问的方式。

Slugolicious's comment is quite apt . Slugolicious 的评论非常贴切 While you may not opt to leverage Bootstrap (or a similarly mature UI framework) directly, feel free to borrow liberally from them;虽然您可能不会选择直接利用 Bootstrap(或类似成熟的 UI 框架),但请随意向它们借用; they have spent years honing and iterating on this code to maximize consistency and accessibility across platforms.他们花了数年时间对这段代码进行磨练和迭代,以最大限度地提高跨平台的一致性和可访问性。 Spinning your own would likely be wasted effort.自己旋转可能会浪费精力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM