2013-11-03 21:51 阅读 230

引导程序3中的 sr-only 是什么?

What is the class sr-only used for? 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>
    <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>


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

6条回答 默认 最新

  • 已采纳
    weixin_41568127 ?yb? 2016-02-07 19:46

    According to bootstrap's documentation, the class is used to hide information intended only for screen readers from the layout of the rendered page.

    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.

    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:

    点赞 28 评论 复制链接分享
  • csdnceshi78 程序go 2017-01-04 16:11

    .sr-only is a class name specifically used for screen readers. You can use any class name, but .sr-only is pretty commonly used. 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.

    There seems to be some information missing here about the use of .sr-only to explain its purpose and being for screen readers. 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. However, the use of .sr-only is not all that needs to be taken into consideration for 508 compliance. 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? There are several slightly different variants of the CSS used for .sr-only. 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. Example markup is as follows:

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

    Additionally, if a DOM element has a width and height of 0, the element is not seen by the DOM. This is why the above CSS uses 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. The above CSS using 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. Hiding the content from desktop and mobile browsers is done by adding an offset from width: 1px and height: 1px previously mentioned by using:

    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:

    View > Page Style > No Style

    I hope the information I provided here is of further use to someone in addition to the other responses.

    点赞 7 评论 复制链接分享
  • csdnceshi72 谁还没个明天 2013-11-27 12:47

    As JoshC said, the class is used to hide information used for screen readers. But not only to hide labels, you might consider to hide from sighted user an internal link "skip to main content" which is desirable for blind users if you have a complex navigation or adds in the page header before the content. They would need to press arrow down key for too many times to reach it by screen reader.

    If you want your site to interact even more with screen readers, use W3C standardized ARIA attributes and I definitely recommend to visit the Google online course, which will take only up to 1-2h or at least watch a Google's 40min video.

    According to the World Health Organization, 285 million people have vision impairments. So making a website accessible is important.

    点赞 5 评论 复制链接分享
  • csdnceshi72 谁还没个明天 2017-03-08 07:34

    The .sr-only class hides an element to all devices except screen readers:

    Skip to main content Combine .sr-only with .sr-only-focusable to show the element again when it is focused

    点赞 5 评论 复制链接分享
  • weixin_41568184 叼花硬汉 2015-01-24 11:17

    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".

    <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

    Glyphicon will be displayed on all other devices, word Error: on text readers.

    点赞 3 评论 复制链接分享
  • csdnceshi54 hurriedly% 2015-01-03 14:07

    I found this in the navbar example, and simplified it.

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

    You see which one is selected (sr-only part is hidden):

    • 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.

    点赞 1 评论 复制链接分享