简体   繁体   English

Bootstrap 3 中的 sr-only 是什么?

[英]What is sr-only in Bootstrap 3?

What is the class sr-only used for?sr-only用于什么? Is it important or can I remove it?它重要还是我可以删除它? Works fine without.没有工作正常。

Here's my example:这是我的例子:

<div class="btn-group">
    <button type="button" class="btn btn-info btn-md">Departments</button>
    <button type="button" class="btn btn-info dropdown-toggle btn-md" data-toggle="dropdown">
    <span class="caret"></span>
    <span class="sr-only">Toggle Dropdown</span>
    </button>
    <ul class="dropdown-menu" role="menu">
        <li><a href="#">Sales</a></li>
        <li><a href="#">Technical</a></li>
        <li class="divider"></li>
        <li><a href="#">Show all</a></li>
    </ul>
</div>

According to bootstrap's documentation , the class is used to hide information intended only for screen readers from the layout of the rendered page.根据bootstrap 的文档,该类用于在呈现页面的布局中隐藏仅供屏幕阅读器使用的信息。

Screen readers will have trouble with your forms if you don't include a label for every input.如果您没有为每个输入都包含一个标签,屏幕阅读器将无法处理您的表单。 For these inline forms, you can hide the labels using the .sr-only class.对于这些内联表单,您可以使用 .sr-only 类隐藏标签。

Here is an example styling used:这是使用的示例样式:

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  border: 0;
}

Is it important or can I remove it?它重要还是我可以删除它? Works fine without.没有工作正常。

It's important, don't remove it.这很重要,不要删除它。

You should always consider screen readers for accessibility purposes.出于可访问性目的,您应该始终考虑屏幕阅读器。 Usage of the class will hide the element anyways, therefore you shouldn't see a visual difference.无论如何,类的使用都会隐藏元素,因此您不应该看到视觉差异。

If you're interested in reading about accessibility:如果您有兴趣阅读有关可访问性的内容:

As JoshC said, the class .sr-only is used to visually hide the information used for screen readers only.正如 JoshC 所说,类.sr-only用于在视觉上隐藏仅用于屏幕阅读器的信息。 But not only to hide labels.但不仅是为了隐藏标签。 You might consider hiding various other elements such as "skip to main content" link, icons which have an alternative texts etc.您可能会考虑隐藏各种其他元素,例如“跳转到主要内容”链接、具有替代文本的图标等。

BTW.顺便提一句。 you can also use .sr-only sr-only-focusable if you need the element to become visible when focused eg "skip to main content"如果您需要元素在聚焦时变得可见,您也可以使用.sr-only sr-only-focusable例如“跳到主要内容”

If you want make your website even more accessible I recommend to start here:如果您想让您的网站更易于访问,我建议从这里开始:

Why?为什么?

According to the World Health Organization, 285 million people have vision impairments.根据世界卫生组织的数据,有 2.85 亿人有视力障碍。 So making a website accessible is important.因此,使网站易于访问很重要。

IMPORTANT: Avoid treating disabled users differently .重要提示:避免区别对待残疾用户 Generally speaking try to avoid developing a different content for different groups of users.一般来说,尽量避免为不同的用户群体开发不同的内容。 Instead try to make accessible the existing content so that it simply works out-of-the-box and for all not specifically targeting eg screen readers.而是尝试使现有内容可访问,以便它开箱即用,并且适用于所有未专门针对屏幕阅读器的内容。 In other words don't try to reinvent the wheel.换句话说,不要试图重新发明轮子。 Otherwise the resulting accessibility will often be worse than if there was nothing developed at all.否则,由此产生的可访问性通常会比根本没有开发的情况更糟。 We developers should not assume how those users will use our website.我们开发人员不应假设这些用户将如何使用我们的网站。 So be very careful when you need to develop such solutions.所以当你需要开发这样的解决方案时要非常小心。 Obviously a "skip link" is a good example of such content if it's made visible when focused.显然,“跳过链接”是此类内容的一个很好的例子,如果它在聚焦时可见。 But there many bad examples too.但也有很多不好的例子。 Such would be hiding from a screen reader a "zoom" button on the map assuming that it has no relevance to blind users.这将在屏幕阅读器中隐藏地图上的“缩放”按钮,假设它与盲人用户无关。 But surprisingly, a zoom function indeed is used among blind users!但令人惊讶的是,盲人用户确实使用了缩放功能! They like to download images like many other users do (even in high resolution), for sending them to somebody else or for using them in some other context.他们喜欢像许多其他用户一样下载图像(即使是高分辨率的),以便将它们发送给其他人或在其他环境中使用它们。 Source - Read more @ADG: Bad ARIA practices 来源 - 阅读更多 @ADG:不良 ARIA 实践

I found this in the navbar example , and simplified it.我在导航栏示例中找到了这一点,并对其进行了简化。

<ul class="nav">
  <li><a>Default</a></li>
  <li><a>Static top</a></li>
  <li><b><a>Fixed top <span class="sr-only">(current)</span></a></b></li>
</ul>

You see which one is selected ( sr-only part is hidden):您会看到选择了哪个( sr-only部分被隐藏):

  • Default默认
  • Static top静态顶部
  • Fixed top固定顶

You hear which one is selected if you use screen reader:如果您使用屏幕阅读器,您会听到选择了哪一个:

  • Default默认
  • Static top静态顶部
  • Fixed top (current)固定顶部(当前)

As a result of this technique blind people supposed to navigate easier on your website.由于这项技术,盲人应该在您的网站上更轻松地导航。

