Bootstrap helper class


This chapter will discuss 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, the text will darken when the mouse is moved over it:

ClassDescriptionInstance
.text-muted Text style of "text-muted" classTry it
.text-primary Text style of "text-primary" classTry it
.text-success Text style of "text-success" classTry it
.text-info "text-info" class text styleTry it
.text-warning "text -warning" class text styleTry it
.text-danger"text-danger" class Text StyleTry it out

Background

The different classes below show different background colors. If the text is a link, the text will darken when the mouse is moved over it:

ClassDescriptionInstance
.bg-primaryThe table cell uses the "bg-primary" classTry it
.bg-success Table cells use the "bg-success" classTry it
.bg-infoTable cells The "bg-info" class is usedTry it
.bg-warningThe table cell uses the "bg-warning" classTry it
.bg-dangerThe table cell uses the "bg-danger" classTry it

Other

ClassDescriptionInstance
.pull-left Elements float to the leftTry it
.pull-rightElements float to the right Try it
.center-blockSet the element to display:block and center itTry it
.clearfixClear float
.showForce element displayTry it
.hiddenForce element hidingTry it
.sr-only Hide elements on devices other than screen readersTry it
.sr-only-focusablewith Used in combination with the .sr-only class to display when the element gets focus (for example: users operating the keyboard)Try it
.text-hideReplace the text content contained in the page element with the background imageTry it
.closeDisplay the close buttonTry it
.caretDisplay drop-down functionTry it

More examples

Close icon

Use a universal close icon to close modal boxes and alert boxes. Use class close to get the close icon.

<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Example - Close Icon</title>
< ;link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
<script src="http:/ /apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
 <script src="http://apps.bdimg.com/libs/bootstrap /3.3.0/js/bootstrap.min.js"></script>
</head>
<body>

<p>Close icon instance
<button type="button" class="close" aria-hidden="true">
×
</button>
</p>

</body>
</html>

Instance

<!DOCTYPE html>
<html>
<head>
   <title>Bootstrap 实例 - 关闭图标</title>
   <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
   <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
   <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
</head>
<body>

<p>关闭图标实例
   <button type="button" class="close" aria-hidden="true">
      ×
   </button>
</p>

</body>
</html>

Run Example»

Click the "Run Example" button to view the online example

Caret

<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Example - Caret</title>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css /bootstrap.min.css">
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body>

<p>Caret example
<span class="caret"></span>
</p>

</body>
</html>

Instance

<!DOCTYPE html>
<html>
<head>
   <title>Bootstrap 实例 - 插入符</title>
   <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
   <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
   <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
</head>
<body>

<p>插入符实例
   <span class="caret"></span>
</p>

</body>
</html>

Running Example»

Click the "Run Instance" button to view the online instance

Quick Float

You can use class pull-left or pull-right to float elements to the left or right respectively. The following example demonstrates this.

<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Example - Fast Float</title>
< ;link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
<script src="http:/ /apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
 <script src="http://apps.bdimg.com/libs/bootstrap /3.3.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="pull-left ">
Quickly float to the left
</div>
<div class="pull-right">
Quickly float to the right
</div>


</body>
</html>

Instance

<!DOCTYPE html>
<html>
<head>
   <title>Bootstrap 实例 - 快速浮动</title>
   <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
   <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
   <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
</head>
<body>

<div class="pull-left">
   向左快速浮动
</div>
<div class="pull-right">
   向右快速浮动
</div>


</body>
</html>

Run Example»

Click the "Run Example" button to view the online example

To align components in the navigation bar, please use .navbar-left or .navbar-right instead. Check out the Bootstrap navigation bar.

Content centering

Use class center-block to center the element.

<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Example - Centered Content Block</title>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
<script src="http: //apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
 <script src="http://apps.bdimg.com/libs/ bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="row" > ##</div>

</body>
</html>


##Example
<!DOCTYPE html>
<html>
<head>
   <title>Bootstrap 实例 - 居中内容块</title>
   <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
   <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
   <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
</head>
<body>

<div class="row">
   <div class="center-block" style="width:200px;background-color:#ccc;">
      这是 center-block 实例
   </div>
</div>

</body>
</html>

Run Example»

Click the "Run Example" button to view the online example

Clear the float

If you need to clear the float of the element, please use .clearfix class.

<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Example - Clear Float</title>
< ;link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
<script src="http:/ /apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
 <script src="http://apps.bdimg.com/libs/bootstrap /3.3.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="clearfix" style ="background: #D8D8D8;border: 1px solid #000;padding: 10px;">
<div class="pull-left" style="background:#58D3F7;">
To the left Quickly float
</div>
##</div>

</body>
</html>


##Example
<!DOCTYPE html>
<html>
<head>
   <title>Bootstrap 实例 - 清除浮动</title>
   <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
   <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
   <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
</head>
<body>

<div class="clearfix"  style="background: #D8D8D8;border: 1px solid #000;padding: 10px;">
   <div class="pull-left" style="background:#58D3F7;">
      向左快速浮动
   </div>
   <div class="pull-right" style="background: #DA81F5;">
      向右快速浮动
   </div>
</div>

</body>
</html>

Run Instance»
Click the "Run Instance" button to view the online instance

Showing and Hiding Content

You can force an element to be shown or hidden (including by screen readers) by using class .show and .hidden.

<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Example - Showing and Hiding Content</title>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
<script src="http ://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs /bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="row " style="padding: 91px 100px 19px 50px;">
<div class="show" style="left-margin:10px;width:300px;background-color:#ccc;">
This is an example of show class
</div>
<div class="hidden" style="width:200px;background-color:#ccc;">
This is hide class Example
</div>
</div>

</body>
</html>

Screen readers

You can hide an element from all devices except screen readers by using class .sr-only.

<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Example - Screen Reader</title>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
<script src="http: //apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
 <script src="http://apps.bdimg.com/libs/ bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="row" style="padding: 91px 100px 19px 50px;">
<form class="form-inline" role="form">
<div class="form-group">
                                                                                                                                                                                                                                                                     ;
       </div>
                                                                                                   <
         <input type="password" class="form-control" placeholder="Password">
                                                                                                               
##</body>
</html>



Example
<!DOCTYPE html>
<html>
<head>
   <title>Bootstrap 实例 - 屏幕阅读器</title>
   <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
   <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
   <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
</head>
<body>

<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 地址</label>
      <input type="email" class="form-control" placeholder="Enter email">
   </div>
   <div class="form-group">
      <label class="sr-only" for="pass">密码</label>
      <input type="password" class="form-control" placeholder="Password">
   </div>
   </form>
</div>

</body>
</html>

Run Example»

Click the "Run Instance" button to view the online instance