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> </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>
sr-only means "this content is visible only to screen readers". If you are using the site with working eyes, you don't apply. Try using the site blindfolded which obviously requires using some kind of aids to make it possible; the . sr-only content is meant to aid users without vision.
sr-only-focusable to show the element again when it's focused (e.g. by a keyboard-only user). Can also be used as mixins.
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 (e.g. by a keyboard-only user): Skip to main content.
Screen readers are software that enables those who cannot see the screen to access information on computers and smartphones. The technology literally reads the screen aloud or converts it to Braille. As the internet became more popular, screen readers became more sophisticated.
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:
Web Accessibility Initiative (WAI)
MDN Accessibility documentation
As JoshC said, the class .sr-only
is used to visually hide the information used for screen readers 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 e.g. "skip to main content"
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. 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 e.g. 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With