.sr-only is a class name specifically used for screen readers. .sr-only是专门用于屏幕阅读器的类名。 You can use any class name, but .sr-only is pretty commonly used.您可以使用任何类名,但.sr-only非常常用。 If you don't care about developing with compliance in mind, then it can be removed.如果您不关心在开发时考虑合规性,那么可以将其删除。 It will not affect UI in any way if removed because the CSS for this class is not visible to desktop and mobile device browsers.如果删除它不会以任何方式影响 UI,因为此类的 CSS 对桌面和移动设备浏览器不可见。

There seems to be some information missing here about the use of .sr-only to explain its purpose and being for screen readers.这里似乎缺少一些关于使用.sr-only来解释其目的和用于屏幕阅读器的信息。 First and foremost, it is very important to always keep impaired users in mind.首先,始终牢记受损用户非常重要。 Impairment is the purpose of 508 compliance: https://www.section508.gov/ , and it is great that bootstrap takes this into consideration.减值是 508 合规的目的: https : //www.section508.gov/ ,引导程序考虑到这一点很棒。 However, the use of .sr-only is not all that needs to be taken into consideration for 508 compliance.但是, .sr-only使用.sr-only并不是 508 合规性需要考虑的全部。 You have the use of color, size of fonts, accessibility via navigation, descriptors, use of aria and so much more.您可以使用颜色、字体大小、导航可访问性、描述符、咏叹调的使用等等。

But as for .sr-only - what does the CSS actually do?但至于.sr-only - CSS 实际上是做什么的? There are several slightly different variants of the CSS used for .sr-only .用于.sr-only的 CSS 有几种略有不同的变体。 One of the few I use is below:我使用的少数之一如下:

.sr-only {
    position: absolute;
    margin: -1px 0 0 -1px;
    padding: 0;
    display: block;
    width: 1px;
    height: 1px;
    font-size: 1px;
    line-height: 1px;
    overflow: hidden;
    clip: rect(0,0,0,0);
    border: 0;
    outline: 0;
    }

The above CSS hides content in desktop and mobile browsers wrapped with this class, but is seen by a screen reader like JAWS: http://www.freedomscientific.com/Products/Blindness/JAWS .上面的 CSS 隐藏了用此类包装的桌面和移动浏览器中的内容,但可以被 JAWS 之类的屏幕阅读器看到: http : //www.freedomscientific.com/Products/Blindness/JAWS Example markup is as follows:示例标记如下:

<a href="#" target="_blank">
    Click to Open Site
    <span class="sr-only">This is an external link</span>
</a>

Additionally, if a DOM element has a width and height of 0, the element is not seen by the DOM.此外,如果 DOM 元素的宽度和高度为 0,则 DOM 看不到该元素。 This is why the above CSS uses width: 1px; height: 1px;这就是为什么上面的 CSS 使用width: 1px; height: 1px; width: 1px; height: 1px; . . By using display: none and setting your CSS to height: 0 and width: 0 , the element is not seen by the DOM and is thus problematic.通过使用display: none并将您的 CSS 设置为height: 0width: 0 ,DOM 看不到该元素,因此存在问题。 The above CSS using width: 1px; height: 1px;上面的 CSS 使用width: 1px; height: 1px; width: 1px; height: 1px; is not all you do to make the content invisible to desktop and mobile browsers (without overflow: hidden , your content would still show on the screen), and visible to screen readers.并不是让桌面和移动浏览器看不到内容(没有overflow: hidden ,您的内容仍会显示在屏幕上),并且对屏幕阅读器可见。 Hiding the content from desktop and mobile browsers is done by adding an offset from width: 1px and height: 1px previously mentioned by using:隐藏桌面和移动浏览器中的内容是通过添加从width: 1pxheight: 1px前面提到的偏移量来完成的:

position: absolute;
margin: -1px 0 0 -1px; 
overflow: hidden;

Lastly, to have a very good idea of what a screen reader sees and relays to its impaired user, turn off page styling for your browser.最后,要很好地了解屏幕阅读器看到的内容并将其传递给受损用户,请关闭浏览器的页面样式。 For Firefox, you can do this by going to:对于 Firefox,您可以通过以下方式执行此操作:

View > Page Style > No Style

I hope the information I provided here is of further use to someone in addition to the other responses.我希望我在这里提供的信息除了其他回复之外,对其他人也有进一步的用处。

Ensures that the object is displayed (or should be) only to readers and similar devices.确保对象仅向阅读器和类似设备显示(或应该显示)。 It give more sense in context with other element with attribute aria-hidden="true" .它在与属性 aria-hidden="true" 的其他元素的上下文中更有意义。

<div class="alert alert-danger" role="alert">
  <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
  <span class="sr-only">Error:</span>
  Enter a valid email address
</div>

Glyphicon will be displayed on all other devices, word Error: on text readers. Glyphicon 将显示在所有其他设备上,文字错误:在文本阅读器上。

The .sr-only class hides an element to all devices except screen readers: .sr-only类对除screen readers:器之外的所有设备隐藏一个元素screen readers:

Skip to main content Combine .sr-only with .sr-only-focusable to show the element again when it is focused跳转到主要内容 结合 .sr-only 和 .sr-only-focusable 以在获得焦点时再次显示该元素

.sr-only {
border: 0 !important;
clip: rect(1px, 1px, 1px, 1px) !important; /* 1 */
-webkit-clip-path: inset(50%) !important;
    clip-path: inset(50%) !important;  /* 2 */
height: 1px !important;
margin: -1px !important;
overflow: hidden !important;
padding: 0 !important;
position: absolute !important;
width: 1px !important;
white-space: nowrap !important;            /* 3 */

} }

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

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