Bootstrap Helper Classes
This chapter discusses some helper classes in Bootstrap that may come in handy.
Text
The following different classes show different text colors. If the text is a link, it will darken when the mouse hovers over it:
| class | Description | Example |
|---|---|---|
| .text-muted | Text style of the "text-muted" class | Try it |
| .text-primary | Text style of the "text-primary" class | Try it |
| .text-success | Text style of the "text-success" class | Try it |
| .text-info | Text style of the "text-info" class | Try it |
| .text-warning | Text style of the "text-warning" class | Try it |
| .text-danger | Text style of the "text-danger" class | Try it |
Background
The following different classes show different background colors. If the text is a link, it will darken when the mouse hovers over it:
| class | Description | Example |
|---|---|---|
| .bg-primary | Table cell using the "bg-primary" class | Try it |
| .bg-success | Table cell using the "bg-success" class | Try it |
| .bg-info | Table cell using the "bg-info" class | Try it |
| .bg-warning | Table cell using the "bg-warning" class | Try it |
| .bg-danger | Table cell using the "bg-danger" class | Try it |
Others
| class | Description | Example |
|---|---|---|
| .pull-left | Float an element to the left | Try it |
| .pull-right | Float an element to the right | Try it |
| .center-block | Set an element to display:block and center it | Try it |
| .clearfix | Clear floats | Try it |
| .show | Force an element to display | Try it |
| .hidden | Force an element to hide | Try it |
| .sr-only | Hide an element from all devices except screen readers | Try it |
| .sr-only-focusable | Used together with the .sr-only class to show the element when it gains focus (e.g., for keyboard users) | Try it |
| .text-hide | Replace the text content of a page element with a background image | Try it |
| .close | Show a close button | Try it |
| .caret | Show dropdown functionality | Try it |
More examples
Close Icon
Use the generic close icon to dismiss modals and alerts. Use the class close to get the close icon.
Example
<
p
Close Icon Example
<button type="button" class="close" aria-hidden="true" ×button p>Try it »
The result is as follows:
aria-hidden="true" It mainly helps people with disabilities (such as the blind) use screen readers (which automatically read content aloud); when playback reaches content with this attribute, it is automatically skipped to avoid confusing disabled users!
Carets
Use carets to indicate dropdown functionality and direction. Use an element with class caret 's to get this functionality.
Example
<
p
Caret Example
<span class="caret" span p>Try it »
The result is as follows:
Quick Floats
You can use the classes pull-left or pull-right to float elements to the left or right, respectively. The following example demonstrates this.
Example
<div class="pull-left">Quick float to the left
div
<div class="pull-right">Quick float to the right
div
Try it »
The result is as follows:
To align components in a navbar, use .navbar-left or .navbar-right instead. See Bootstrap Navbar。
Centering Content
Use the class center-block to center an element.
Example
<div class="row">
<div class="center-block" style=">width:200px;background-color:#ccc;
"
This is a center-block example
div
div
Try it »
The result is as follows:
Clear floats
To clear an element's floats, use .clearfix class。
Example
<div class="clearfix" style=">background: #D8D8D8;border: 1px solid #000;padding: 10px;
"
<div class="pull-left" style=">background:#58D3F7;
"
Quick float to the left
div
<div class="pull-right" style=">background: #DA81F5;
"
Quick float to the right
div
div
Try it »
The result is as follows:
Showing and Hiding Content
You can use classes .show and .hidden to force an element to show or hide (including for screen readers).
Example
<div class="row" style=">padding: 91px 100px 19px 50px;
"
<div class="show" style=">margin-left:10px;width:300px;background-color:#ccc;
"
This is an example of the show class
div
<div class="hidden" style=">width:200px;background-color:#ccc;
"
This is an example of the hide class
div
div
Try it »
The result is as follows:
Screen Readers
You can use classes .sr-only to hide an element from all devices except screen readers.
Example
<div class="row" style=">padding: 91px 100px 19px 50px;
"
<form class="form-inline" role="form">
<div class="form-group">
<label class="sr-only" for="email">Email Address
label
<input type="email" class="form-control" placeholder=">Enter email
"
div
<div class="form-group">
<label class="sr-only" for="pass" Password label>
<input type="password" class="form-control" placeholder="Password" div form div>Try it »
The result is as follows:
Here, we see two label elements for input types both with the class sr-only, so the labels will be visible only to screen readers.