zoukankan      html  css  js  c++  java
  • bootstrap整理-1

    基本的HTML模板

    小伙伴们,上一小节的视频课程已经对Bootstrap提供的模板做了详细的介绍,在这一小节中我们把这个模板代码粘贴过来,以供你们学习查看,你们可以根据实际情况在此基础上进行扩展,只需要确保文件引用顺序一致即可。

    如右侧代码编辑器中就是最基本的HTML模板。

    我们来简单解释一下其中几条的重要代码:

    bootstrap模板为使IE6、7、8版本(IE9以下版本)浏览器兼容html5新增的标签,引入下面代码文件即可。

    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    
    

    同理为使IE6、7、8版本浏览器兼容css3样式,引入下面代码:

     
     
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>Bootstrap的HTML标准模板</title>  
            <!-- Bootstrap -->
            <link href="css/bootstrap.min.css" rel="stylesheet">
            <!--你自己的样式文件 -->
            <link href="css/your-style.css" rel="stylesheet">       
            <!-- 以下两个插件用于在IE8以及以下版本浏览器支持HTML5元素和媒体查询,如果不需要用可以移除 -->
            <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <![endif]-->
        </head>
        <body>
            <h1>Hello, world!</h1>
           
            <!-- 如果要使用Bootstrap的js插件,必须先调入jQuery -->
            <script src="http://libs.baidu.com/jquery/1.9.0/jquery.min.js"></script>
            <!-- 包括所有bootstrap的js插件或者可以根据需要使用的js插件调用 -->
        </body>
    </html>
     
     
     

    全局样式

    在制作Web页面时,前端人员都习惯为网站设置一个全局样式(reset.css)。那么在Bootstrap框架中也不例外。Bootstrap框架的核心是轻量的CSS基础代码库,他并没有一味的重置样式,而是注重各浏览器基础表现,降低开发难度。大部分前端人员都具有归零的思想,但实际你会发现,归零之后的样式在开发过程中会存在着潜在的问题,例如,在全局样式表中将em变成一个普通标记,明明应该是斜体,怎么就没效果了呢?

    Bootstrap框架在这一部分做了一定的变更,不再一味追求归零,而是更注重重置可能产生问题的样式(如,body,form的margin等),保留和坚持部分浏览器的基础样式,解决部分潜在的问题,提升一些细节的体验,具体说明如下:

    • 移除body的margin声明
    • 设置body的背景色为白色
    • 为排版设置了基本的字体、字号和行高
    • 设置全局链接颜色,且当链接处于悬浮“:hover”状态时才会显示下划线样式

    小伙伴们,你们可以单击查看右侧“style.css”文件,来查看上面所说的全局样式(这个是从下载的boostrap.css中摘取出来的)。

    其实Bootstrap的全局样式采用了“normalize.css”,但并没有一味的全部使用此重置样式,而是在此基础上进行了一些改良,让其更适合Bootstrap的设计思想。

    你可以通过“normalize.less”(LESS版本)或“_normalize.scss”(Sass版本)进行深入的了解。

     

    标题(一)

    Bootstrap和普通的HTML页面一样,定义标题都是使用标签<h1>到<h6>,只不过Bootstrap覆盖了其默认的样式,使用其在所有浏览器下显示的效果一样,具体定义的规则可以如下表所示:

    通过比较可以发现,Bootstrap标题样式进行了以下显著的优化重置:

    1、重新设置了margin-topmargin-bottom的值,  h1~h3重置后的值都是20pxh4~h6重置后的值都是10px。
    2、所有标题的行高都是1.1(也就是font-size的1.1倍),而且文本颜色和字体都继承父元素的颜色和字体。
    3、固定不同级别标题字体大小,h1=36px,h2=30px,h3=24px,h4=18px,h5=14pxh6=12px。

    标题的具体运用非常简单,和我们平时运用是一样的,使用<h1>~<h6>标签,分别表示标题一至标题六,h 后面的数字越大,表示级别越小,文本也越小。来看一个简单的效果:右侧代码编辑器中的10-16行的代码。

    在Bootstrap中为了让非标题元素和标题使用相同的样式,还特意定义了.h1~.h6六个类名。如右侧代码编辑器中   18-23行代码所示:

    对比两个示例的效果图,可以说他们的效果是一模一样的。
     
    <!--Bootstrap中的标题-->
    <h1>Bootstrap标题一</h1>
    <h2>Bootstrap标题二</h2>
    <h3>Bootstrap标题三</h3>
    <h4>Bootstrap标题四</h4>
    <h5>Bootstrap标题五</h5>
    <h6>Bootstrap标题六</h6>
     
    <!--Bootstrap中让非标题元素和标题使用相同的样式-->
    <div class="h1">Bootstrap标题一</div>
    <div class="h2">Bootstrap标题二</div>
    <div class="h3">Bootstrap标题三</div>
    <div class="h4">Bootstrap标题四</div>
    <div class="h5">Bootstrap标题五</div>
    <div class="h6">Bootstrap标题六</div>
     
     

    标题(二)

    除此之外,我们在Web的制作中,常常会碰到在一个标题后面紧跟着一行小的副标题。在Bootstrap中他也考虑了这种排版效果,使用了<small>标签来制作副标题。这个副标题具有其自己的一些独特样式:

    1、行高都是1,而且font-weight设置了normal变成了常规效果(不加粗),同时颜色被设置为灰色(#999)。
    2、由于<small>内的文本字体在h1~h3内,其大小都设置为当前字号的65%;而在h4~h6内的字号都设置为当前字号的75%;
    详细代码请参阅bootstrap.css文件中第407行~第443行。

    h1 small,
    .h1 small,
    h2 small,
    .h2 small,
    h3 small,
    .h3 small,
    h1 .small,
    .h1 .small,
    h2 .small,
    .h2 .small,
    h3 .small,
    .h3 .small {
      font-size: 65%;
    }
    h4,
    .h4,
    h5,
    .h5,
    h6,
    .h6 {
      margin-top: 10px;
      margin-bottom: 10px;
    }
    h4 small,
    .h4 small,
    h5 small,
    .h5 small,
    h6 small,
    .h6 small,
    h4 .small,
    .h4 .small,
    h5 .small,
    .h5 .small,
    h6 .small,
    .h6 .small {
      font-size: 75%;
    }
    
    
    
     
    <!--Bootstrap中使用了<small>标签来制作副标题-->
    <h1>Bootstrap标题一<small>我是副标题</small></h1>
    <h2>Bootstrap标题二<small>我是副标题</small></h2>
    <h3>Bootstrap标题三<small>我是副标题</small></h3>
    <h4>Bootstrap标题四<small>我是副标题</small></h4>
    <h5>Bootstrap标题五<small>我是副标题</small></h5>
    <h6>Bootstrap标题六<small>我是副标题</small></h6>
     
     

    段落(正文文本)

    段落是排版中另一个重要元素之一。在Bootstrap中为文本设置了一个全局的文本样式(这里所说的文本是指正文文本):

    1、全局文本字号为14px(font-size)

    2、行高为1.42857143(line-height),大约是20px(大家看到一串的小数或许会有疑惑,其实他是通过LESS编译器计算出来的,当然Sass也有这样的功能)。

    3、颜色为深灰色(#333)

    4、字体为"Helvetica Neue", Helvetica, Arial, sans-serif;(font-family),或许这样的字体对我们中文并不太合适,但在实际项目中,大家可以根据自己的需求进行重置,在此我们不做过多阐述,我们回到这里。该设置都定义在<body>元素上,由于这几个属性都是继承属性,所以Web页面中文本(包括段落p元素)如无重置都会具有这些样式效果。

    /*源码请查看bootstrap.css文件中第274行~280行*/

    body {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 14px;
    line-height: 1.42857143;
    color: #333;
    background-color: #fff;
    }
    
    


    另外在Bootstrap中,为了让段落p元素之间具有一定的间距,便于用户阅读文本,特意设置了p元素的margin值(默认情况之下,p元素具有一个上下外边距,并且保持一个行高的高度):
    /*源码请查看bootstrap.css文件中第467行~469行*/

    p {
     margin: 0 0 10px;
    }
    
    

    如果你对CSS预处理器有所了解,那么你完全可以根据Bootstrap提供的预编译版本LESS(或者Sass)进行自定义排版设置。在Bootstrap中,排版设置的默认值都存在variables.less文件中(Sass版本存在_variables.scss中)的两个变量:

    LESS版本:

    @font-size-base: 14px; @line-height-base: 1.428571429; // 20/14
    
    

    Sass版本:

    $font-size-base: 14px !default; $line-height-base: 1.428571429 !default; // 20/14
    
    
    第一条语句用于设置字体大小,第二条语句用于设置行高。系统默认使用这两个值产生整个页面相应的margin、paddingline-height的值。换句话说,你只需要修改这两个变量的值,然后重新编译,就可以自定义自己的Bootstrap排版样式。(有兴趣的同学可以尝试一下,此处对于LESS或Sass版本运用不做过多阐述)
     
    //example
    <p>超酷的互联网、IT技术免费学习平台,创新的网络一站式学习、实践体验;服务及时贴心,内容专业、有趣易学。专注服务互联网工程师快速成为技术高手!</p>
    <p>超酷的互联网、IT技术免费学习平台,创新的网络一站式学习、实践体验;服务及时贴心,内容专业、有趣易学。专注服务互联网工程师快速成为技术高手!</p>
     
     

    强调内容

    在实际项目中,对于一些重要的文本,希望突出强调的部分都会做另外的样式处理。Bootstrap同样对这部分做了一些轻量级的处理。

    如果想让一个段落p突出显示,可以通过添加类名“.lead”实现,其作用就是增大文本字号,加粗文本,而且对行高和margin也做相应的处理。用法如下:

    <p>我是普通文本,我的样子长成这样我是普通文本,我的样子长成这样我是普通文本,</p>
    <p class="lead">我是特意要突出的文本,我的样子成这样。我是特意要突出的文本,我的样子长成这样。</p>
    

    效果查看最右侧结果窗口。

    “.lead”对应的样式如下:
    /*源码查看bootstrap.css文件第470行~480行*/

    .lead {
    margin-bottom: 20px;
    font-size: 16px;
    font-weight: 200;
    line-height: 1.4;
    }
    @media (min- 768px) {/*大中型浏览器字体稍大*/
    .lead {
    font-size: 21px;
      }
    }
    

    除此之外,Bootstrap还通过元素标签:<small><strong><em><cite>给文本做突出样式处理。
    /*源码查看bootstrap.css文件第55行~第58行*/

    b,strong {
      font-weight: bold; /*文本加粗*/
    }
    

    /*源码查看bootstrap.css文件第481行~第484行*/

    small,.small {
      font-size: 85%; /*标准字体的85%,也就是14px * 0.85px,差不多12px*/
    }
    

    /*源码查看bootstrap.css第485行~第487行*/

    cite {
    font-style: normal;
    }
    
     

    粗体

    粗体就是给文本加粗,在普通的元素中我们一般通过font-weight设置为bold关键词给文本加粗。在Bootstrap中,可以使用<b><strong>标签让文本直接加粗。
    /*源码查看bootstrap.css文件第55行~第58行*/

    b,strong {
      font-weight: bold; /*文本加粗*/
    }
    


    例如,下面的代码使用<strong>标签定义了强调文本:

    <p>我在学习<strong>Bootstrap</strong>,我要掌握<strong>Bootstrap</strong>的所有知识。</p>
    
     

    斜体

    在排版中,除了用加粗来强调突出的文本之外,还可以使用斜体。斜体类似于加粗一样,除了可以给元素设置样式font-style值为italic实现之外,在Bootstrap中还可以通过使用标签<em><i>来实现。
    例如,下面的代码使用了<em><i>标签定义了强调文本:

    <p>我在慕课网上跟<em>大漠</em>一起学习<i>Bootstrap</i>的使用。我一定要学会<i>Bootstrap</i>。</p>
    
     

    强调相关的类

    在Bootstrap中除了使用标签<strong>、<em>等说明正文某些字词、句子的重要性,Bootstrap还定义了一套类名,这里称其为强调类名(类似前面说的“.lead”),这些强调类都是通过颜色来表示强调,具本说明如下:

    • .text-muted:提示,使用浅灰色(#999)
    • .text-primary:主要,使用蓝色(#428bca)
    • .text-success:成功,使用浅绿色(#3c763d)
    • .text-info:通知信息,使用浅蓝色(#31708f)
    • .text-warning:警告,使用黄色(#8a6d3b)
    • .text-danger:危险,使用褐色(#a94442)

    具本源码查看bootstrap.css文件第500行~第532行:

    .text-muted {
    color: #999;
    }
    .text-primary {
    color: #428bca;
    }
    a.text-primary:hover {
    color: #3071a9;
    }
    .text-success {
    color: #3c763d;
    }
    a.text-success:hover {
    color: #2b542c;
    }
    .text-info {
    color: #31708f;
    }
    a.text-info:hover {
    color: #245269;
    }
    .text-warning {
    color: #8a6d3b;
    }
    a.text-warning:hover {
    color: #66512c;
    }
    .text-danger {
    color: #a94442;
    }
    a.text-danger:hover {
    color: #843534;
    }
    
    

    强调相关的类

    在Bootstrap中除了使用标签<strong>、<em>等说明正文某些字词、句子的重要性,Bootstrap还定义了一套类名,这里称其为强调类名(类似前面说的“.lead”),这些强调类都是通过颜色来表示强调,具本说明如下:

    • .text-muted:提示,使用浅灰色(#999)
    • .text-primary:主要,使用蓝色(#428bca)
    • .text-success:成功,使用浅绿色(#3c763d)
    • .text-info:通知信息,使用浅蓝色(#31708f)
    • .text-warning:警告,使用黄色(#8a6d3b)
    • .text-danger:危险,使用褐色(#a94442)

    具本源码查看bootstrap.css文件第500行~第532行:

    .text-muted {
    color: #999;
    }
    .text-primary {
    color: #428bca;
    }
    a.text-primary:hover {
    color: #3071a9;
    }
    .text-success {
    color: #3c763d;
    }
    a.text-success:hover {
    color: #2b542c;
    }
    .text-info {
    color: #31708f;
    }
    a.text-info:hover {
    color: #245269;
    }
    .text-warning {
    color: #8a6d3b;
    }
    a.text-warning:hover {
    color: #66512c;
    }
    .text-danger {
    color: #a94442;
    }
    a.text-danger:hover {
    color: #843534;
    }
    
    <div class="text-muted">.text-muted 效果</div>
    <div class="text-primary">.text-primary效果</div>
    <div class="text-success">.text-success效果</div>
    <div class="text-info">.text-info效果</div>
    <div class="text-warning">.text-warning效果</div>
    <div class="text-danger">.text-danger效果</div>
     

    文本对齐风格

    在排版中离不开文本的对齐方式。在CSS中常常使用text-align来实现文本的对齐风格的设置。其中主要有四种风格:

      ☑  左对齐,取值left

      ☑  居中对齐,取值center

      ☑  右对齐,取值right

      ☑  两端对齐,取值justify

    为了简化操作,方便使用,Bootstrap通过定义四个类名来控制文本的对齐风格:

      ☑   .text-left:左对齐

      ☑   .text-center:居中对齐

      ☑   .text-right:右对齐

      ☑   .text-justify:两端对齐

    具体源码查看bootstrap.css文件第488行~第499行:

    .text-left {
    text-align: left;
    }
    .text-right {
    text-align: right;
    }
    .text-center {
    text-align: center;
    }
    .text-justify {
    text-align: justify;
    }
    
    
    

    例如下面的四行代码,分别定义文本的四种不同的对齐风格:

    <p class="text-left">我居左</p>
    <p class="text-center">我居中</p>
    <p class="text-right">我居右</p>
    <p class="text-justify">我两端对齐</p>
    
    
    特别声明:目前两端对齐在各浏览器下解析各有不同,特别是应用于中文文本的时候。所以项目中慎用。
     
    <div class="text-righ">hello</div>
    <p class="text-left">我居左</p>
    <p class="text-center">我居中</p>
    <p class="text-right">我居右</p>
    <p class="text-justify">There is clearly a need for CSS to be taken seriously by graphic artists. The Zen Garden aims to excite, inspire, and encourage participation. To begin, view some of the existing designs in the list. Clicking on any one will load the style sheet into this very page. The code remains the same, the only thing that has changed is the external .css file. </p>
     
     
     
     

    列表--简介

    在HTML文档中,列表结构主要有三种:有序列表、无序列表和定义列表。具体使用的标签说明如下:
    无序列表

    <ul>
        <li>…</li>
    </ul>
    
    

    有序列表

    <ol>
        <li>…</li>
    </ol>
    
    

    定义列表

    <dl>
        <dt>…</dt>
        <dd>…</dd>
    </dl>
    
    


    Bootstrap根据平时的使用情形提供了六种形式的列表:

       ☑  普通列表

       ☑  有序列表

       ☑  去点列表

       ☑  内联列表

       ☑  描述列表

       ☑  水平描述列表
     
    <ul>
        <li>无序列表信息1</li>
        <li>无序列表信息2</li>
        <li>无序列表信息3</li>
    </ul>
    <ol>
        <li>有序列表信息1</li>
        <li>有序列表信息2</li>
        <li>有序列表信息3</li>
    </ol>
    <dl>
        <dt>定义列表标题</dt>
        <dd>定义列表信息1</dd>
        <dd>定义列表信息2</dd>
    </dl>
     

    表单控件(输入框input)

    每一个表单都是由表单控件组成。离开了控件,表单就失去了意义。接下来的我们简单的来了解Bootstrap框架中表单控件的相关知识。

    单行输入框,常见的文本输入框,也就是inputtype属性值为text。在Bootstrap中使用input时也必须添加type类型,如果没有指定type类型,将无法得到正确的样式,因为Bootstrap框架都是通过input[type=“?”](其中?号代表type类型,比如说text类型,对应的是input[type=“text”])的形式来定义样式的。

    为了让控件在各种表单风格中样式不出错,需要添加类名“form-control”,如:

    <form role="form">
    <div class="form-group">
    <input type="email" class="form-control" placeholder="Enter email">
    </div>
    </form>
    
     
    <form role="form">
      <div class="form-group">
        <input type="email" class="form-control" placeholder="Enter email">
        <input type="password" class="form-control" placeholder="Enter password">
      </div>
     
     

    表单控件(下拉选择框select)

    Bootstrap框架中的下拉选择框使用和原始的一致,多行选择设置multiple属性的值为multiple。Bootstrap框架会为这些元素提供统一的样式风格。如:

    <form role="form">
    <div class="form-group">
      <select class="form-control">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
      </select>
      </div>
      <div class="form-group">
      <select multiple class="form-control">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
      </select>
    </div>
    </form>
    
     
    <form role="form">
      <div class="form-group">
        <select class="form-control">
          <option>1</option>
          <option>2</option>
          <option>3</option>
          <option>4</option>
          <option>5</option>
          </select>
      </div>
      <div class="form-group">
          <select multiple class="form-control">
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
            <option>5</option>
          </select>
      </div>

    表单控件(文本域textarea)

    文本域和原始使用方法一样,设置rows可定义其高度,设置cols可以设置其宽度。但如果textarea元素中添加了类名“form-control”类名,则无需设置cols属性。因为Bootstrap框架中的“form-control”样式的表单控件宽度为100%auto

    <form role="form">
      <div class="form-group">
        <textarea class="form-control" rows="3"></textarea>
      </div>
    </form>
    
     
     

    表单控件(复选框checkbox和单选择按钮radio)

    Bootstrap框架中checkbox和radio有点特殊,Bootstrap针对他们做了一些特殊化处理,主要是checkbox和radio与label标签配合使用会出现一些小问题(最头痛的是对齐问题)。使用Bootstrap框架,开发人员无需考虑太多,只需要按照下面的方法使用即可。

    <form role="form">
    <div class="checkbox">
    <label>
    <input type="checkbox" value="">
    记住密码
    </label>
    </div>
    <div class="radio">
    <label>
    <input type="radio" name="optionsRadios" id="optionsRadios1" value="love" checked>
    喜欢
    </label>
    </div>
    <div class="radio">
    <label>
    <input type="radio" name="optionsRadios" id="optionsRadios2" value="hate">
    不喜欢
    </label>
    </div>
    </form>
    

    运行效果如下或查看右侧结果窗口(案例1):

    从上面的示例,我们可以得知:
    1、不管是checkbox还是radio都使用label包起来了
    2、checkbox连同label标签放置在一个名为“.checkbox”的容器内
    3、radio连同label标签放置在一个名为“.radio”的容器内
    在Bootstrap框架中,主要借助“.checkbox”和“.radio”样式,来处理复选框、单选按钮与标签的对齐方式。源码请查看bootstrap.css文件第1742行~第1762行:

    .radio,
    .checkbox {
    display: block;
    min-height: 20px;
    padding-left: 20px;
    margin-top: 10px;
    margin-bottom: 10px;
    }
    .radio label,
    .checkbox label {
    display: inline;
    font-weight: normal;
    cursor: pointer;
    }
    .radio input[type="radio"],
    .radio-inline input[type="radio"],
    .checkbox input[type="checkbox"],
    .checkbox-inline input[type="checkbox"] {
    float: left;
    margin-left: -20px;
    }
    .radio + .radio,
    .checkbox + .checkbox {
    margin-top: -5px;
    }
    
     

    表单控件(复选框和单选按钮水平排列)

    有时候,为了布局的需要,将复选框和单选按钮需要水平排列。Bootstrap框架也做了这方面的考虑:
    1、如果checkbox需要水平排列,只需要在label标签上添加类名“checkbox-inline”
    2、如果radio需要水平排列,只需要在label标签上添加类名“radio-inline”

    如下所示:

    <form role="form">
      <div class="form-group">
        <label class="checkbox-inline">
          <input type="checkbox"  value="option1">游戏
        </label>
        <label class="checkbox-inline">
          <input type="checkbox"  value="option2">摄影
        </label>
        <label class="checkbox-inline">
        <input type="checkbox"  value="option3">旅游
        </label>
      </div>
      <div class="form-group">
        <label class="radio-inline">
          <input type="radio"  value="option1" name="sex">男性
        </label>
        <label class="radio-inline">
          <input type="radio"  value="option2" name="sex">女性
        </label>
        <label class="radio-inline">
          <input type="radio"  value="option3" name="sex">中性
        </label>
      </div>
    </form>
    

    运行效果如下或查看右侧结果窗口:

    实现源码请查看bootstrap.css文件第1767行~第1780行:

    .radio-inline,
    .checkbox-inline {
    display: inline-block;
    padding-left: 20px;
    margin-bottom: 0;
    font-weight: normal;
    vertical-align: middle;
    cursor: pointer;
    }
    .radio-inline + .radio-inline,
    .checkbox-inline + .checkbox-inline {
    margin-top: 0;
    margin-left: 10px;
    }
    
     

    表单控件(按钮)

    按钮也是表单重要控件之一,制作按钮通常使用下面代码来实现:

      ☑  input[type=submit”]

      ☑  input[type=“button”]

      ☑  input[type=reset”]

      ☑  <button>

    在Bootstrap框架中的按钮都是采用<button>来实现。

    有关于Bootstrap中按钮如何制作,在这里不做过多阐述,因为按钮也是Bootstrap框架中核心部分之一,后面我们专门有一节内容来介绍Bootstrap的按钮。

    这里先让大家看看Bootstrap的按钮长成什么样:

    <table class="table table-bordered table-striped"> 
        <thead> 
          <tr> 
            <th>Button</th> 
            <th>class=""</th> 
            <th>Description</th> 
          </tr> 
        </thead> 
        <tbody> 
          <tr> 
            <td><button class="btn" href="#">Default</button></td> 
            <td><code>btn</code></td> 
            <td>Standard gray button with gradient</td> 
          </tr> 
          <tr> 
            <td><button class="btn btn-primary" href="#">Primary</button></td> 
            <td><code>btn btn-primary</code></td> 
            <td>Provides extra visual weight and identifies the primary action in a set of buttons</td> 
          </tr> 
          <tr> 
            <td><button class="btn btn-info" href="#">Info</button></td> 
            <td><code>btn btn-info</code></td> 
            <td>Used as an alternative to the default styles</td> 
          </tr> 
          <tr> 
            <td><button class="btn btn-success" href="#">Success</button></td> 
            <td><code>btn btn-success</code></td> 
            <td>Indicates a successful or positive action</td> 
          </tr> 
          <tr> 
            <td><button class="btn btn-warning" href="#">Warning</button></td> 
            <td><code>btn btn-warning</code></td> 
            <td>Indicates caution should be taken with this action</td> 
          </tr> 
          <tr> 
            <td><button class="btn btn-danger" href="#">Danger</button></td> 
            <td><code>btn btn-danger</code></td> 
            <td>Indicates a dangerous or potentially negative action</td> 
          </tr> 
          <tr> 
            <td><button class="btn btn-inverse" href="#">Inverse</button></td> 
            <td><code>btn btn-inverse</code></td> 
            <td>Alternate dark gray button, not tied to a semantic action or use</td> 
          </tr> 
        </tbody> 
      </table>
     

    表单控件大小

    前面看到的表单控件都正常的大小。可以通过设置控件的height,line-height,paddingfont-size等属性来实现控件的高度设置。不过Bootstrap框架还提供了两个不同的类名,用来控制表单控件的高度。这两个类名是:
    1、input-sm:让控件比正常大小更小
    2、input-lg:让控件比正常大小更大

    这两个类适用于表单中的inputtextareaselect控件,具体使用如下:

    <input class="form-control input-lg" type="text" placeholder="添加.input-lg,控件变大">
    <input class="form-control" type="text" placeholder="正常大小">
    <input class="form-control input-sm" type="text" placeholder="添加.input-sm,控件变小">
    

    运行效果如下或查看右侧结果窗口:

    源码请查阅bootstrap.css文件第1795~第1824行:

    .input-sm {
    height: 30px;
    padding: 5px 10px;
    font-size: 12px;
    line-height: 1.5;
    border-radius: 3px;
    }
    select.input-sm {
    height: 30px;
    line-height: 30px;
    }
    textarea.input-sm,
    select[multiple].input-sm {
    height: auto;
    }
    .input-lg {
    height: 46px;
    padding: 10px 16px;
    font-size: 18px;
    line-height: 1.33;
    border-radius: 6px;
    }
    select.input-lg {
    height: 46px;
    line-height: 46px;
    }
    textarea.input-lg,
    select[multiple].input-lg {
    height: auto;
    }
    


    从上面的源码中不难发现,不管是“input-sm”还是“input-lg”仅对控件高度做了处理。但往往很多时候,我们需要控件宽度也要做一定的变化处理。这个时候就要借住Bootstrap框架的网格系统。所以你要控制表单宽度,可以像下面这样使用:

    <form role="form" class="form-horizontal">
      <div class="form-group">
      <div class="col-xs-4">
        <input class="form-control input-lg" type="text" placeholder=".col-xs-4">
      </div>
      <div class="col-xs-4">
        <input class="form-control input-lg" type="text" placeholder=".col-xs-4">
      </div>
      <div class="col-xs-4">
        <input class="form-control input-lg" type="text" placeholder=".col-xs-4">
      </div>
      </div>
        …
    </form>
    

    注:网格布局在后面章节中会进行详细讲解。

    运行效果如下或查看右侧结果窗口:

    前面介绍水平表单时说过,如果表单使用了类名“form-horizontal”,其中“form-group”就相当于网格系统中的“row”。换句话说,如果没有这样做,要通过网格系统来控制表单控件宽度,就需要这样使用:

    <div class="row">
    <div class="col-xs-4">
    <input class="form-control input-lg" type="text" placeholder=".col-xs-4">
    </div>
    <div class="col-xs-4">
    <input class="form-control input-lg" type="text" placeholder=".col-xs-4">
    </div>
    <div class="col-xs-4">
    <input class="form-control input-lg" type="text" placeholder=".col-xs-4">
    </div>
    </div>
    
    <h1>案例1</h1>
    <form role="form">
      <div class="form-group">
        <label class="control-label">控件变大</label>
        <input class="form-control input-lg" type="text" placeholder="添加.input-lg,控件变大">
      </div>
      <div class="form-group">
        <label class="control-label">正常大小</label>
        <input class="form-control" type="text" placeholder="正常大小">
      </div> 
      <div class="form-group">
        <label class="control-label">控件变小</label>
        <input class="form-control input-sm" type="text" placeholder="添加.input-sm,控件变小">
      </div>
    </form>
      <br>
      <br>
      <br>
     <h1>案例2</h1>
    <form role="form" class="form-horizontal">
      <div class="form-group">
        <div class="col-xs-4">
          <input class="form-control input-lg" type="text" placeholder=".col-xs-4">
        </div>
        <div class="col-xs-4">
          <input class="form-control input-lg" type="text" placeholder=".col-xs-4">
        </div>
        <div class="col-xs-4">
          <input class="form-control input-lg" type="text" placeholder=".col-xs-4">
        </div>
      </div>
      <div class="form-group">
        <div class="col-xs-6"><input class="form-control" type="text" placeholder=".col-xs-6"></div>
        <div class="col-xs-6"><input class="form-control" type="text" placeholder=".col-xs-6"></div>
       
      </div> 
      <div class="form-group">
        <div class="col-xs-5">
          <input class="form-control input-sm" type="text" placeholder=".col-xs-5">
        </div>
        <div class="col-xs-7">
          <input class="form-control input-sm" type="text" placeholder=".col-xs-7">
        </div>
      </div>
    </form>

    表单控件状态(焦点状态)

    表单主要用来与用户沟通,好的表单就能更好的与用户进行沟通,而好的表单一定离不开表单的控件状态。

    表单状态的作用:

    每一种状态都能给用户传递不同的信息,比如表单有焦点的状态可以告诉用户可以输入或选择东西,禁用状态可以告诉用户不可以输入或选择东西,还有就是表单控件验证状态,可以告诉用户的操作是否正确等。那么在Bootstrap框架中的表单控件也具备这些状态。

    焦点状态是通过伪类“:focus”来实现。Bootstrap框架中表单控件的焦点状态删除了outline的默认样式,重新添加阴影效果。

    源码请查阅bootstrap.css文件第1707行~第1712行:

    .form-control:focus {
    border-color: #66afe9;
    outline: 0;
      -webkit-box-shadow: inset 0 1px 1pxrgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, .6);
    box-shadow: inset 0 1px 1pxrgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, .6);
    }
    
    

     从源码中我们可以看出,要让控件在焦点状态下有上面样式效果,需要给控件添加类名“form-control”:

    <form role="form" class="form-horizontal">
      <div class="form-group">
        <div class="col-xs-6">
          <input class="form-control input-lg" type="text" placeholder="不是焦点状态下效果">
        </div>
        <div class="col-xs-6">
          <input class="form-control input-lg" type="text" placeholder="焦点点状态下效果">
        </div>
      </div>
    </form>
    

    运行效果如下或查看右侧结果窗口:

    (鼠标单击输入框,使其获得焦点就可以看到加入蓝色边框效果)

    在Bootstrap框架中,fileradiocheckbox控件在焦点状态下的效果也与普通的input控件不太一样,主要是因为Bootstrap对他们做了一些特殊处理:
    /*源码查看boostrap.css文件第1676行~第1682行*/

    input[type="file"]:focus,
    input[type="radio"]:focus,
    input[type="checkbox"]:focus {
    outline: thin dotted;
    outline: 5px auto -webkit-focus-ring-color;
    outline-offset: -2px;
    }
    

    表单控件状态(禁用状态)

    Bootstrap框架的表单控件的禁用状态和普通的表单禁用状态实现方法是一样的,在相应的表单控件上添加属性“disabled”。和其他表单的禁用状态不同的是,Bootstrap框架做了一些样式风格的处理:
    /*源码请查看bootstrap.css文件第1723行~第1729行*/

    .form-control[disabled],
    .form-control[readonly],
    fieldset[disabled] .form-control {
    cursor: not-allowed;
    background-color: #eee;
    opacity: 1;
    }
    

    使用方法为:只需要在需要禁用的表单控件上加上“disabled”即可:

    <input class="form-control" type="text" placeholder="表单已禁用,不能输入" disabled>
    

    运行效果如下或查看右侧结果窗口:

    在使用了“form-control”的表单控件中,样式设置了禁用表单背景色为灰色,而且手型变成了不准输入的形状。如果控件中不使用类名“form-control”,禁用的控件只会有一个不准输入的手型出来。
    /*源码请查阅bootstrap.css文件第1781行~第1794行*/

    input[type="radio"][disabled],
    input[type="checkbox"][disabled],
    .radio[disabled],
    .radio-inline[disabled],
    .checkbox[disabled],
    .checkbox-inline[disabled],
    fieldset[disabled] input[type="radio"],
    fieldset[disabled] input[type="checkbox"],
    fieldset[disabled] .radio,
    fieldset[disabled] .radio-inline,
    fieldset[disabled] .checkbox,
    fieldset[disabled] .checkbox-inline {
    cursor: not-allowed;
    }
    

    在Bootstrap框架中,如果fieldset设置了disabled属性,整个域都将处于被禁用状态。

    <form role="form">
    <fieldset disabled>
      <div class="form-group">
      <label for="disabledTextInput">禁用的输入框</label>
        <input type="text" id="disabledTextInput" class="form-control" placeholder="禁止输入">
      </div>
      <div class="form-group">
      <label for="disabledSelect">禁用的下拉框</label>
        <select id="disabledSelect" class="form-control">
      <option>不可选择</option>
      </select>
      </div>
      <div class="checkbox">
      <label>
        <input type="checkbox">无法选择
      </label>
      </div>
      <button type="submit" class="btnbtn-primary">提交</button>
    </fieldset>
    </form>
    

    运行效果如下或查看右侧结果窗口:

    据说对于整个禁用的域中,如果legend中有输入框的话,这个输入框是无法被禁用的。我们一起来验证一下:

    <form role="form">
    <fieldset disabled>
    <legend><input type="text" class="form-control" placeholder="显然我颜色变灰了,但是我没被禁用,不信?单击试一下" /></legend>
        …
    </fieldset>
    </form>
    

    运行效果如下或查看右侧结果窗口:

     <h3>示例1</h3>
    <form role="form" class="form-horizontal">
      <div class="form-group">
        <div class="col-xs-6">
          <input class="form-control input-lg" type="text" placeholder="不是焦点状态下效果">
        </div>
        <div class="col-xs-6">
          <input class="form-control input-lg" id="disabledInput" type="text" placeholder="表单已被禁用,不可输入" disabled>
        </div>
      </div>
    </form>
    <br>
    <br>
    <br>
    <h3>示例2</h3>  
    <form role="form">
      <fieldset disabled>
        <div class="form-group">
          <label for="disabledTextInput">禁用的输入框</label>
          <input type="text" id="disabledTextInput" class="form-control" placeholder="禁止输入">
        </div>
        <div class="form-group">
          <label for="disabledSelect">禁用的下拉框</label>
          <select id="disabledSelect" class="form-control">
            <option>不可选择</option>
          </select>
        </div>
        <div class="checkbox">
          <label>
            <input type="checkbox"> 无法选择
          </label>
        </div>
        <button type="submit" class="btn btn-primary">提交</button>
      </fieldset>
    </form>
    <br>
    <br>
    <br>
    <h3>示例3</h3>
    <form role="form">
      <fieldset disabled>
        <legend><input type="text" class="form-control" placeholder="显然我颜色变灰了,但是我没被禁用,不信?单击试一下" /></legend>
        <div class="form-group">
          <label for="disabledTextInput">禁用的输入框</label>
          <input type="text" id="disabledTextInput" class="form-control" placeholder="禁止输入">
        </div>
        <div class="form-group">
          <label for="disabledSelect">禁用的下拉框</label>
          <select id="disabledSelect" class="form-control">
            <option>不可选择</option>
          </select>
        </div>
        <div class="checkbox">
          <label>
            <input type="checkbox"> 无法选择
          </label>
        </div>
        <button type="submit" class="btn btn-primary">提交</button>
      </fieldset>
    </form> 
     
     

    表单控件状态(验证状态)

    在制作表单时,不免要做表单验证。同样也需要提供验证状态样式,在Bootstrap框架中同样提供这几种效果。
    1、.has-warning:警告状态(黄色)
    2、.has-error:错误状态(红色)
    3、.has-success:成功状态(绿色)

    使用的时候只需要在form-group容器上对应添加状态类名。

    <form role="form">
    <div class="form-group has-success">
      <label class="control-label" for="inputSuccess1">成功状态</label>
      <input type="text" class="form-control" id="inputSuccess1" placeholder="成功状态" >
    </div>
    <div class="form-group has-warning">
      <label class="control-label" for="inputWarning1">警告状态</label>
      <input type="text" class="form-control" id="inputWarning1" placeholder="警告状态">
    </div>
    <div class="form-group has-error">
      <label class="control-label" for="inputError1">错误状态</label>
      <input type="text" class="form-control" id="inputError1" placeholder="错误状态">
    </div>
    </form>
    

    运行效果如下或查看右侧结果窗口:

    从效果可以看出,三种状态下效果都是一样的,只是颜色不一样而以。

    源码请查阅bootstrap.css文件第1481行~第1918行。

    其他两种状态省略源码不在此展示。
    很多时候,在表单验证的时候,不同的状态会提供不同的icon,比如成功是一个对号(√),错误是一个叉号(×)等。在Bootstrap框中也提供了这样的效果。如果你想让表单在对应的状态下显示icon出来,只需要在对应的状态下添加类名“has-feedback”。请注意,此类名要与“has-error”、“has-warning”和“has-success”在一起:

    <form role="form">
    <div class="form-group has-success has-feedback">
      <label class="control-label" for="inputSuccess1">成功状态</label>
      <input type="text" class="form-control" id="inputSuccess1" placeholder="成功状态" >
      <span class="glyphiconglyphicon-ok form-control-feedback"></span>
    </div>
    <div class="form-group has-warning has-feedback">
      <label class="control-label" for="inputWarning1">警告状态</label>
      <input type="text" class="form-control" id="inputWarning1" placeholder="警告状态">
      <span class="glyphiconglyphicon-warning-sign form-control-feedback"></span>
    </div>
    <div class="form-group has-error has-feedback">
      <label class="control-label" for="inputError1">错误状态</label>
      <input type="text" class="form-control" id="inputError1" placeholder="错误状态">
      <span class="glyphiconglyphicon-remove form-control-feedback"></span>
    </div>
    </form>
    

    运行效果如下或查看右侧结果窗口:

    从效果图中可以看出,图标都居右。在Bootstrap的小图标都是使用@font-face来制作(后面的内容中将会着重用一节内容来介绍)。而且必须在表单中添加了一个span元素:

    <span class="glyphiconglyphicon-warning-sign form-control-feedback"></span>

    通过定位来实现,具体源码请查阅bootstrap.css文件第1825行~第1840行。

     

    表单提示信息

    平常在制作表单验证时,要提供不同的提示信息。在Bootstrap框架中也提供了这样的效果。使用了一个"help-block"样式,将提示信息以块状显示,并且显示在控件底部。

    <form role="form">
    <div class="form-group has-success has-feedback">
      <label class="control-label" for="inputSuccess1">成功状态</label>
      <input type="text" class="form-control" id="inputSuccess1" placeholder="成功状态" >
      <span class="help-block">你输入的信息是正确的</span>
      <span class="glyphiconglyphicon-ok form-control-feedback"></span>
    </div>
      …
    </form>
    

    运行效果如下或查看右侧结果窗口:

    具体样式代码请查看bootstrap.css文件第1922行~第1927行:

    .help-block {
    display: block;
    margin-top: 5px;
    margin-bottom: 10px;
    color: #737373;
    }
    

    在Bootstrap V2.x版本中还提供了一个行内提示信息,其使用了类名“help-inline”。一般让提示信息显示在控件的后面,也就是同一水平显示。如果你想在BootstrapV3.x版本也有这样的效果,你可以添加这段代码:

    .help-inline{
      display:inline-block;
      padding-left:5px;
      color: #737373;
    }
    

    如果你不想为bootstrap.css增加自己的代码,而且设计又有这种样的需求,那么只能借助于Bootstrap的网格系统。(网格系统在后面的章节中会详细讲解)

    <form role="form">
    <div class="form-group">
    <label class="control-label" for="inputSuccess1">成功状态</label>
    <div class="row">
    <div class="col-xs-6">
    <input type="text" class="form-control" id="inputSuccess1" placeholder="成功状态" >
    </div>
    <span class="col-xs-6 help-block">你输入的信息是正确的</span>
    </div>
    </div>
    </form>
    

    运行效果如下或查看右侧结果窗口:

    结束语:有关于Bootstrap框架中表单的运用除了按钮部分,到此就算是介绍完了。如果你觉得这样的表单效果并不是你需要的,你完全可以通过forms.less或者_forms.scss文件进行定制,然后重新编译就可以得到你需要的表单效果。在接下来的一节中,我们Bootstrap框架中另一个核心内容——按钮

     

    按钮

    按钮也是Bootstrap框架核心内容之一。因为按钮是Web制作中不可缺少的东西。而且不同的Web页面具有不同的按钮风格,甚至说同一个Web网站或应用程序具有多种按钮风格,比如说不同的按钮颜色、大小和状态等。那么Bootstrap框架也考虑了这些因素,接下来的内容我们一起来探讨Bootstrap框架中的另一核心部分内容——按钮。

    Bootstrap框架的按钮也是一个独立部分,我们同样在不同的版本之中能找到对应的代码:

    1. LESS版本:查看源文件buttons.less
    2. Sass版本:查看源文件_buttons.scss
    3. 已编译版本:查看源文件bootstrap.css文件第1992行~第2353行
     
       <button class="btn" type="button">基础按钮.btn</button> 
       <button class="btn btn-default" type="button">默认按钮.btn-default</button>
       <button class="btn btn-primary" type="button">主要按钮.btn-primary</button>
       <button class="btn btn-success" type="button">成功按钮.btn-success</button>
       <button class="btn btn-info" type="button">信息按钮.btn-info</button>
       <button class="btn btn-warning" type="button">警告按钮.btn-warning</button>
       <button class="btn btn-danger" type="button">危险按钮.btn-danger</button>
       <button class="btn btn-link" type="button">链接按钮.btn-link</button>
     
     

    基本按钮

    Bootstrap框架V3.x版本的基本按钮和V2.x版本的基本按钮一样,都是通过类名“btn”来实现。不同的是在V3.x版本要简约很多,去除了V2.x版本中的大量的CSS3中的部分特效,比如说文本阴影(text-shadow)、渐变背景(background-image)、边框阴影(box-shadow)等。
    难能可贵的是,Bootstrap框架中的考虑了不同浏览器的解析差异,进行了比较安全的兼容性处理,使按钮效果在不同的浏览器中所呈现的效果基本相同。

    源码请查阅bootstrap.css文件第1992行~第2010行:

    .btn {
    display: inline-block;
    padding: 6px 12px;
    margin-bottom: 0;
    font-size: 14px;
    font-weight: normal;
    line-height: 1.42857143;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    cursor: pointer;
      -webkit-user-select: none;
         -moz-user-select: none;
          -ms-user-select: none;
    user-select: none;
    background-image: none;
    border: 1px solid transparent;
    border-radius: 4px;
    }
    

    Bootstrap框架的按钮使用非常的简单,使用方式如下:

    <button class="btn" type="button">我是一个基本按钮</button>
    

    运行效果如下或查看右侧结果窗口:

     

    默认按钮

    Bootstrap框架首先通过基础类名“.btn”定义了一个基础的按钮风格,然后通过“.btn-default”定义了一个默认的按钮风格。默认按钮的风格就是在基础按钮的风格的基础上修改了按钮的背景颜色边框颜色文本颜色
    /*源码请查阅bootstrap.css文件第2040行~第2044行*/

    .btn-default {
    color: #333;
    background-color: #fff;
    border-color: #ccc;
    }
    

    使用默认按钮风格也非常的简单,只需要在基础按钮“btn”的基础上增加类名“btn-default”即可:

    <button class="btn btn-default" type="button">默认按钮</button>
    

    运行效果如下或查看右侧结果窗口:

     

    多标签支持

    一般制作按钮除了使用<button>标签元素之外,还可以使用<input type="submit">和<a>标签等。同样,在Bootstrap框架中制作按钮时,除了刚才所说的这些标签元素之外,还可以使用在其他的标签元素上,唯一需要注意的是,要在制作按钮的标签元素上添加类名“btn”。如果不添加是不会有任何按钮效果。

    我们一起来看看其他标签制作的基本按钮效果:

    <button class="btn btn-default" type="button">button标签按钮</button>
    <input type="submit" class="btn btn-default" value="input标签按钮"/>
    <a href="##" class="btn btn-default">a标签按钮</a>
    <span class="btn btn-default">span标签按钮</span>
    <div class="btn btn-default">div标签按钮</div>
    

    运行效果如下或查看右侧结果窗口:

    注意:虽然在Bootstrap框架中使用任何标签元素都可以实现按钮风格,但个人并不建议这样使用,为了避免浏览器兼容性问题,个人强烈建议使用buttona标签来制作按钮。

     

    定制风格

    在介绍按钮开篇就说过,Web页面可能会有不同的按钮风格。那么在Bootstrap框架也考虑了。在Bootstrap框架中除了默认的按钮风格之外,还有其他六种按钮风格,每种风格的其实都一样,不同之处就是按钮的背景颜色、边框颜色和文本颜色。

    在Bootstrap框架中不同的按钮风格都是通过不同的类名来实现,在使用过程中,开发者只需要选择不同的类名即可:

    使用起来就很简单,就像前面介绍的默认按钮一样的使用方法,只需要在基础按钮“.btn”基础上追加对应的类名,就可以得到需要的按钮风格。如:

    <button class="btn" type="button">基础按钮.btn</button>
    <button class="btn btn-default" type="button">默认按钮.btn-default</button>
    <button class="btn btn-primary" type="button">主要按钮.btn-primary</button>
    <button class="btn btn-success" type="button">成功按钮.btn-success</button>
    <button class="btn btn-info" type="button">信息按钮.btn-info</button>
    <button class="btn btn-warning" type="button">警告按钮.btn-warning</button>
    <button class="btn btn-danger" type="button">危险按钮.btn-danger</button>
    <button class="btn btn-link" type="button">链接按钮.btn-link</button>
    

    运行效果如下或查看右侧结果窗口:

    有关于按钮风格定制的源码可以查阅bootstrap.css文件第2081行~第2318行。

     

    按钮大小

    上一节介绍了按钮的定制风格,也就是如何实现Web页面中多种风格按钮。在Bootstrap框架中,对于按钮的大小,也是可以定制的。类似于input一样,通过在基础按钮“.btn”的基础上追加类名来控制按钮的大小。

    在Bootstrap框架中提供了三个类名来控制按钮大小:

    从上表中不难发现,在Bootstrap框架中控制按钮的大小都是通过修改按钮的paddingline-heightfont-sizeborder-radius几个属性。
    /*源码查阅bootstrap.css文件中第2319行~第2339行*/

    .btn-lg,
    .btn-group-lg> .btn {
    padding: 10px 16px;
    font-size: 18px;
    line-height: 1.33;
    border-radius: 6px;
    }
    .btn-sm,
    .btn-group-sm> .btn {
    padding: 5px 10px;
    font-size: 12px;
    line-height: 1.5;
    border-radius: 3px;
    }
    .btn-xs,
    .btn-group-xs> .btn {
    padding: 1px 5px;
    font-size: 12px;
    line-height: 1.5;
    border-radius: 3px;
    }
    

    那么在实际使用中,这几个类名可以配合按钮中其他颜色类名一起使用,但唯一一点不能缺少“.btn”类名:

    <button class="btn btn-primary btn-lg" type="button">大型按钮.btn-lg</button>
    <button class="btn btn-primary" type="button">正常按钮</button>
    <button class="btn btn-primary btn-sm" type="button">小型按钮.btn-sm</button>
    <button class="btn btn-primary btn-xs" type="button">超小型按钮.btn-xs</button>
    

    效果如下图或查看右侧结果窗口:

     

    块状按钮

    从前面几节的内容中,大家可能发现了,每个示例中的按钮宽度都是依靠按钮文本和padding的值来决定。但有时候在制作按钮的时候需要按钮宽度充满整个父容器(100%),特别是在移动端的制作中。那么前面的方法我们都无法很好的实现,除非重新定义按钮的宽度。其实在Bootstrap中并不需要这样做,Bootstrap框架中提供了一个类名“btn-block”。按钮使用这个类名就可以让按钮充满整个容器,并且这个按钮不会有任何的paddingmargin值。在实际当中,常把这种按钮称为块状按钮。具体源码请查阅bootstrap.css文件第2340行~第2353行:

    .btn-block {
    display: block;
     100%;
    padding-right: 0;
    padding-left: 0;
    }
    .btn-block + .btn-block {
    margin-top: 5px;
    }
    input[type="submit"].btn-block,
    input[type="reset"].btn-block,
    input[type="button"].btn-block {
     100%;
    }
    

    使用方法和前面的类似,只需要在原按钮类名上添加“.btn-block”类名,当然“.btn”类名是不可或缺的:

    <button class="btnbtn-primary btn-lg btn-block" type="button">大型按钮.btn-lg</button>
    <button class="btnbtn-primary btn-block" type="button">正常按钮</button>
    <button class="btnbtn-primary btn-sm btn-block" type="button">小型按钮.btn-sm</button>
    <button class="btnbtn-primary btn-xs btn-block" type="button">超小型按钮.btn-xs</button>
    

    运行效果如下:

     

    按钮状态——活动状态

    Bootstrap框架针对按钮的状态做了一些特殊处理。在Bootstrap框架中针对按钮的状态效果主要分为两种:活动状态禁用状态

    Bootstrap按钮的活动状态主要包括按钮的悬浮状态(:hover)点击状态(:active)焦点状态(:focus)几种。
    /*源码请查看bootstrap.css文件第2011行~第2029行*/

    .btn:focus,
    .btn:active:focus,
    .btn.active:focus {
    outline: thin dotted;
    outline: 5px auto -webkit-focus-ring-color;
    outline-offset: -2px;
    }
    .btn:hover,
    .btn:focus {
    color: #333;
    text-decoration: none;
    }
    .btn:active,
    .btn.active {
    background-image: none;
    outline: 0;
      -webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, .125);
    box-shadow: inset 0 3px 5px rgba(0, 0, 0, .125);
    }
    

    而且不同风格下的按钮都具有这几种状态效果,只是颜色做了一定的调整,我们以默认风格(btn-default)为例:
    /*源码请查看bootstrap.css文件第2045行~第2058行*/

    .btn-default:hover,
    .btn-default:focus,
    .btn-default:active,
    .btn-default.active,
    .open .dropdown-toggle.btn-default {
    color: #333;
    background-color: #ebebeb;
    border-color: #adadad;
    }
    .btn-default:active,
    .btn-default.active,
    .open .dropdown-toggle.btn-default {
    background-image: none;
    }
    

    当按钮处理正在点击状态(也就是鼠标按下的未松开的状态),对于<button>元素是通过“:active”伪类实现,而对于<a>这样的标签元素则是通过添加类名“.active”来实现。

    按钮状态——禁用状态

    和input等表单控件一样,在Bootstrap框架的按钮中也具有禁用状态的设置。禁用状态与其他状态按钮相比,就是背景颜色的透明度做了一定的处理,opcity的值从100%调整为65%。

    在Bootstrap框架中,要禁用按钮有两种实现方式:

    方法1:在标签中添加disabled属性

    方法2:在元素标签中添加类名“disabled”

    两者的主要区别是:

    “.disabled”样式不会禁止按钮的默认行为,比如说提交和重置行为等。如果想要让这样的禁用按钮也能禁止按钮的默认行为,则需要通过JavaScript这样的语言来处理。对于<a>标签也存在类似问题,如果通过类名“.disable”来禁用按钮,其链接行为是无法禁止。而在元素标签中添加“disabled”属性的方法是可以禁止元素的默认行为的。

    下面是两种方法的举例:

    <button class="btnbtn-primary btn-lgbtn-block" type="button" disabled="disabled">通过disabled属性禁用按钮</button>
    <button class="btnbtn-primary btn-block disabled" type="button">通过添加类名disabled禁用按钮</button>
    <button class="btnbtn-primary btn-smbtn-block" type="button">未禁用的按钮</button>
    

    运行效果如下或查看右侧结果窗口:

    对应的样式代码请查阅bootstrap.css文件第2030行~第2039行:

    .btn.disabled,
    .btn[disabled],
    fieldset[disabled] .btn {
    pointer-events: none;
    cursor: not-allowed;
    filter: alpha(opacity=65);
      -webkit-box-shadow: none;
    box-shadow: none;
    opacity: .65;
    }
    

    同样的,其他风格按钮也具有这样的效果,只是颜色做了一定的调整,比如信息按钮(.btn-info)所示:
    /*源码请查看bootstrap.css文件第2182行~第2199行*/

    .btn-info.disabled,
    .btn-info[disabled],
    fieldset[disabled] .btn-info,
    .btn-info.disabled:hover,
    .btn-info[disabled]:hover,
    fieldset[disabled] .btn-info:hover,
    .btn-info.disabled:focus,
    .btn-info[disabled]:focus,
    fieldset[disabled] .btn-info:focus,
    .btn-info.disabled:active,
    .btn-info[disabled]:active,
    fieldset[disabled] .btn-info:active,
    .btn-info.disabled.active,
    .btn-info[disabled].active,
    fieldset[disabled] .btn-info.active {
    background-color: #5bc0de;
    border-color: #46b8da;
    }
    
    到此有关于按钮的基础知识就算是介绍完了,同样的,大家可以通过buttons.less或者_buttons.scss来自定义按钮风格。
     

    图像

    图像在网页制作中也是常要用到的元素,在Bootstrap框架中对于图像的样式风格提供以下几种风格:

    1、img-responsive:响应式图片,主要针对于响应式设计
    2、img-rounded:圆角图片
    3、img-circle:圆形图片
    4、img-thumbnail:缩略图片

    使用方法:

    使用方法非常简单,只需要在<img>标签上添加对应的类名,如下代码:

    <img  alt="140x140" src="http://placehold.it/140x140">
    <img  class="img-rounded" alt="140x140" src="http://placehold.it/140x140">
    <img  class="img-circle" alt="140x140" src="http://placehold.it/140x140">
    <img  class="img-thumbnail" alt="140x140" src="http://placehold.it/140x140">
    <img  class="img-responsive" alt="140x140" src="http://placehold.it/140x140">
    

    运行效果如下或查看右侧结果窗口:

    每种状态对应的源码可以查阅bootstrap.css文件第306行~第335行:

    img {
    vertical-align: middle;
    }
    .img-responsive,
    .thumbnail>img,
    .thumbnail a >img,
    .carousel-inner > .item >img,
    .carousel-inner > .item > a >img {
    display: block;
    max- 100%;
    height: auto;
    }
    .img-rounded {
    border-radius: 6px;
    }
    .img-thumbnail {
    display: inline-block;
    max- 100%;
    height: auto;
    padding: 4px;
    line-height: 1.42857143;
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 4px;
      -webkit-transition: all .2s ease-in-out;
    transition: all .2s ease-in-out;
    }
    .img-circle {
    border-radius: 50%;
    }
    

    设置图片大小:

    由于样式没有对图片做大小上的样式限制,所以在实际使用的时候,需要通过其他的方式来处理图片大小。比如说控制图片容器大小。(注意不可以通过css样式直接修改img图片的大小,这样操作就不响应了)

    注意:

    对于圆角图片和圆形图片效果,因为是使用了CSS3的圆角样式来实现的,所以注意对于IE8以及其以下版本不支持,是没有圆角效果的。

    Bootstrap框架为了大家更好的维护图像的样式,同样将这部分样式独立出来:
    1、LESS版本,可以查阅scaffolding.less
    2、Sass版本,可以查阅_scaffolding.scss
    大家可以修改其中之一,重新编译就可以得到自己需要的图片样式效果。

     

     

     

    图标(一)

    200个icon

    这里说的图标就是Web制作中常看到的小icon图标,可以说这些小icon图标是一个优秀Web中不可缺少的一部分,起到画龙点睛的效果。在Bootstrap框架中也为大家提供了近200个不同的icon图片,而这些图标都是使用CSS3的@font-face属性配合字体来实现的icon效果。

    放心使用

    在具体介绍Bootstrap的icon图标之前,我们首先要感谢glyphicons.com网站,因为Bootstrap框架中图标都是glyphicons.com这个商业网站提供的,并且免费授权给Bootstrap框架使用,所以大家可以放心使用在自己的项目当中。

    Bootstrap框架将内部样式也提取出来:
    1、LESS版本:对应源文件为glyphicons.less文件
    2、Sass版本:对应源文件为_glyphicons.scss文件
    3、编译后的Bootstrap版本:查看bootstrap.css文件第2375行~第2992行

    原理分析

    Bootstrap框架中的图标都是字体图标,其实现原理就是通过@font-face属性加载了字体。
    /*源码请查看bootstrap.css文件第2357行~第2380行*/

    @font-face {
    font-family: 'Glyphicons Halflings';
    src: url('../fonts/glyphicons-halflings-regular.eot');
    src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('../fonts/glyphicons-halflings-regular.woff') format('woff'), url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'), url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
    }
    

    大家或许会问,这些字体我去哪里获取。如果你是从前面一直阅读过来,我们在介绍文件结构那一节就已介绍过。在Bootstrap框架中有一个fonts的目录,这个目录中提供的字体文件就是用于制作icon的字体文件。
    自定义完字体之后,需要对icon设置一个默认样式,在Bootstrap框架中是通过给元素添加“glyphicon”类名来实现,然后通过伪元素“:before”的“content”属性调取对应的icon编码:
    /*源码请查看bootstrap.css文件第2381行~第2992行*/

    .glyphicon {
    position: relative;
    top: 1px;
    display: inline-block;
    font-family: 'Glyphicons Halflings';
    font-style: normal;
    font-weight: normal;
    line-height: 1;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
    }
    .glyphicon-asterisk:before {
    content: "2a";
    }
    ….
    
         <span class="glyphicon glyphicon-search"></span>
         <span class="glyphicon glyphicon-asterisk"></span>
         <span class="glyphicon glyphicon-plus"></span>
         <span class="glyphicon glyphicon-cloud"></span>
     

    图标(二)

    在网页中使用图标也非常的简单,在任何内联元素上应用所对应的样式即可:

    <span class="glyphicon glyphicon-search"></span>
    <span class="glyphicon glyphicon-asterisk"></span>
    <span class="glyphicon glyphicon-plus"></span>
    <span class="glyphicon glyphicon-cloud"></span>
    

    运行效果如下或查看右侧结果窗口:

    所有icon都是以”glyphicon-”前缀的类名开始,然后后缀表示图标的名称。具体说明如下:

    所有名称查看:

    请点击:http://getbootstrap.com/components/#glyphicons 链接查阅或根据bootstrap.css文件第2393行~第2992行查阅。

    特别说明

    除了使用glyphicon.com提供的图标之外,还可以使用第三方为Bootstrap框架设计的图标字体,如Font Awesome(http://www.bootcss.com/p/font-awesome/)。使用方法和上面介绍的一样,不过记得将字体下载到你本地。 感兴趣的可以阅读官网相关介绍。

    网格系统

    实现原理

    网格系统的实现原理非常简单,仅仅是通过定义容器大小,平分12份(也有平分成24份或32份,但12份是最常见的),再调整内外边距,最后结合媒体查询,就制作出了强大的响应式网格系统。Bootstrap框架中的网格系统就是将容器平分成12份。

    在使用的时候大家可以根据实际情况重新编译LESS(或Sass)源码来修改12这个数值(也就是换成24或32,当然你也可以分成更多,但不建议这样使用)。

    (这一章节注意效果要在全屏状态下查看,鼠标滑过结果窗口时可单击出现的全屏按钮)

     

    工作原理

    Bootstrap框架的网格系统工作原理如下:

    1、数据行(.row)必须包含在容器(.container)中,以便为其赋予合适的对齐方式和内距(padding)。如:

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

    2、在行(.row)中可以添加列(.column),但列数之和不能超过平分的总列数,比如12。如:

    <div class="container">
    <div class="row">
      <div class="col-md-4"></div>
      <div class="col-md-8"></div>
    

    3、具体内容应当放置在列容器(column)之内,而且只有列(column)才可以作为行容器(.row)的直接子元素

    4、通过设置内距(padding)从而创建列与列之间的间距。然后通过为第一列和最后一列设置负值的外距(margin)来抵消内距(padding)的影响

    为了更好的理解Bootstrap框架的网格系统工作原理,我们来看一张草图:

    简单对图解释一下:

    1、最外边框,带有一大片白色区域,就是相当于浏览器的可视区域。在Bootstrap框架的网格系统中带有响应式效果,其带有四种类型的浏览器(超小屏,小屏,中屏和大屏),其断点(像素的分界点)是768px、992px和1220px。

    2、第二个边框(1)相当于容器(.container)。针对不同的浏览器分辨率,其宽度也不一样:自动、750px、970px和1170px。在bootstrap.css的第736行~第756行进行设置:

    .container {
      padding-right: 15px;
      padding-left: 15px;
      margin-right: auto;
      margin-left: auto;
      @media (min- 768px) {
      .container {
         750px;
      }
      @media (min- 992px) {
      .container {
         970px;
      }
      @media (min- 1200px) {
      .container {
         1170px;
      }
    

    3、2号横条阐述的是,将容器的行(.row)平分了12等份,也就是列。每个列都有一个“padding-left:15px”(图中粉红色部分)和一个“padding-right:15px”(图中紫色部分)。这样也导致了第一个列的padding-left和最后一列的padding-right占据了总宽度的30px,从而致使页面不美观,当然,如果你需要留有一定的间距,这个做法是不错的。如bootstrap.css中第767行~第772行所示:

    .col-xs-1, .col-sm-1, .col-md-1, .col-lg-1, .col-xs-2, .col-sm-2, .col-md-2, .col-lg-2, .col-xs-3, .col-sm-3, .col-md-3, .col-lg-3, .col-xs-4, .col-sm-4, .col-md-4, .col-lg-4, .col-xs-5, .col-sm-5, .col-md-5, .col-lg-5, .col-xs-6, .col-sm-6, .col-md-6, .col-lg-6, .col-xs-7, .col-sm-7, .col-md-7, .col-lg-7, .col-xs-8, .col-sm-8, .col-md-8, .col-lg-8, .col-xs-9, .col-sm-9, .col-md-9, .col-lg-9, .col-xs-10, .col-sm-10, .col-md-10, .col-lg-10, .col-xs-11, .col-sm-11, .col-md-11, .col-lg-11, .col-xs-12, .col-sm-12, .col-md-12, .col-lg-12 {
      position: relative;
      min-height: 1px;
      padding-right: 15px;
      padding-left: 15px;
    

    4、3号横条就是行容器(.row),其定义了“margin-left”和”margin-right”值为”-15px”,用来抵消第一个列的左内距和最后一列的右内距。在bootstrap.css的第763行~第767行可以看到:

    .row {
      margin-right: -15px;
      margin-left: -15px;
    

    5、将行与列给合在一起就能看到横条4的效果。也就是我们期望看到的效果,第一列和最后一列与容器(.container)之间没有间距。

    横条5只是想向大家展示,你可以根据需要,任意组合列与列,只是他们的组合数之和不要超过总列数。

     
    <div class="container">
      <div class="row">
        <div class="col-md-4">.col-md-4</div>
        <div class="col-md-8">.col-md-8</div>
      </div>
      <div class="row">
        <div class="col-md-4">.col-md-4</div>
        <div class="col-md-4">.col-md-4</div>
        <div class="col-md-4">.col-md-4</div>
      </div>
      <div class="row">
        <div class="col-md-3">.col-md-3</div>
        <div class="col-md-6">.col-md-6</div>
        <div class="col-md-3">.col-md-3</div>
      </div>
    </div>
     
     
     

    基本用法

    网格系统用来布局,其实就是列的组合。Bootstrap框架的网格系统中有四种基本的用法。由于Bootstrap框架在不同屏幕尺寸使用了不同的网格样式,在这一节中所涉及到的示例,我们都以中屏(970px)为例进行介绍,其他屏幕的使用也类似这一种。关于屏幕尺寸如下图:

    1、列组合

    列组合简单理解就是更改数字来合并列(原则:列总和数不能超12),有点类似于表格的colspan属性,例如:

    <div class="container">
      <div class="row">
        <div class="col-md-4">.col-md-4</div>
        <div class="col-md-8">.col-md-8</div>
      </div>
      <div class="row">
        <div class="col-md-4">.col-md-4</div>
        <div class="col-md-4">.col-md-4</div>
        <div class="col-md-4">.col-md-4</div>
      </div>
      <div class="row">
        <div class="col-md-3">.col-md-3</div>
        <div class="col-md-6">.col-md-6</div>
        <div class="col-md-3">.col-md-3</div>
     </div>
    </div>
    

    使用上面的结构,你将看到下图的效果:

    (在右侧结果窗口中查看时需要设置为全屏)

    实现列组合方式非常简单,只涉及两个CSS两个特性:浮动宽度百分比。在bootstrap.css文件的第1088行~1126行:

    /*确保所有列左浮动*/

    .col-md-1, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6, .col-md-7, .col-md-8, .col-md-9, .col-md-10, .col-md-11, .col-md-12 {
        float: left;
     }
    

    /*定义每个列组合的宽度(使用的百分比)*/

      .col-md-12 {
         100%;
      }
      .col-md-11 {
         91.66666667%;
      }
      .col-md-10 {
         83.33333333%;
      }
      .col-md-9 {
         75%;
      }
      .col-md-8 {
         66.66666667%;
      }
      .col-md-7 {
         58.33333333%;
      }
      .col-md-6 {
         50%;
      }
      .col-md-5 {
         41.66666667%;
      }
      .col-md-4 {
         33.33333333%;
      }
      .col-md-3 {
         25%;
      }
      .col-md-2 {
         16.66666667%;
      }
      .col-md-1 {
         8.33333333%;
      }
    
     
     

    列偏移

    有的时候,我们不希望相邻的两个列紧靠在一起,但又不想使用margin或者其他的技术手段来。这个时候就可以使用列偏移(offset)功能来实现。使用列偏移也非常简单,只需要在列元素上添加类名“col-md-offset-*”(其中星号代表要偏移的列组合数),那么具有这个类名的列就会向右偏移。例如,你在列元素上添加“col-md-offset-4”,表示该列向右移动4个列的宽度。

    <div class="container">
    <div class="row">
    <div class="col-md-4">.col-md-4</div>
    <div class="col-md-2 col-md-offset-4">列向右移动四列的间距</div>
    <div class="col-md-2">.col-md-3</div>
    </div>
    <div class="row">
    <div class="col-md-4">.col-md-4</div>
    <div class="col-md-4 col-md-offset-4">列向右移动四列的间距</div>
    </div>
    </div>
    

    如上面的示例代码,得到的效果如下或在最右侧结果窗口中可以看到(鼠标移到结果窗口,单击出现的全屏按钮):

    实现原理非常简单,就是利用十二分之一(1/12)的margin-left。然后有多少个offset,就有多少个margin-left。在bootstrap.css中第1205行~1241行所示:

      .col-md-offset-12 {
       margin-left: 100%;
    }
      .col-md-offset-11 {
        margin-left: 91.66666667%;
      }
      .col-md-offset-10 {
        margin-left: 83.33333333%;
      }
      .col-md-offset-9 {
        margin-left: 75%;
      }
      .col-md-offset-8 {
        margin-left: 66.66666667%;
      }
      .col-md-offset-7 {
        margin-left: 58.33333333%;
      }
      .col-md-offset-6 {
        margin-left: 50%;
      }
      .col-md-offset-5 {
        margin-left: 41.66666667%;
      }
      .col-md-offset-4 {
        margin-left: 33.33333333%;
      }
      .col-md-offset-3 {
        margin-left: 25%;
      }
      .col-md-offset-2 {
        margin-left: 16.66666667%;
      }
      .col-md-offset-1 {
        margin-left: 8.33333333%;
      }
      .col-md-offset-0 {
        margin-left: 0;
      }
    

    注意:

    不过有一个细节需要注意,使用”col-md-offset-*”对列进行向右偏移时,要保证列与偏移列的总数不超过12,不然会致列断行显示,如:

    <div class="row">
      <div class="col-md-3">.col-md-3</div>
      <div class="col-md-3 col-md-offset-3">col-md-offset-3</div>
      <div class="col-md-4">col-md-4</div>
    </div>
    

    上面代码中列和偏移列总数为3+3+3+4 = 13>12,所以发生了列断行。

    如上面的示例代码,得到的效果如下或在最右侧结果窗口中可以看到(鼠标移到结果窗口,单击出现的全屏按钮):

    <div class="container">
    <h4>列向右移动四列的间距</h4>
      <div class="row">
        <div class="col-md-4">.col-md-4</div>
        <div class="col-md-2 col-md-offset-4">列向右移动四列的间距</div>
        <div class="col-md-2">.col-md-3</div>
      </div>
      <div class="row">
        <div class="col-md-4">.col-md-4</div>
        <div class="col-md-4 col-md-offset-4">列向右移动四列的间距</div>
      </div>
    </div>
    <br />
    <h4>发生行断裂</h4>
    <div class="container">
      <div class="row">
        <div class="col-md-4">.col-md-4</div>
        <div class="col-md-2 col-md-offset-4">列向右移动四列的间距</div>
        <div class="col-md-2">.col-md-3</div>
      </div>
      <div class="row">
        <div class="col-md-4">.col-md-4</div>
        <div class="col-md-4 col-md-offset-4">列向右移动四列的间距</div>
      </div>
      <div class="row">
        <div class="col-md-3">.col-md-3</div>
        <div class="col-md-3 col-md-offset-3">col-md-offset-3</div>
        <div class="col-md-4">col-md-4</div>
      </div>
    </div>
     
     

    列排序

    列排序其实就是改变列的方向,就是改变左右浮动,并且设置浮动的距离。在Bootstrap框架的网格系统中是通过添加类名“col-md-push-*”和“col-md-pull-*” (其中星号代表移动的列组合数)。

    我们来看一个简单的示例:

    <div class="container">
      <div class="row">
        <div class="col-md-4">.col-md-4</div>
        <div class="col-md-8">.col-md-8</div>
      </div>
    </div>
    

    默认情况之下,上面的代码效果如下:

    “col-md-4”居左,“col-md-8”居右,如果要互换位置,需要将“col-md-4”向右移动8个列的距离,也就是8个offset ,也就是在“<div class=“col-md-4”>”添加类名“col-md-push-8”,调用其样式。

    也要将“col-md-8”向左移动4个列的距离,也就是4个offset,在“<div class=”col-md-8”>”上添加类名“col-md-pull-4”:

    Bootstrap仅通过设置left和right来实现定位效果。在boostrap.css文件的第1127行~第1204行可以看到具体的代码:

    .col-md-pull-12 {
        right: 100%;
      }
      .col-md-pull-11 {
        right: 91.66666667%;
      }
      .col-md-pull-10 {
        right: 83.33333333%;
      }
      .col-md-pull-9 {
        right: 75%;
      }
      .col-md-pull-8 {
        right: 66.66666667%;
      }
      .col-md-pull-7 {
        right: 58.33333333%;
      }
      .col-md-pull-6 {
        right: 50%;
      }
      .col-md-pull-5 {
        right: 41.66666667%;
      }
    
      .col-md-pull-4 {
        right: 33.33333333%;
      }
    
      .col-md-pull-3 {
        right: 25%;
      }
    
      .col-md-pull-2 {
        right: 16.66666667%;
      }
      .col-md-pull-1 {
        right: 8.33333333%;
      }
      .col-md-pull-0 {
        right: 0;
      }
    
      .col-md-push-12 {
        left: 100%;
      }
      .col-md-push-11 {
        left: 91.66666667%;
      }
      .col-md-push-10 {
        left: 83.33333333%;
      }
      .col-md-push-9 {
        left: 75%;
      }
      .col-md-push-8 {
        left: 66.66666667%;
      }
      .col-md-push-7 {
        left: 58.33333333%;
      }
      .col-md-push-6 {
        left: 50%;
      }
      .col-md-push-5 {
        left: 41.66666667%;
      }
      .col-md-push-4 {
        left: 33.33333333%;
      }
      .col-md-push-3 {
        left: 25%;
      }
      .col-md-push-2 {
        left: 16.66666667%;
      }
      .col-md-push-1 {
        left: 8.33333333%;
      }
      .col-md-push-0 {
        left: 0;
      }
    
     

    列的嵌套

    Bootstrap框架的网格系统还支持列的嵌套。你可以在一个列中添加一个或者多个行(row)容器,然后在这个行容器中插入列(像前面介绍的一样使用列)。但在列容器中的行容器(row),宽度为100%时,就是当前外部列的宽度。来看一个简单示例:

    <div class="container">
        <div class="row">
            <div class="col-md-8">
            我的里面嵌套了一个网格
                <div class="row">
                <div class="col-md-6">col-md-6</div>
                <div class="col-md-6">col-md-6</div>
              </div>
            </div>
        <div class="col-md-4">col-md-4</div>
        </div>
        <div class="row">
            <div class="col-md-4">.col-md-4</div>
        <div class="col-md-8">
        我的里面嵌套了一个网格
            <div class="row">
              <div class="col-md-4">col-md-4</div>
              <div class="col-md-4">col-md-4</div>
              <div class="col-md-4">col-md-4</div>
            </div>
        </div>
        </div>
    </div>
    

    效果如下:

    注意:嵌套的列总数也需要遵循不超过12列。不然会造成末位列换行显示。

     <div class="container">
      <div class="row">
        <div class="col-md-8">
          <div class="row">
            <div class="col-md-6">col-md-6</div>
            <div class="col-md-6">col-md-6</div>
          </div>
        </div>
        <div class="col-md-4">col-md-4</div>
      </div>
      <div class="row">
        <div class="col-md-4">.col-md-4</div>
        <div class="col-md-8">
          <div class="row">
            <div class="col-md-4">col-md-4</div>
            <div class="col-md-4">col-md-4</div>
            <div class="col-md-4">col-md-4</div>
          </div>
        </div>
      </div>
    </div>
     
     

    下拉菜单(基本用法)

    小伙伴们注意,在Bootstrap框架中的下拉菜单组件是一个独立的组件,根据不同的版本,它对应的文件:

      ☑  LESS版本:对应的源码文件为 dropdowns.less

      ☑  Sass版本:对应的源码文件为 _dropdowns.scss

      ☑  编译后的Bootstrap版本:查看bootstrap.css文件第3004行~第3130行

    在使用Bootstrap框架的下拉菜单时,必须调用Bootstrap框架提供的bootstrap.js文件。当然,如果你使用的是未编译版本,在js文件夹下你能找到一个名为“dropdown.js”的文件。你也可以调用这个js文件。不过在我们的教程中,我们统一调用压缩好的“bootstrap.min.js”文件:

    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    

    特别声明:因为Bootstrap的组件交互效果都是依赖于jQuery库写的插件,所以在使用bootstrap.min.js之前一定要先加载jquery.min.js才会生效果。

    我们先来看官网上一个简单的示例:

    <div class="dropdown">
    <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
    下拉菜单
    <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
       <li role="presentation"><a role="menuitem" tabindex="-1" href="#">下拉菜单项</a></li>
       …
       <li role="presentation" class="divider"></li>
       <li role="presentation"><a role="menuitem" tabindex="-1" href="#">下拉菜单项</a></li>
    </ul>
    </div>
    

    使用方法:

    在使用Bootstrap框架中的下拉菜单组件时,其结构运用的正确与否非常的重要,如果结构和类名未使用正确,直接影响组件是否能正常运用。我们来简单的看看:

    1、使用一个名为“dropdown”的容器包裹了整个下拉菜单元素,示例中为:

    <div class="dropdown"></div>

    2、使用了一个<button>按钮做为父菜单,并且定义类名“dropdown-toggle”和自定义“data-toggle”属性,且值必须和最外容器类名一致,此示例为:

    data-toggle="dropdown"

    3、下拉菜单项使用一个ul列表,并且定义一个类名为“dropdown-menu”,此示例为:

    <ul class="dropdown-menu">

    <div class="dropdown">
      <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
        下拉菜单
        <span class="caret"></span>
      </button>
      <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
        <li role="presentation"><a role="menuitem" tabindex="-1" href="#">下拉菜单项</a></li>
        <li role="presentation"><a role="menuitem" tabindex="-1" href="#">下拉菜单项</a></li>
        <li role="presentation"><a role="menuitem" tabindex="-1" href="#">下拉菜单项</a></li>
        <li role="presentation"><a role="menuitem" tabindex="-1" href="#">下拉菜单项</a></li>
      </ul>
    </div>
      <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
     

    下拉菜单(原理分析)

    Bootstrap框架中的下拉菜单组件,其下拉菜单项默认是隐藏的,如下所示:

    因为“dropdown-menu”默认样式设置了“display:none”,其详细源码请查看bootstrap.css文件第3010行~第3029行:

    .dropdown-menu {
      position: absolute;/*设置绝对定位,相对于父元素div.dropdown*/
      top: 100%;/*让下拉菜单项在父菜单项底部,如果父元素不设置相对定位,该元素相对于body元素*/
      left: 0;
      z-index: 1000;/*让下拉菜单项不被其他元素遮盖住*/
      display: none;/*默认隐藏下拉菜单项*/
      float: left;
      min- 160px;
      padding: 5px 0;
      margin: 2px 0 0;
      font-size: 14px;
      list-style: none;
      background-color: #fff;
      background-clip: padding-box;
      border: 1px solid #ccc;
      border: 1px solid rgba(0, 0, 0, .15);
      border-radius: 4px;
      -webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
      box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
    }
    

    当用户点击父菜单项时,下拉菜单将会被显示出来,如下所示:

    当用户再次点击时,下拉菜单将继续隐藏,如下所示:

    原理分析:

    现在我们来分析一下实现原理,非常简单,通过js技术手段,给父容器“div.dropdown”添加或移除类名“open”来控制下拉菜单显示或隐藏。也就是说,默认情况,“div.dropdown”没有类名“open”,当用户第一次点击时,“div.dropdown”会添加类名“open”;当用户再次点击时,“div.dropdown”容器中的类名“open”又会被移除。我们可以通过浏览器的firebug查看整个过程:

    默认情况:

    用户第一次点击:

    用户再次点击:

    在bootstrap.css文件第3076行~第3078行,我们可以很容易发现:

    .open > .dropdown-menu {
      display: block;
    }
    
     

    下拉菜单(下拉分隔线)

    在Bootstrap框架中的下拉菜单还提供了下拉分隔线,假设下拉菜单有两个组,那么组与组之间可以通过添加一个空的<li>,并且给这个<li>添加类名“divider”来实现添加下拉分隔线的功能。对应的样式代码:

    /*源码bootstrap.css文件第3034行~第3039行*/

    .dropdown-menu .divider {
      height: 1px;
      margin: 9px 0;
      overflow: hidden;
      background-color: #e5e5e5;
    }
    
    

    效果如下:

     

    下拉菜单(菜单标题)

    上一小节讲解通过添加“divider”可以将下拉菜单分组,为了让这个分组更明显,还可以给每个组添加一个头部(标题)。如下:

    <div class="dropdown">
    <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
    下拉菜单
    <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
    <li role="presentation" class="dropdown-header">第一部分菜单头部</li>
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">下拉菜单项</a></li>
    …
    <li role="presentation" class="divider"></li>
    <li role="presentation" class="dropdown-header">第二部分菜单头部</li>
    …
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">下拉菜单项</a></li>
    </ul>
    </div>
    

    对应的样式如下:

    /*查看bootstrap.css文件第3090行~第3096行*/

    .dropdown-header {
      display: block;
      padding: 3px 20px;
      font-size: 12px;
      line-height: 1.42857143;
      color: #999;
    }
    

    运行效果如下:

     
     

    下拉菜单(对齐方式)

    实现右对齐方法:

    Bootstrap框架中下拉菜单默认是左对齐,如果你想让下拉菜单相对于父容器右对齐时,可以在“dropdown-menu”上添加一个“pull-right”或者“dropdown-menu-right”类名,如下所示:

    <div class="dropdown">
      <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
      下拉菜单
      <span class="caret"></span>
      </button>
      <ul class="dropdown-menu pull-right" role="menu" aria-labelledby="dropdownMenu1">
       …
      </ul>
    </div>
    

    上面代码中的“pull-right”类可以用“dropdown-menu-right”代替,两个类的作用是一样的,可从下面的源代码中看出。

    实现原理:

    对应的样式如下:

    /*源码请查看bootstrap.css文件第3030行~第3033行和3082行~第3085行*/

    .dropdown-menu.pull-right {
      right: 0;
      left: auto;
    }
    .dropdown-menu-right {
      right: 0;
      left: auto;
    }
    

    同时一定要为.dropdown添加float:leftcss样式。

    .dropdown{
      float: left;
    }
    

    运行效果如下所示:

    下拉菜单与父容器左边对齐:

    与此同时,还有一个类名刚好与“dropdown-menu-right”相反的类名“dropdown-menu-left”,其效果就是让下拉菜单与父容器左边对齐,其实就是默认效果。

    /*请查看bootstrap.css文件第3086行~第3089行*/

    .dropdown-menu-left {
      right: auto;
      left: 0;
    }
    
     
     

    下拉菜单(菜单项状态)

    下拉菜单项的默认的状态(不用设置)有悬浮状态(:hover)和焦点状态(:focus):

    /*查看bootstrap.css文件第3049行~第3054行*/

    .dropdown-menu > li > a:hover,
    .dropdown-menu > li > a:focus {
      color: #262626;
      text-decoration: none;
      background-color: #f5f5f5;
    }
    

    下拉菜单项除了上面两种状态,还有当前状态(.active)和禁用状态(.disabled)。这两种状态使用方法只需要在对应的菜单项上添加对应的类名:

    <div class="dropdown">
      <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
      下拉菜单
      <span class="caret"></span>
      </button>
      <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
        <li role="presentation" class="active"><a role="menuitem" tabindex="-1" href="#">下拉菜单项</a></li>
        ….
        <li role="presentation" class="disabled"><a role="menuitem" tabindex="-1" href="#">下拉菜单项</a></li>
      </ul>
    </div>
    

    运行效果如下:

    对应的样式代码也非常简单:

    /*请查看bootstrap.css文件第3055行~第3075行*/

    .dropdown-menu > .active > a,
    .dropdown-menu > .active > a:hover,
    .dropdown-menu > .active > a:focus {
      color: #fff;
      text-decoration: none;
      background-color: #428bca;
      outline: 0;
    }
    .dropdown-menu > .disabled > a,
    .dropdown-menu > .disabled > a:hover,
    .dropdown-menu > .disabled > a:focus {
      color: #999;
    }
    .dropdown-menu > .disabled > a:hover,
    .dropdown-menu > .disabled > a:focus {
      text-decoration: none;
      cursor: not-allowed;
      background-color: transparent;
      background-image: none;
      filter: progid:DXImageTransform.Microsoft.gradient(enabled = false);
    }
    
     

    按钮(按钮组)

    单个按钮在Web页面中的运用有时候并不能满足我们的业务需求,常常会看到将多个按钮组合在一起使用,比如富文本编辑器里的一组小图标按钮等。那么在这一节中,我们主要向大家介绍Bootstrap框架为大家提供的按钮组组件。

    源码查询:

    按钮组也是一个独立的组件,所以可以找到对应的源码文件:

      ☑  LESS版本:对应的源文件为buttons.less

      ☑  Sass版本:对应的源文件为_buttons.scss

      ☑  CSS版本:对应bootstrap.css文件第3131行~第3291行

    使用方法:

    按钮组和下拉菜单组件一样,需要依赖于button.js插件才能正常运行。不过我们同样可以直接只调用bootstrap.js文件。因为这个文件已集成了button.js插件功能。

    对于结构方面,非常的简单。使用一个名为“btn-group”的容器,把多个按钮放到这个容器中。如下所示:

    <div class="btn-group">
      <button type="button" class="btn btn-default">
         <span class="glyphicon glyphicon-step-backward"></span>
      </button>
       …
      <button type="button" class="btn btn-default">
         <span class="glyphicon glyphicon-step-forward"></span>
      </button>
    </div>
    

    运行效果如下所示:

    除了可以使用<button>元素之外,还可以使用其他标签元素,比如<a>标签。唯一要保证的是:不管使用什么标签,“.btn-group”容器里的标签元素需要带有类名“.btn”。

    按钮组实现源码如下:

    /*查看bootstrap.css文件第3131行~第3161行*/

    .btn-group,
    .btn-group-vertical {
      position: relative;
      display: inline-block;
      vertical-align: middle;
    }
    .btn-group > .btn,
    .btn-group-vertical > .btn {
      position: relative;
      float: left;
    }
    .btn-group > .btn:hover,
    .btn-group-vertical > .btn:hover,
    .btn-group > .btn:focus,
    .btn-group-vertical > .btn:focus,
    .btn-group > .btn:active,
    .btn-group-vertical > .btn:active,
    .btn-group > .btn.active,
    .btn-group-vertical > .btn.active {
      z-index: 2;
    }
    .btn-group > .btn:focus,
    .btn-group-vertical > .btn:focus {
      outline: none;
    }
    .btn-group .btn + .btn,
    .btn-group .btn + .btn-group,
    .btn-group .btn-group + .btn,
    .btn-group .btn-group + .btn-group {
       margin-left: -1px;
    }
    

    从效果图上我们可以看出,按钮组四个角都是圆角(支持CSS3的浏览器),但有的小伙伴会问,我们平常制作网页时每个按钮都是带有圆角,而在按钮组中的按钮,除了第一个和最后一个具有边上的圆角之外,其他的按钮没有圆角,它是怎么实现的呢?其实实现方法非常简单:

      1、默认所有按钮都有圆角

      2、除第一个按钮和最后一个按钮(下拉按钮除外),其他的按钮都取消圆角效果

      3、第一个按钮只留左上角和左下角是圆角

      4、最后一个按钮只留右上角和右下角是圆角

    对应的源码如下:

    /*查看bootstrap.css文件第3174行~第3203行*/

    .btn-group > .btn:not(:first-child):not(:last-child):not(.dropdown-toggle) {
      border-radius: 0;
    }
    .btn-group > .btn:first-child {
      margin-left: 0;
    }
    .btn-group > .btn:first-child:not(:last-child):not(.dropdown-toggle) {
      border-top-right-radius: 0;
      border-bottom-right-radius: 0;
    }
    .btn-group > .btn:last-child:not(:first-child),
    .btn-group > .dropdown-toggle:not(:first-child) {
      border-top-left-radius: 0;
      border-bottom-left-radius: 0;
    }
    .btn-group > .btn-group {
      float: left;
    }
    .btn-group > .btn-group:not(:first-child):not(:last-child) > .btn {
      border-radius: 0;
    }
    .btn-group > .btn-group:first-child> .btn:last-child,
    .btn-group > .btn-group:first-child> .dropdown-toggle {
      border-top-right-radius: 0;
      border-bottom-right-radius: 0;
    }
    .btn-group > .btn-group:last-child> .btn:first-child {
      border-top-left-radius: 0;
      border-bottom-left-radius: 0;
    }
    
    <div class="btn-group">
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-step-backward"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-fast-backward"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-backward"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-play"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-pause"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-stop"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-forward "></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-fast-forward"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-step-forward"></span></button>
    </div>

    按钮(按钮工具栏)

    在富文本编辑器中,将按钮组分组排列在一起,比如说复制、剪切和粘贴一组;左对齐、中间对齐、右对齐和两端对齐一组,如下图所示:

    那么Bootstrap框架按钮工具栏也提供了这样的制作方法,你只需要将按钮组“btn-group”按组放在一个大的容器“btn-toolbar”中,如下所示:

    <div class="btn-toolbar">
      <div class="btn-group">
        …
      </div>
      <div class="btn-group">
        …
      </div>
      <div class="btn-group">
        …
      </div>
      <div class="btn-group">
        …
      </div>
    </div>
    

    实现原理主要是让容器的多个分组“btn-group”元素进行浮动,并且组与组之前保持5px的左外距。代码如下:

    /*源码请查阅bootstrap.css文件第3162行~第3173行*/

    .btn-toolbar {
      margin-left: -5px;
    }
    .btn-toolbar .btn-group,
    .btn-toolbar .input-group {
      float: left;
    }
    .btn-toolbar > .btn,
    .btn-toolbar > .btn-group,
    .btn-toolbar > .input-group {
      margin-left: 5px;
    }
    

    注意在”btn-toolbar”上清除浮动。

    /*源码请查阅bootstrap.css文件第5062行*/

    .btn-toolbar:before,
    .btn-toolbar:after{
     display: table;
    content: " ";
    }
    .btn-toolbar:after{
      clear: both;
    }
    

    运行效果如下:

    按钮组大小设置

    在介绍按钮一节中,我们知道按钮是通过btn-lg、btn-sm和btn-xs三个类名来调整padding、font-size、line-height和border-radius属性值来改变按钮大小。那么按钮组的大小,我们也可以通过类似的方法:

      ☑  .btn-group-lg:大按钮组

      ☑  .btn-group-sm:小按钮组

      ☑  .btn-group-xs:超小按钮组

    只需要在“.btn-group”类名上追加对应的类名,就可以得到不同大小的按钮组。如下所示:

    <div class="btn-toolbar">
      <div class="btn-group btn-group-lg">
        …
      </div>
      <div class="btn-group">
        …
      </div>
      <div class="btn-group btn-group-sm">
        …
      </div>
      <div class="btn-group btn-group-xs">
       …
      </div>
    </div>
    

    运行效果如下所示:

    实现上图效果样式代码如下:

    /*源码请查阅bootstrap.css文件第2320行~第2340行*/

    .btn-lg,
    .btn-group-lg> .btn{
      padding: 10px 16px;
      font-size: 18px;
      line-height: 1.33;
      border-radius: 6px;
    }
    .btn-sm,
    .btn-group-sm> .btn {
      padding: 5px 10px;
      font-size: 12px;
      line-height: 1.5;
      border-radius: 3px;
    }
    .btn-xs,
    .btn-group-xs> .btn{
      padding: 1px 5px;
      font-size: 12px;
      line-height: 1.5;
      border-radius: 3px;
    }
    
    <div class="btn-toolbar">
      <div class="btn-group">
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-left"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-center"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-right"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-justify"></span></button>
      </div>
      <div class="btn-group">
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-indent-left"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-indent-right"></span></button>
      </div>
      <div class="btn-group">
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-font"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-bold"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-italic"></span></button>
      </div>
      <div class="btn-group">
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-text-height"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-text-width"></span></button>
      </div>
    </div>
    <br />
    <br />
    <div class="btn-toolbar">
      <div class="btn-group btn-group-lg">
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-left"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-center"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-right"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-justify"></span></button>
      </div>
      <div class="btn-group">
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-indent-left"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-indent-right"></span></button>
      </div>
      <div class="btn-group btn-group-sm">
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-font"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-bold"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-italic"></span></button>
      </div>
      <div class="btn-group btn-group-xs">
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-text-height"></span></button>
        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-text-width"></span></button>
      </div>
    </div>
     

    按钮(嵌套分组)

    很多时候,我们常把下拉菜单和普通的按钮组排列在一起,实现类似于导航菜单的效果。如下所示:

    使用的时候,只需要把当初制作下拉菜单的“dropdown”的容器换成“btn-group”,并且和普通的按钮放在同一级。如下所示:

    <div class="btn-group">
    <button class="btnbtn-default" type="button">首页</button>
    <button class="btnbtn-default" type="button">产品展示</button>
    <button class="btnbtn-default" type="button">案例分析</button>
    <button class="btnbtn-default" type="button">联系我们</button>
    <div class="btn-group">
       <button class="btnbtn-default dropdown-toggle" data-toggle="dropdown" type="button">关于我们<span class="caret"></span></button>
       <ul class="dropdown-menu">
             <li><a href="##">公司简介</a></li>
             <li><a href="##">企业文化</a></li>
             <li><a href="##">组织结构</a></li>
             <li><a href="##">客服服务</a></li>
        </ul>
    </div>
    </div>
    

    实现的样式代码:

    /*查看bootstrap.css文件第3192行~第3223行*/

    .btn-group > .btn-group {
      float: left;
    }
    .btn-group > .btn-group:not(:first-child):not(:last-child) > .btn {
      border-radius: 0;
    }
    .btn-group > .btn-group:first-child> .btn:last-child,
    .btn-group > .btn-group:first-child> .dropdown-toggle {
      border-top-right-radius: 0;
      border-bottom-right-radius: 0;
    }
    .btn-group > .btn-group:last-child> .btn:first-child {
      border-top-left-radius: 0;
      border-bottom-left-radius: 0;
    }
    .btn-group .dropdown-toggle:active,
    .btn-group.open .dropdown-toggle {
      outline: 0;
    }
    .btn-group > .btn + .dropdown-toggle {
      padding-right: 8px;
      padding-left: 8px;
    }
    .btn-group > .btn-lg + .dropdown-toggle {
      padding-right: 12px;
      padding-left: 12px;
    }
    .btn-group.open .dropdown-toggle {
      -webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, .125);
      box-shadow: inset 0 3px 5px rgba(0, 0, 0, .125);
    }
    .btn-group.open .dropdown-toggle.btn-link {
      -webkit-box-shadow: none;
      box-shadow: none;
    }
    
    <div class="btn-group">
      <button class="btn btn-default" type="button">首页</button>
      <button class="btn btn-default" type="button">产品展示</button>
      <button class="btn btn-default" type="button">案例分析</button>
      <button class="btn btn-default" type="button">联系我们</button>
      <div class="btn-group">
          <button class="btn btn-default dropdown-toggle" data-toggle="dropdown" type="button">关于我们<span class="caret"></span></button>
        <ul class="dropdown-menu">
             <li><a href="##">公司简介</a></li>
             <li><a href="##">企业文化</a></li>
             <li><a href="##">组织结构</a></li>
             <li><a href="##">客服服务</a></li>
        </ul>
      </div>
    </div>

    按钮(垂直分组)

    前面看到的示例,按钮组都是水平显示的。但在实际运用当中,总会碰到垂直显示的效果。在Bootstrap框架中也提供了这样的风格。我们只需要把水平分组的“btn-group”类名换成“btn-group-vertical”即可。如下所示:

    <div class="btn-group-vertical">
    <button class="btnbtn-default" type="button">首页</button>
    <button class="btnbtn-default" type="button">产品展示</button>
    <button class="btnbtn-default" type="button">案例分析</button>
    <button class="btnbtn-default" type="button">联系我们</button>
    <div class="btn-group">
       <button class="btnbtn-default dropdown-toggle" data-toggle="dropdown" type="button">关于我们<span class="caret"></span></button>
       <ul class="dropdown-menu">
          <li><a href="##">公司简介</a></li>
          <li><a href="##">企业文化</a></li>
          <li><a href="##">组织结构</a></li>
          <li><a href="##">客服服务</a></li>
    </ul>
    </div>
    </div>
    

    运行的效果如下:

    实现垂直分组的样式代码:

    /*请查看bootstrap.css文件第3234行~第3276行*/

    .btn-group-vertical > .btn,
    .btn-group-vertical > .btn-group,
    .btn-group-vertical > .btn-group > .btn {
      display: block;
      float: none;
       100%;
      max- 100%;
    }
    .btn-group-vertical > .btn-group > .btn {
      float: none;
    }
    .btn-group-vertical > .btn + .btn,
    .btn-group-vertical > .btn + .btn-group,
    .btn-group-vertical > .btn-group + .btn,
    .btn-group-vertical > .btn-group + .btn-group {
      margin-top: -1px;
      margin-left: 0;
    }
    .btn-group-vertical > .btn:not(:first-child):not(:last-child) {
      border-radius: 0;
    }
    .btn-group-vertical > .btn:first-child:not(:last-child) {
      border-top-right-radius: 4px;
      border-bottom-right-radius: 0;
      border-bottom-left-radius: 0;
    }
    .btn-group-vertical > .btn:last-child:not(:first-child) {
      border-top-left-radius: 0;
      border-top-right-radius: 0;
      border-bottom-left-radius: 4px;
    }
    .btn-group-vertical > .btn-group:not(:first-child):not(:last-child) > .btn {
      border-radius: 0;
    }
    .btn-group-vertical > .btn-group:first-child:not(:last-child) > .btn:last-child,
    .btn-group-vertical > .btn-group:first-child:not(:last-child) > .dropdown-toggle {
      border-bottom-right-radius: 0;
      border-bottom-left-radius: 0;
    }
    .btn-group-vertical > .btn-group:last-child:not(:first-child) > .btn:first-child {
      border-top-left-radius: 0;
      border-top-right-radius: 0;
    }
    

    和水平分组按钮不一样的是:

      ☑  水平分组按钮第一个按钮左上角和左下角具有圆角以及最后一个按钮右上角和右下角具有圆角

      ☑  垂直分组按钮第一个按钮左上角和右上角具有圆角以及最后一个按钮左下角和右下角具有圆角

     
     

    按钮(等分按钮)

    等分按钮的效果在移动端上特别的实用。整个按钮组宽度是容器的100%,而按钮组里面的每个按钮平分整个容器宽度。例如,如果你按钮组里面有五个按钮,那么每个按钮是20%的宽度,如果有四个按钮,那么每个按钮是25%宽度,以此类推。

    等分按钮也常被称为是自适应分组按钮,其实现方法也非常的简单,只需要在按钮组“btn-group”上追加一个“btn-group-justified”类名,如下所示:

    <div class="btn-wrap">
    <div class="btn-group btn-group-justified">
      <a class="btnbtn-default" href="#">首页</a>
      <a class="btnbtn-default" href="#">产品展示</a>
      <a class="btnbtn-default" href="#">案例分析</a>
      <a class="btnbtn-default" href="#">联系我们</a>
    </div>
    </div>
    

    运行效果如下:

    实现原理非常简单,把“btn-group-justified”模拟成表格(display:table),而且把里面的按钮模拟成表格单元格(display:table-cell)。具体样式代码如下:

    /*源码请查看bootstrap.css文件第3277行~第3291行*/

    .btn-group-justified {
      display: table;
       100%;
      table-layout: fixed;
      border-collapse: separate;
    }
    .btn-group-justified > .btn,
    .btn-group-justified > .btn-group {
      display: table-cell;
      float: none;
       1%;
    }
    .btn-group-justified > .btn-group .btn {
       100%;
    }
    

    特别声明:在制作等分按钮组时,请尽量使用<a>标签元素来制作按钮,因为使用<button>标签元素时,使用display:table在部分浏览器下支持并不友好。

     <div class="btn-wrap">
        <div class="btn-group btn-group-justified">
        <a class="btn btn-default" href="#">首页</a>
        <a class="btn btn-default" href="#">产品展示</a>
        <a class="btn btn-default" href="#">案例分析</a>
        <a class="btn btn-default" href="#">联系我们</a>
      </div>
    </div>
     
     

    按钮下拉菜单

    按钮下拉菜单仅从外观上看和上一节介绍的下拉菜单效果基本上是一样的。不同的是在普通的下拉菜单的基础上封装了按钮(.btn)样式效果。简单点说就是点击一个按钮,会显示隐藏的下拉菜单。

    按钮下拉菜单其实就是普通的下拉菜单,只不过把“<a>”标签元素换成了“<button>”标签元素。唯一不同的是外部容器“div.dropdown”换成了“div.btn-group”。如下所示:

    <div class="btn-group">
          <button class="btn btn-default dropdown-toggle" data-toggle="dropdown" type="button">按钮下拉菜单<span class="caret"></span></button>
          <ul class="dropdown-menu">
              <li><a href="##">按钮下拉菜单项</a></li>
              <li><a href="##">按钮下拉菜单项</a></li>
              <li><a href="##">按钮下拉菜单项</a></li>
              <li><a href="##">按钮下拉菜单项</a></li>
          </ul>
    </div>
    

    实现样式代码如下:

    /*查看bootstrap.css文件第3204行~第3223行*/

    .btn-group .dropdown-toggle:active,
    .btn-group.open .dropdown-toggle {
      outline: 0;
    }
    .btn-group > .btn + .dropdown-toggle {
      padding-right: 8px;
      padding-left: 8px;
    }
    .btn-group > .btn-lg + .dropdown-toggle {
      padding-right: 12px;
      padding-left: 12px;
    }
    .btn-group.open .dropdown-toggle {
      -webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, .125);
              box-shadow: inset 0 3px 5px rgba(0, 0, 0, .125);
    }
    .btn-group.open .dropdown-toggle.btn-link {
      -webkit-box-shadow: none;
              box-shadow: none;
    }
    

    运行的效果如下:

     

    按钮的向下向上三角形

    按钮的向下三角形,我们是通过在<button>标签中添加一个“<span>”标签元素,并且命名为“caret”:

    <button class="btn btn-default dropdown-toggle" data-toggle="dropdown" type="button">按钮下拉菜单<span class="caret"></span></button>
    

    这个三角形完全是通过CSS代码来实现的:

    /*源码请查看bootstrap.css文件第2994行~第3003行*/

    .caret {
      display: inline-block;
       0;
      height: 0;
      margin-left: 2px;
      vertical-align: middle;
      border-top: 4px solid;
      border-right: 4px solid transparent;
      border-left: 4px solid transparent;
    }
    

    另外在按钮中的三角形“caret”做了一定的样式处理:

    /*源码查看bootstrap.css文件第3224行~第3233行*/

    .btn .caret {
      margin-left: 0;
    }
    .btn-lg .caret {
      border- 5px 5px 0;
      border-bottom- 0;
    }
    .dropup .btn-lg .caret {
      border- 0 5px 5px;
    }
    

    有的时候我们的下拉菜单会向上弹起(接下来一个小节会介绍),这个时候我们的三角方向需要朝上显示,实现方法:需要在“.btn-group”类上追加“dropup”类名(这也是做向上弹起下拉菜单要用的类名)。

    /*源码请查看bootstrap.css文件第3109行~第3114行*/

    .dropup .caret,
    .navbar-fixed-bottom .dropdown .caret {
      content: "";
      border-top: 0;
      border-bottom: 4px solid;
    }
    

    上面代码中可以看出,向上三角与向下三角的区别:其实就是改变了一个border-bottom的值。

    下面是向上弹起菜单的例子:

    <div class="btn-group dropup">
      <button class="btn btn-default dropdown-toggle" data-toggle="dropdown" type="button">按钮下拉菜单<span class="caret"></span></button>
      <ul class="dropdown-menu">
            <li><a href="##">按钮下拉菜单项</a></li>
            <li><a href="##">按钮下拉菜单项</a></li>
            <li><a href="##">按钮下拉菜单项</a></li>
            <li><a href="##">按钮下拉菜单项</a></li>
      </ul>
    </div>
    

    运行效果如下:

     

    向上弹起的下拉菜单

    有些菜单是需要向上弹出的,比如说你的菜单在页面最底部,而这个菜单正好有一个下拉菜单,为了让用户有更好的体验,不得不让下拉菜单向上弹出。在Bootstrap框架中专门为这种效果提代了一个类名“dropup”。使用方法正如前面所示,只需要在“btn-group”上添加这个类名(当然,如果是普通向上弹出下拉菜单,你只需要在“dropdown”类名基础上追加“dropup”类名即可)。

    <div class="btn-group dropup">
        <button class="btn btn-default dropdown-toggle" data-toggle="dropdown" type="button">按钮下拉菜单<span class="caret"></span></button>
        <ul class="dropdown-menu">
             <li><a href="##">按钮下拉菜单项</a></li>
             <li><a href="##">按钮下拉菜单项</a></li>
             <li><a href="##">按钮下拉菜单项</a></li>
             <li><a href="##">按钮下拉菜单项</a></li>
        </ul>
    </div>
    

    运行的效果如下:

    /*源码请查阅bootstrap.css文件第3115行~第3120行*/

    .dropup .dropdown-menu,
    .navbar-fixed-bottom .dropdown .dropdown-menu {
      top: auto;
      bottom: 100%;
      margin-bottom: 1px;
    }
    

    从上面的例子中可以看出,实现方法为:主要将“dropdown-menu”的top值变成了auto,而把bottom值换成了100%:

     

    导航(基础样式)

    导航对于一位前端人员来说并不陌生。可以说导航是一个网站重要的元素组件之一,可以便于用户查找网站所提供的各项功能服务。导航的制作方法也是千奇百怪,五花八门。在这一节中将向大家介绍如何使用Bootstrap框架制作各式各样的导航。

    在Bootstrap框架将导航独立出来成为一个导航组件,根据不同的版本,可以找到对应的源码:

       ☑ LESS版本:对应的源文件是navs.less

       ☑ Sass版本:对应的源文件是_navs.scss

       ☑ 编译后版本:对应源码是bootstrap.css文件第3450行~第3641行

    导航基础样式

    Bootstrap框架中制作导航条主要通过“.nav”样式。默认的“.nav”样式不提供默认的导航样式,必须附加另外一个样式才会有效,比如“nav-tabs”、“nav-pills”之类。比如右侧代码编辑器中就有一个tab导航条的例子,他的实现方法就是为ul标签加入.nav和nav-tabs两个类样式。

    /*源码请查阅bootstrap.css文件第3450行~第3493行*/

    .nav {
      padding-left: 0;
      margin-bottom: 0;
      list-style: none;
    }
    .nav> li {
      position: relative;
      display: block;
    }
    .nav> li > a {
      position: relative;
      display: block;
      padding: 10px 15px;
    }
    .nav> li >a:hover,
    .nav> li >a:focus {
      text-decoration: none;
      background-color: #eee;
    }
    .nav>li.disabled> a {
      color: #999;
    }
    .nav>li.disabled>a:hover,
    .nav>li.disabled>a:focus {
      color: #999;
      text-decoration: none;
      cursor: not-allowed;
      background-color: transparent;
    }
    .nav .open > a,
    .nav .open >a:hover,
    .nav .open >a:focus {
      background-color: #eee;
      border-color: #428bca;
    }
    .nav .nav-divider {
      height: 1px;
      margin: 9px 0;
      overflow: hidden;
      background-color: #e5e5e5;
    }
    .nav> li > a >img {
      max- none;
    }
    

    导航(标签形tab导航)

    标签形导航,也称为选项卡导航。特别是在很多内容分块显示的时,使用这种选项卡来分组十分适合。

    标签形导航是通过“nav-tabs”样式来实现。在制作标签形导航时需要在原导航“nav”上追加此类名,如:

    <ul class="nav nav-tabs">
         <li><a href="##">Home</a></li>
         <li><a href="##">CSS3</a></li>
         <li><a href="##">Sass</a></li>
         <li><a href="##">jQuery</a></li>
         <li><a href="##">Responsive</a></li>
    </ul>
    

    运行的效果如下所示:

    实现原理非常的简单,将菜单项(li)按块显示,并且让他们在同一水平上排列,然后定义非高亮菜单的样式和鼠标悬浮效果。代码如下:

    /*源码请查阅bootstrap.css文件第3494行~第3509行*/

    .nav-tabs {
    border-bottom: 1px solid #ddd;
    }
    .nav-tabs > li {
    float: left;
    margin-bottom: -1px;
    }
    .nav-tabs > li > a {
    margin-right: 2px;
    line-height: 1.42857143;
    border: 1px solid transparent;
    border-radius: 4px 4px 0 0;
    }
    .nav-tabs > li >a:hover {
    border-color: #eee #eee #ddd;
    }
    

    其实上例的效果和我们平时看到的选项卡效果并不一致。一般情况之下,选项卡教会有一个当前选中项。其实在Bootstrap框架也相应提供了。假设我们想让“Home”项为当前选中项,只需要在其标签上添加类名“class="active"”即可:

    <ul class="nav nav-tabs">
        <li class="active"><a href="##">Home</a></li>
        …
    </ul>
    

    运行效果如下:

    对应样式代码如下:

    /*源码请查阅bootstrap.css文件第3510行~第3518行*/

    .nav-tabs >li.active> a,
    .nav-tabs >li.active>a:hover,
    .nav-tabs >li.active>a:focus {
      color: #555;
      cursor: default;
      background-color: #fff;
      border: 1px solid #ddd;
      border-bottom-color: transparent;
    }
    

    除了当前项之外,有的选项卡还带有禁用状态,实现这样的效果,只需要在标签项上添加“class="disabled"”即可:

    <ul class="nav nav-tabs">
         <li class="active"><a href="##">Home</a></li>
         …
         <li class="disabled"><a href="##">Responsive</a></li>
    </ul>
    

    运行效果如下:

    实现这个效果的样式,在默认样式“.nav”中就带有:

    /*源码请查看bootstrap.css文件第3469行~第3478行*/

    .nav>li.disabled> a {
      color: #999;
    }
    .nav>li.disabled>a:hover,
    .nav>li.disabled>a:focus {
      color: #999;
      text-decoration: none;
      cursor: not-allowed;
      background-color: transparent;
    }
    
    注意:我们看到的选项卡效果,点击菜单项就可以切换内容,如果要实现这样的效果需要配合js插件,这部分将在后面的教程中会介绍。
     
    <ul class="nav nav-tabs">
        <li><a href="##">Home</a></li>
          <li><a href="##">CSS3</a></li>
          <li><a href="##">Sass</a></li>
          <li><a href="##">jQuery</a></li>
          <li><a href="##">Responsive</a></li>
    </ul>
    <br />
    <ul class="nav nav-tabs">
        <li class="active"><a href="##">Home</a></li>
          <li><a href="##">CSS3</a></li>
          <li><a href="##">Sass</a></li>
          <li><a href="##">jQuery</a></li>
          <li><a href="##">Responsive</a></li>
    </ul>
    <br />
     <ul class="nav nav-tabs">
        <li class="active"><a href="##">Home</a></li>
          <li><a href="##">CSS3</a></li>
          <li><a href="##">Sass</a></li>
          <li><a href="##">jQuery</a></li>
          <li class="disabled"><a href="##">Responsive</a></li>
     </ul>
     
     

    导航(胶囊形(pills)导航)

    胶囊形(pills)导航听起来有点别扭,因为其外形看起来有点像胶囊形状。但其更像我们平时看到的大众形导航。当前项高亮显示,并带有圆角效果。其实现方法和“nav-tabs”类似,同样的结构,只需要把类名“nav-tabs”换成“nav-pills”即可:

    <ul class="nav nav-pills">
          <li class="active"><a href="##">Home</a></li>
          <li><a href="##">CSS3</a></li>
          <li><a href="##">Sass</a></li>
          <li><a href="##">jQuery</a></li>
          <li class="disabled"><a href="##">Responsive</a></li>
    </ul>
    

    实现效果样式代码:

    /*源码请查阅bootstrap.css文件第3563行~第3577行*/

    .nav-pills > li {
      float: left;
    }
    .nav-pills > li > a {
      border-radius: 4px;
    }
    .nav-pills > li + li {
      margin-left: 2px;
    }
    .nav-pills >li.active> a,
    .nav-pills >li.active>a:hover,
    .nav-pills >li.active>a:focus {
    color: #fff;
      background-color: #428bca;
    }
    

    导航(垂直堆叠的导航)

    在实际运用当中,除了水平导航之外,还有垂直导航,就类似前面介绍的垂直排列按钮一样。制作垂直堆叠导航只需要在“nav-pills”的基础上添加一个“nav-stacked”类名即可:

    <ul class="nav nav-pills nav-stacked">
         <li class="active"><a href="##">Home</a></li>
         <li><a href="##">CSS3</a></li>
         <li><a href="##">Sass</a></li>
         <li><a href="##">jQuery</a></li>
         <li class="disabled"><a href="##">Responsive</a></li>
    </ul>
    

    运行效果如下:

    垂直堆叠导航与胶囊形导航相比,主要是让导航项不浮动,让其垂直排列,然后给相邻导航项留有一定的间距:

    /*源码请查阅bootstrap.css文件第3578行~第3584行*/

    .nav-stacked > li {
      float: none;
    }
    .nav-stacked > li + li {
      margin-top: 2px;
      margin-left: 0;
    }
    

    大家是否还记得,在下拉菜单一节中,下拉菜单组与组之间有一个分隔线。其实在垂直堆叠导航也具有这样的效果,只需要添加在导航项之间添加“<li class=”nav-divider”></li>”即可:

    <ul class="nav nav-pills nav-stacked">
        <li class="active"><a href="##">Home</a></li>
        <li><a href="##">CSS3</a></li>
        <li><a href="##">Sass</a></li>
        <li><a href="##">jQuery</a></li>
       <li class="nav-divider"></li>
        <li class="disabled"><a href="##">Responsive</a></li>
    </ul>
    

    运行效果如下:

    实现样式:

    /*源码请查阅bootstrap.css文件第3485行~3490行*/

    .nav .nav-divider {
    height: 1px;
    margin: 9px 0;
    overflow: hidden;
    background-color: #e5e5e5;
    }
    
    或许你会问,如果我在”nav-tabs”上添加“nav-stacked”是不是也能实现垂直的标签选项导航呢?答案是:在bootstrap V2.x版本可以,但在Bootstrap V3.x版本将这个效果取消了,可能作者觉得垂直选择项并不太常见,也不美观吧。
     
     
    <ul class="nav nav-pills nav-stacked">
         <li class="active"><a href="##">Home</a></li>
          <li><a href="##">CSS3</a></li>
          <li><a href="##">Sass</a></li>
          <li><a href="##">jQuery</a></li>
          <li class="disabled"><a href="##">Responsive</a></li>
     </ul>
     <br />
    <ul class="nav nav-pills nav-stacked">
        <li class="active"><a href="##">Home</a></li>
          <li><a href="##">CSS3</a></li>
          <li><a href="##">Sass</a></li>
          <li><a href="##">jQuery</a></li>
        <li class="nav-divider"></li>
          <li class="disabled"><a href="##">Responsive</a></li>
    </ul>
     

    自适应导航(使用)

    自适应导航指的是导航占据容器全部宽度,而且菜单项可以像表格的单元格一样自适应宽度。自适应导航和前面使用“btn-group-justified”制作的自适应按钮组是一样的。只不过在制作自适应导航时更换了另一个类名“nav-justified”。当然他需要和“nav-tabs”或者“nav-pills”配合在一起使用。如:

    <ul class="nav nav-tabs nav-justified">
         <li class="active"><a href="##">Home</a></li>
         <li><a href="##">CSS3</a></li>
         <li><a href="##">Sass</a></li>
         <li><a href="##">jQuery</a></li>
         <li><a href="##">Responsive</a></li>
    </ul>
    

    运行效果如下:(可单击全屏查看)

     
    <ul class="nav nav-tabs nav-justified">
      <li class="active"><a href="##">Home</a></li>
      <li><a href="##">CSS3</a></li>
      <li><a href="##">Sass</a></li>
      <li><a href="##">jQuery</a></li>
      <li><a href="##">Responsive</a></li>
    </ul>
    <br />
     
     

    自适应导航(实现原理)

    实现原理并不难,列表(<ul>)上设置宽度为“100%”,然后每个菜单项(<li>)设置了“display:table-cell”,让列表项以模拟表格单元格的形式显示:

    /*源码请查阅bootstrap.css文件第3585行~第3607行*/

    .nav-justified {
       100%;
    }
    .nav-justified > li {
      float: none;
    }
    .nav-justified > li > a {
      margin-bottom: 5px;
      text-align: center;
    }
    .nav-justified > .dropdown .dropdown-menu {
      top: auto;
      left: auto;
    }
    @media (min- 768px) {
      .nav-justified > li {
      display: table-cell;
       1%;
      }
      .nav-justified > li > a {
      margin-bottom: 0;
      }
    }
    

    这里有一个媒体查询条件:“@media (min-768px){…}”表示自适应导航仅在浏览器视窗宽度大于768px才能按上图风格显示。当你的浏览器视窗宽度小于768px的时候,将会按下图的风格展示:

    注:在最右侧代码窗口中设置全屏可以切换效果。

    从上图效果可以得知,“nav-tabs”和“nav-justified”配合在一起使用,也就是自适应选项卡导航,浏览器视窗宽度小于768px时,在样式上做了另外的处理。

    /*源码请查阅bootstrap.css文件第3519行~第3562行*/

    .nav-tabs.nav-justified {
      100%;
     border-bottom: 0;
    }
    .nav-tabs.nav-justified > li {
     float: none;
    }
    .nav-tabs.nav-justified > li > a {
     margin-bottom: 5px;
     text-align: center;
    }
    .nav-tabs.nav-justified > .dropdown .dropdown-menu {
     top: auto;
     left: auto;
    }
    @media (min- 768px) {
     .nav-tabs.nav-justified > li {
     display: table-cell;
      1%;
      }
    .nav-tabs.nav-justified > li > a {
     margin-bottom: 0;
      }
    }
    .nav-tabs.nav-justified > li > a {
     margin-right: 0;
     border-radius: 4px;
    }
    .nav-tabs.nav-justified > .active > a,
    .nav-tabs.nav-justified > .active >a:hover,
    .nav-tabs.nav-justified > .active >a:focus {
     border: 1px solid #ddd;
    }
    @media (min- 768px) {
     .nav-tabs.nav-justified > li > a {
     border-bottom: 1px solid #ddd;
     border-radius: 4px 4px 0 0;
      }
    .nav-tabs.nav-justified > .active > a,
    .nav-tabs.nav-justified > .active >a:hover,
    .nav-tabs.nav-justified > .active >a:focus {
     border-bottom-color: #fff;
      }
    }
    
     

    导航加下拉菜单(二级导航)

    前面介绍的都是使用Bootstrap框架制作一级导航,但很多时候,在Web页面中是离不开二级导航的效果。那么在Bootstrap框架中制作二级导航就更容易了。只需要将li当作父容器,使用类名“dropdown”,同时在li中嵌套另一个列表ul,使用前面介绍下拉菜单的方法就可以:

    <ul class="nav nav-pills">
         <li class="active"><a href="##">首页</a></li>
         <li class="dropdown">
            <a href="##" class="dropdown-toggle" data-toggle="dropdown">教程<span class="caret"></span></a>
            <ul class="dropdown-menu">
                <li><a href="##">CSS3</a></li>
                …
           </ul>
         </li>
         <li><a href="##">关于我们</a></li>
    </ul>
    

    运行效果如下:

    通过浏览器调试工具,不难发现,点击有二级导航的菜单项,会自动添加“open”类名,再次点击就会删除添加的“open”类名:

    简单点来说,就是依靠这个类名来控制二级导航显示与否,并且设置了背景色和边框:

    /*源码查看bootstrap.css文件第3479行~3484行*/

    .nav .open > a,
    .nav .open >a:hover,
    .nav .open >a:focus {
    background-color: #eee;
    border-color: #428bca;
    }
    

    大家回忆一下,在制作下拉菜单时,可以用分隔线,那么在二级导航中是否可以呢?我们一起来看看:

    不用再说太多,只需要添加“<li class=”nav-divider”></li>”这样的一个空标签就可以了。

    运行效果如下:

    /*源码请查看bootstrap.css文件第3485行~第3490行*/

    .nav .nav-divider {
      height: 1px;
      margin: 9px 0;
      overflow: hidden;
      background-color: #e5e5e5;
    }
    
     
    <ul class="nav nav-pills">
      <li class="active"><a href="##">首页</a></li>
      <li class="dropdown">
          <a href="##" class="dropdown-toggle" data-toggle="dropdown">教程<span class="caret"></span></a>
          <ul class="dropdown-menu">
              <li><a href="##">CSS3</a></li>
            <li><a href="##">Sass</a></li>
            <li><a href="##">jQuery</a></li>
            <li><a href="##">Responsive</a></li>
          </ul>
      </li>
     <li><a href="##">关于我们</a></li>
    </ul>
    <br />
     
     

    面包屑式导航

    面包屑(Breadcrumb)一般用于导航,主要是起的作用是告诉用户现在所处页面的位置(当前位置)。在Bootstrap框架中面包屑也是一个独立模块组件:

    • LESS版本:对应源文件breadcrumbs.less
    • Sass版本:对应源文件_breadcrumbs.scss
    • 编译出来的版本:源码对应bootstrap.css文件第4112行~第4129行

    使用方法:

    使用方式就很简单,为ol加入breadcrumb类:

    <ol class="breadcrumb">
      <li><a href="#">首页</a></li>
      <li><a href="#">我的书</a></li>
      <li class="active">《图解CSS3》</li>
    </ol>
    

    实现原理:

    看来不错吧!作者是使用li+li:before实现li与li之间的分隔符,所以这种方案在IE低版本就惨了(不支持)。

    /*源码查看bootstrap.css文件第4112行~第4129行*/

    .breadcrumb {
    padding: 8px 15px;
    margin-bottom: 20px;
    list-style: none;
    background-color: #f5f5f5;
    border-radius: 4px;
    }
    
    .breadcrumb> li {
    display: inline-block;
    }
    
    .breadcrumb> li + li:before {
    padding: 0 5px;
    color: #ccc;
    content: "/0a0";
    }
    
    .breadcrumb> .active {
    color: #999;
    }
    
     
    <!--代码-->
    <ol class="breadcrumb">
      <li><a href="#">首页</a></li>
      <li><a href="#">我的书</a></li>
      <li class="active">《图解CSS3》</li>
    </ol>
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    CF733F
    P4826
    洛谷P2687 & P1108
    CF42A
    洛谷P1858
    CF1428C
    洛谷P4981
    树形DP
    背包六讲(也不知道为啥就是六个 $QwQ$)
    2020
  • 原文地址:https://www.cnblogs.com/lycokcc/p/4250934.html
Copyright © 2011-2022 走看看