Bootstrap_文档_03_表单_按钮_图片_辅助类

Bootstrap_文档_03_表单_按钮_图片_辅助类_第1张图片


表单

基本实例

单独的表单控件会被自动赋予一些全局样式。

所有设置了 .form-control 类的 


多选和单选框

多选框(checkbox)用于选择列表中的一个或多个选项,

而单选框(radio)用于从多个选项中只选择一个。

Disabled checkboxes and radios are supported, 

but to provide a "not-allowed" cursor on hover of the parent 

you'll need to add the .disabled class to the parent .radio.radio-inline.checkbox, or .checkbox-inline.


默认外观(堆叠在一起)

效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第4张图片

代码如下:

 class="checkbox">
  
     type="checkbox" value="">
    Option one is this and thatbe sure to include why it's great
  
class="checkbox disabled"> type="checkbox" value="" disabled> Option two is disabled
class="radio"> type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked> Option one is this and thatbe sure to include why it's great
class="radio"> type="radio" name="optionsRadios" id="optionsRadios2" value="option2"> Option two can be something else and selecting it will deselect option one
class="radio disabled"> type="radio" name="optionsRadios" id="optionsRadios3" value="option3" disabled> Option three is disabled



内联单选和多选框

通过将 .checkbox-inline 或 .radio-inline 类 

应用到一系列的多选框(checkbox)或单选框(radio)控件上,

可以使这些控件排列在一行

效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第5张图片

代码如下:

 class="checkbox-inline">
   type="checkbox" id="inlineCheckbox1" value="option1"> 1

 class="checkbox-inline">
   type="checkbox" id="inlineCheckbox2" value="option2"> 2

 class="checkbox-inline">
   type="checkbox" id="inlineCheckbox3" value="option3"> 3



 class="radio-inline">
   type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1"> 1

 class="radio-inline">
   type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2"> 2

 class="radio-inline">
   type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3"> 3



不带label文本的Checkbox 和 radio

如果需要  内没有文字,输入框(input)正是你所期望的。

 目前只适用于非内联的 checkbox 和 radio。

请记住,仍然需要为使用辅助技术的用户提供某种形式的 label(例如,使用 aria-label)。

效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第6张图片

代码如下:

 class="checkbox">
  
     type="checkbox" id="blankCheckbox" value="option1" aria-label="...">
  
class="radio"> type="radio" name="blankRadio" id="blankRadio1" value="option1" aria-label="...">


下拉列表(select)

注意,很多原生选择菜单 - 

即在 Safari 和 Chrome 中 - 的圆角是无法通过修改 border-radius 属性来改变的。

效果如下:


代码如下: 

 class="form-control">
  1
  2
  3
  4
  5






对于标记了 multiple 属性的 


静态控件

如果需要在表单中将一行纯文本和 label 元素放置于同一行,

为 

 元素添加 .form-control-static 类即可。

1. 效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第8张图片

1. 代码如下:

 class="form-horizontal">
   class="form-group">
     class="col-sm-2 control-label">Email
     class="col-sm-10">
       class="form-control-static">[email protected]

class="form-group"> for="inputPassword" class="col-sm-2 control-label">Password class="col-sm-10"> type="password" class="form-control" id="inputPassword" placeholder="Password">



2. 效果如下:


2. 代码如下:

注意: sr-only即screen reader only


 class="form-inline">
   class="form-group">
     class="sr-only">Email
     class="form-control-static">[email protected]


   class="form-group">
     for="inputPassword2" class="sr-only">Password
     type="password" class="form-control" id="inputPassword2" placeholder="Password">
  

   type="submit" class="btn btn-default">Confirm identity

 


焦点状态

我们将某些表单控件的默认 outline 样式移除,然后对 :focus 状态赋予 box-shadow 属性。

效果如下:


演示:focus 状态

在本文档中,我们为上面实例中的输入框赋予了自定义的样式,

用于演示 .form-control 元素的 :focus状态。


禁用状态

为输入框设置 disabled 属性可以禁止其与用户有任何交互(焦点、输入等)。

被禁用的输入框颜色更浅,并且还添加了 not-allowed 鼠标状态。

效果如下:


 class="form-control" id="disabledInput" type="text" placeholder="Disabled input here..." disabled />


被禁用的 fieldset

 设置 disabled 属性,可以禁用 
 中包含的所有控件。


     class="checkbox">
      
         type="checkbox"> Can't check this
      
    

     type="submit" class="btn btn-primary">Submit
  




只读状态

为输入框设置 readonly 属性可以禁止用户修改输入框中的内容。

处于只读状态的输入框颜色更浅(就像被禁用的输入框一样),但是仍然保留标准的鼠标状态。

效果如下:


代码如下:

 class="form-control" type="text" placeholder="Readonly input here…" readonly />


Help text

Block level help text for form controls.

Associating help text with form controls

Help text should be explicitly associated with the form control 

it relates to using the aria-describedbyattribute. 

This will ensure that assistive technologies – such as screen readers – 

will announce this help text when the user focuses or enters the control.


效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第10张图片

代码如下:

 class="sr-only" for="inputHelpBlock">Input with help text
 type="text" id="inputHelpBlock" class="form-control" aria-describedby="helpBlock">
...
 id="helpBlock" class="help-block">
    A block of help text that breaks onto a new line and may extend beyond one line.



校验状态

Bootstrap 对表单控件的校验状态,如 error、warning 和 success 状态,都定义了样式。

使用时,添加 .has-warning.has-error 或 .has-success 类到这些控件的父元素即可。

任何包含在此元素之内的 .control-label.form-control 和 .help-block 元素都将接受这些校验状态的样式。

将验证状态传达给辅助设备和盲人用户

使用这些校验样式只是为表单控件提供一个可视的、基于色彩的提示,

但是并不能将这种提示信息传达给使用辅助设备的用户 - 例如屏幕阅读器 - 或者色盲用户。

为了确保所有用户都能获取正确信息,Bootstrap 还提供了另一种提示方式。

例如,你可以在表单控件的  标签上以文本的形式显示提示信息(就像下面代码中所展示的);

包含一个 Glyphicon 字体图标(还有赋予 .sr-only 类的文本信息 - 参考Glyphicon 字体图标实例);

或者提供一个额外的 辅助信息 块。

另外,对于使用辅助设备的用户,无效的表单控件还可以赋予一个 aria-invalid="true" 属性。

 


效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第11张图片

代码如下:

 class="form-group has-success">
   class="control-label" for="inputSuccess1">Input with success
   type="text" class="form-control" id="inputSuccess1" aria-describedby="helpBlock2">
   id="helpBlock2" class="help-block">
    A block of help text that breaks onto a new line and may extend beyond one line.
  

 class="form-group has-warning">
   class="control-label" for="inputWarning1">Input with warning
   type="text" class="form-control" id="inputWarning1">

 class="form-group has-error">
   class="control-label" for="inputError1">Input with error
   type="text" class="form-control" id="inputError1">

 class="has-success">
   class="checkbox">
    
       type="checkbox" id="checkboxSuccess" value="option1">
      Checkbox with success
    
  

 class="has-warning">
   class="checkbox">
    
       type="checkbox" id="checkboxWarning" value="option1">
      Checkbox with warning
    
  

 class="has-error">
   class="checkbox">
    
       type="checkbox" id="checkboxError" value="option1">
      Checkbox with error
    
  



添加额外的图标

你还可以针对校验状态为输入框添加额外的图标。

只需设置相应的 .has-feedback 类并添加正确的图标即可。

反馈图标(feedback icon)只能使用在文本输入框  元素上。

图标、label 和输入控件组

对于不带有 label 标签的输入框以及右侧带有附加组件的输入框组,需要手动为其图标定位。

为了让所有用户都能访问你的网站,我们强烈建议为所有输入框添加 label 标签。

如果你不希望将 label 标签展示出来,可以通过添加 .sr-only 类来实现。

如果的确不能添加 label 标签,请调整图标的 top 值。

对于输入框组,请根据你的实际情况调整 right 值。

向辅助技术设备传递图标的含义

为了确保辅助技术- 如屏幕阅读器 - 正确传达一个图标的含义,

额外的隐藏的文本应包含在 .sr-only 类中,并明确关联使用了 aria-describedby 的表单控件。

或者,以某些其他形式(例如,文本输入字段有一个特定的警告信息)传达含义,

例如改变与表单控件实际相关联的  的文本。

虽然下面的例子已经提到各自表单控件本身的  文本的验证状态,

上述技术(使用 .sr-only 文本 和 aria-describedby) )已经包括了需要说明的目的。



1. 效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第12张图片

1. 代码如下:

 class="form-group has-success has-feedback">
   class="control-label" for="inputSuccess2">Input with success
   type="text" class="form-control" id="inputSuccess2" aria-describedby="inputSuccess2Status">
   class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true">
   id="inputSuccess2Status" class="sr-only">(success)

 class="form-group has-warning has-feedback">
   class="control-label" for="inputWarning2">Input with warning
   type="text" class="form-control" id="inputWarning2" aria-describedby="inputWarning2Status">
   class="glyphicon glyphicon-warning-sign form-control-feedback" aria-hidden="true">
   id="inputWarning2Status" class="sr-only">(warning)

 class="form-group has-error has-feedback">
   class="control-label" for="inputError2">Input with error
   type="text" class="form-control" id="inputError2" aria-describedby="inputError2Status">
   class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true">
   id="inputError2Status" class="sr-only">(error)

 class="form-group has-success has-feedback">
   class="control-label" for="inputGroupSuccess1">Input group with success

   class="input-group">
     class="input-group-addon">@
     type="text" class="form-control" id="inputGroupSuccess1" 
        aria-describedby="inputGroupSuccess1Status" />
  

   class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true">
   id="inputGroupSuccess1Status" class="sr-only">(success)



为水平排列的表单和内联表单设置可选的图标

2. 效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第13张图片

2. 代码如下:

 class="form-horizontal">
   class="form-group has-success has-feedback">
     class="control-label col-sm-3" for="inputSuccess3">Input with success

     class="col-sm-9">
       type="text" class="form-control"         id="inputSuccess3" aria-describedby="inputSuccess3Status">
       class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true">
       id="inputSuccess3Status" class="sr-only">(success)
    

  


   class="form-group has-success has-feedback">
     class="control-label col-sm-3" for="inputGroupSuccess2">        Input group with success    

     class="col-sm-9">

       class="input-group">
         class="input-group-addon">@
         type="text" class="form-control" id="inputGroupSuccess2" 
            aria-describedby="inputGroupSuccess2Status" />
      

       class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true">
       id="inputGroupSuccess2Status" class="sr-only">(success)
    
  







3. 效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第14张图片

3. 代码如下:

 class="form-inline">
   class="form-group has-success has-feedback">
     class="control-label" for="inputSuccess4">Input with success
     type="text" class="form-control"         id="inputSuccess4" aria-describedby="inputSuccess4Status">
     class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true">    
     id="inputSuccess4Status" class="sr-only">(success)
  

 class="form-inline">
   class="form-group has-success has-feedback">
     class="control-label" for="inputGroupSuccess3">Input group with success

     class="input-group">
       class="input-group-addon">@
       type="text" class="form-control" id="inputGroupSuccess3" 
            aria-describedby="inputGroupSuccess3Status" />
    

     class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true">
     id="inputGroupSuccess3Status" class="sr-only">(success)
  





效果可选的图标与设置 .sr-only 类的 label

如果你使用 .sr-only 类来隐藏表单控件的  (而不是使用其它标签选项,如 aria-label 属性), 

一旦它被添加,Bootstrap 会自动调整图标的位置。

效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第15张图片

代码如下:

 class="form-group has-success has-feedback">
   class="control-label sr-only" for="inputSuccess5">Hidden label
   type="text" class="form-control" id="inputSuccess5"     aria-describedby="inputSuccess5Status">

   class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true">  
   id="inputSuccess5Status" class="sr-only">(success)



 class="form-group has-success has-feedback">
   class="control-label sr-only" for="inputGroupSuccess4">Input group with success

   class="input-group">
     class="input-group-addon">@
     type="text" class="form-control" id="inputGroupSuccess4" 
        aria-describedby="inputGroupSuccess4Status" />
  

   class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true">
   id="inputGroupSuccess4Status" class="sr-only">(success)


 

控件尺寸

通过 .input-lg 类似的类可以为控件设置高度,通过 .col-lg-* 类似的类可以为控件设置宽度。

高度尺寸

创建大一些或小一些的表单控件以匹配按钮尺寸.

效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第16张图片

代码如下:

 class="form-control input-lg" type="text" placeholder=".input-lg">
 class="form-control" type="text" placeholder="Default input">
 class="form-control input-sm" type="text" placeholder=".input-sm">

 class="form-control input-lg">...
 class="form-control">...
 class="form-control input-sm">...


水平排列的表单组的尺寸

通过添加 .form-group-lg 或 .form-group-sm 类,

为 .form-horizontal 包裹的 label 元素和表单控件快速设置尺寸。

效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第17张图片

代码如下:

 class="form-horizontal">
   class="form-group form-group-lg">
     class="col-sm-2 control-label" for="formGroupInputLarge">Large label
     class="col-sm-10">
       class="form-control" type="text"           id="formGroupInputLarge" placeholder="Large input">
    
  

   class="form-group form-group-sm">
     class="col-sm-2 control-label" for="formGroupInputSmall">Small label
     class="col-sm-10">
       class="form-control" type="text" id="formGroupInputSmall" placeholder="Small input">
    
  




调整列(column)尺寸

用栅格系统中的列(column)包裹输入框或其任何父元素,都可很容易的为其设置宽度。

效果如下:


代码如下:

 class="row">
   class="col-xs-2">
     type="text" class="form-control" placeholder=".col-xs-2">
  
   class="col-xs-3">
     type="text" class="form-control" placeholder=".col-xs-3">
  
   class="col-xs-4">
     type="text" class="form-control" placeholder=".col-xs-4">
  




按钮

可作为按钮使用的标签或元素

为  class="btn btn-default" type="button" value="Input"> class="btn btn-default" type="submit" value="Submit">

针对组件的注意事项

虽然按钮类可以应用到  和 

跨浏览器展现

我们总结的最佳实践是:

强烈建议尽可能使用 



预定义样式

使用下面列出的类可以快速创建一个带有预定义样式的按钮。

效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第19张图片

代码如下:


 type="button" class="btn btn-default">(默认样式)Default


 type="button" class="btn btn-primary">(首选项)Primary


 type="button" class="btn btn-success">(成功)Success


 type="button" class="btn btn-info">(一般信息)Info


 type="button" class="btn btn-warning">(警告)Warning


 type="button" class="btn btn-danger">(危险)Danger


 type="button" class="btn btn-link">(链接)Link

Conveying meaning to assistive technologies

为按钮添加不同的颜色只是一种视觉上的信息表达方式,

但是,对于使用辅助技术 -- 例如屏幕阅读器 -- 的用户来说,颜色是不可见的。

建议,确保通过颜色表达的信息或者通过内容自身表达出来(按钮上的文字),

或者通过其他方式 -- 例如通过 .sr-only 类隐藏的额外文本 -- 表达出来。


尺寸

需要让按钮具有不同尺寸吗?使用 .btn-lg.btn-sm 或 .btn-xs 就可以获得不同尺寸的按钮。

效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第20张图片

代码如下:

type="button" class="btn btn-primary btn-lg">(大按钮)Large button type="button" class="btn btn-default btn-lg">(大按钮)Large button

type="button" class="btn btn-primary">(默认尺寸)Default button type="button" class="btn btn-default">(默认尺寸)Default button

type="button" class="btn btn-primary btn-sm">(小按钮)Small button type="button" class="btn btn-default btn-sm">(小按钮)Small button

type="button" class="btn btn-primary btn-xs">(超小尺寸)Extra small button type="button" class="btn btn-default btn-xs">(超小尺寸)Extra small button


通过给按钮添加 .btn-block 类可以将其拉伸至父元素100%的宽度,

而且按钮也变为了块级(block)元素。

效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第21张图片

代码如下:

 type="button" class="btn btn-primary btn-lg btn-block">(块级元素)Block level button
 type="button" class="btn btn-default btn-lg btn-block">(块级元素)Block level button



激活状态

当按钮处于激活状态时,其表现为被按压下去(底色更深、边框夜色更深、向内投射阴影)。

对于  type="button" class="btn btn-default btn-lg active">Button


链接()元素

可以为基于  元素创建的按钮添加 .active 类。

效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第23张图片

代码如下:

 href="#" class="btn btn-primary btn-lg active" role="button">Primary link
 href="#" class="btn btn-default btn-lg active" role="button">Link


禁用状态

通过为按钮的背景设置 opacity 属性就可以呈现出无法点击的效果。


button 元素

为  type="button" class="btn btn-default btn-lg" disabled="disabled">    Button

跨浏览器兼容性

如果为 


链接()元素

为基于  元素创建的按钮添加 .disabled 类。

效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第25张图片

代码如下:

 href="#" class="btn btn-primary btn-lg disabled" role="button">Primary link
 href="#" class="btn btn-default btn-lg disabled" role="button">Link

我们把 .disabled 作为工具类使用,就像 .active 类一样,因此不需要增加前缀。

图片

响应式图片

在 Bootstrap 版本 3 中,通过为图片添加 .img-responsive 类可以让图片支持响应式布局。

其实质是为图片设置了 max-width: 100%;、 height: auto; 和 display: block; 属性,

从而让图片在其父元素中更好的缩放。


如果需要让使用了 .img-responsive 类的图片水平居中,

请使用 .center-block 类,不要用 .text-center

 请参考助手类章节 了解更多关于 .center-block 的用法。

SVG 图像和 IE 8-10

在 Internet Explorer 8-10 中,设置为 .img-responsive 的 SVG 图像显示出的尺寸不匀称。

为了解决这个问题,在出问题的地方添加 width: 100% \9; 即可。

Bootstrap 并没有自动为所有图像元素设置这一属性,因为这会导致其他图像格式出现错乱。

 src="..." class="img-responsive" alt="Responsive image">


图片形状

通过为  元素添加以下相应的类,可以让图片呈现不同的形状。

效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第26张图片

跨浏览器兼容性

请时刻牢记:Internet Explorer 8 不支持 CSS3 中的圆角属性。

 src="..." alt="..." class="img-rounded">
 src="..." alt="..." class="img-circle">
 src="..." alt="..." class="img-thumbnail">

辅助类

情境文本颜色

通过颜色来展示意图,Bootstrap 提供了一组工具类。

这些类可以应用于链接,并且在鼠标经过时颜色可以还可以加深,就像默认的链接一样。

Fusce dapibus, tellus ac cursus commodo, tortor mauris nibh.

Nullam id dolor id nibh ultricies vehicula ut id elit.

Duis mollis, est non commodo luctus, nisi erat porttitor ligula.

Maecenas sed diam eget risus varius blandit sit amet non magna.

Etiam porta sem malesuada magna mollis euismod.

Donec ullamcorper nulla non metus auctor fringilla.

 class="text-muted">...

class="text-primary">...

class="text-success">...

class="text-info">...

class="text-warning">...

class="text-danger">...

处理差异

Sometimes emphasis classes cannot be applied due to the specificity of another selector. 

In most cases, a sufficient workaround is to wrap your text in a  with the class.

Conveying meaning to assistive technologies

Using color to add meaning only provides a visual indication, 

which will not be conveyed to users of assistive technologies – such as screen readers. 

Ensure that information denoted by the color is either obvious from the content itself

 (the contextual colors are only used to reinforce meaning that is already present in the text/markup), 

or is included through alternative means, such as additional text hidden with the .sr-only class.


情境背景色

和情境文本颜色类一样,使用任意情境背景色类就可以设置元素的背景。

链接组件在鼠标经过时颜色会加深,就像上面所讲的情境文本颜色类一样

效果如下:

Bootstrap_文档_03_表单_按钮_图片_辅助类_第27张图片

代码如下:

 class="bg-primary">...

class="bg-success">...

class="bg-info">...

class="bg-warning">...

class="bg-danger">...

处理差异

Sometimes contextual background classes cannot be applied due to the specificity of another selector. 

In some cases, a sufficient workaround is to wrap your element's content in a 

 with the class.

Conveying meaning to assistive technologies

As with contextual colors, ensure that any meaning conveyed through color is also conveyed in a format 

that is not purely presentational.

关闭按钮

通过使用一个象征关闭的图标,可以让模态框和警告框消失。

效果如下:


 type="button" class="close" aria-label="Close">     aria-hidden="true">×


三角符号

通过使用三角符号可以指示某个元素具有下拉菜单的功能。

注意,向上弹出式菜单中的三角符号是反方向的。

 class="caret">


快速浮动

通过添加一个类,可以将任意元素向左或向右浮动。

!important 被用来明确 CSS 样式的优先级。

这些类还可以作为 mixin(参见 less 文档) 使用。

 class="pull-left">...
class="pull-right">...
// Classes
.pull-left {
  float: left !important;
}
.pull-right {
  float: right !important;
}

// Usage as mixins
.element {
  .pull-left();
}
.another-element {
  .pull-right();
}

不能用于导航条组件中

排列导航条中的组件时可以使用这些工具类:.navbar-left 或 .navbar-right 。 

参见导航条文档以获取更多信息。


让内容块居中

为任意元素设置 display: block 属性并通过 margin 属性让其中的内容居中。

下面列出的类还可以作为 mixin 使用。

 class="center-block">...
// Class
.center-block {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

// Usage as a mixin
.element {
  .center-block();
}


清除浮动

通过为父元素添加 .clearfix 类可以很容易地清除浮动(float)。

这里所使用的是 Nicolas Gallagher 创造的 micro clearfix 方式。

此类还可以作为 mixin 使用。


 class="clearfix">...
// Mixin itself
.clearfix() {
  &:before,
  &:after {
    content: " ";
    display: table;
  }
  &:after {
    clear: both;
  }
}

// Usage as a mixin
.element {
  .clearfix();
}


显示或隐藏内容

.show 和 .hidden 类可以强制任意元素显示或隐藏(对于屏幕阅读器也能起效)。

这些类通过 !important 来避免 CSS 样式优先级问题,就像 quick floats 一样的做法。

注意,这些类只对块级元素起作用,另外,还可以作为 mixin 使用。


.hide 类仍然可用,但是它不能对屏幕阅读器起作用,

并且从 v3.0.1 版本开始就不建议使用了。请使用 .hidden或 .sr-only 。


另外,.invisible 类可以被用来仅仅影响元素的可见性,

也就是说,元素的 display 属性不被改变,并且这个元素仍然能够影响文档流的排布。


 class="show">...
 class="hidden">...
// Classes
.show {
  display: block !important;
}
.hidden {
  display: none !important;
}
.invisible {
  visibility: hidden;
}

// Usage as mixins
.element {
  .show();
}
.another-element {
  .hidden();
}


屏幕阅读器和键盘导航

.sr-only 类可以对屏幕阅读器以外的设备隐藏内容。

.sr-only 和 .sr-only-focusable 联合使用的话可以在元素有焦点的时候再次显示出来(例如,使用键盘导航的用户)。

对于遵循 可访问性的最佳实践 很有必要。

这个类也可以作为 mixin 使用。

 class="sr-only sr-only-focusable" href="#content">Skip to main content
// Usage as a mixin
.skip-navigation {
  .sr-only();
  .sr-only-focusable();
}


图片替换

使用 .text-hide 类或对应的 mixin 可以用来将元素的文本内容替换为一张背景图。

 class="text-hide">Custom heading
// Usage as a mixin
.heading {
  .text-hide();
}

响应式工具

为了加快对移动设备友好的页面开发工作,

利用媒体查询功能并使用这些工具类可以方便的针对不同设备展示或隐藏页面内容。

另外还包含了针对打印机显示或隐藏内容的工具类。

有针对性的使用这类工具类,从而避免为同一个网站创建完全不同的版本。

相反,通过使用这些工具类可以在不同设备上提供不同的展现形式。


可用的类

通过单独或联合使用以下列出的类,可以针对不同屏幕尺寸隐藏或显示页面内容。

  超小屏幕手机 (<768px) 小屏幕平板 (≥768px) 中等屏幕桌面 (≥992px) 大屏幕桌面 (≥1200px)
.visible-xs-* 可见
.visible-sm-* 可见
.visible-md-* 可见
.visible-lg-* 可见
.hidden-xs 可见 可见 可见
.hidden-sm 可见 可见 可见
.hidden-md 可见 可见 可见
.hidden-lg 可见 可见 可见


从 v3.2.0 版本起,形如 .visible-*-* 的类针对每种屏幕大小都有了三种变体,

每个针对 CSS 中不同的 display属性,列表如下:

类组 CSS display
.visible-*-block display: block;
.visible-*-inline display: inline;
.visible-*-inline-block display: inline-block;

因此,以超小屏幕(xs)为例,可用的 .visible-*-* 类是:

.visible-xs-block.visible-xs-inline 和 .visible-xs-inline-block


.visible-xs.visible-sm.visible-md 和 .visible-lg 类也同时存在。

但是从 v3.2.0 版本开始不再建议使用

除了 

 相关的元素的特殊情况外,它们与 .visible-*-block 大体相同。


打印类

和常规的响应式类一样,使用下面的类可以针对打印机隐藏或显示某些内容。

class 浏览器 打印机
.visible-print-block
.visible-print-inline
.visible-print-inline-block
可见
.hidden-print 可见

.visible-print 类也是存在的,但是从 v3.2.0 版本开始不建议使用

它与 .visible-print-block 类大致相同,除了 

 相关元素的特殊情况外。


测试用例

调整你的浏览器大小,或者用其他设备打开页面,都可以测试这些响应式工具类.

Bootstrap_文档_03_表单_按钮_图片_辅助类_第28张图片


使用 Less

.

Bootstrap 的 CSS 文件是通过 Less 源码编译而来的。

Less 是一门预处理语言,支持变量、mixin、函数等额外功能。

对于希望使用 Less 源码而非编译而来的 CSS 文件的用户,

Bootstrap 框架中包含的大量变量、mixin 将非常有价值。

针对栅格系统的变量和 mixin 包含在栅格系统章节。


编译 Bootstrap

可以通过两种方式使用 Bootstrap :使用编译后的 CSS 文件或者使用 Less 源码文件。

若要编译 Less 文件,请参考“起步”章节的内容以了解如何设置开发环境并运行必须的编译指令。


变量

整个 Bootstrap 项目中使用了大量的变量,这些变量被用来代表颜色、空白(内部、边距)、字体等。

详细内容请参考定制工具


颜色

Bootstrap 使用了两种颜色模式:灰度颜色和语义颜色。

灰度颜色用于快速获取常用的黑色色调;语义颜色包含了各种赋予语义的颜色值

Bootstrap_文档_03_表单_按钮_图片_辅助类_第29张图片


你在项目中可以使用这些预定义的颜色变量,或者重新为其赋予别名,使其更有语义。

// Use as-is
.masthead {
  background-color: @brand-primary;
}

// Reassigned variables in Less
@alert-message-background: @brand-info;
.alert {
  background-color: @alert-message-background;
}


Scaffolding

某几个变量是改变网站外观的关键要素。

// Scaffolding
@body-bg:    #fff;
@text-color: @black-50;

链接

仅仅通过改变一个变量,可以很容易地为链接赋予正确的颜色。

// Variables
@link-color:       @brand-primary;
@link-hover-color: darken(@link-color, 15%);

// Usage
a {
  color: @link-color;
  text-decoration: none;

  &:hover {
    color: @link-hover-color;
    text-decoration: underline;
  }
}

注意:@link-hover-color 使用了 Less 提供的一个内置函数,用于自动为鼠标悬停设置合适的颜色。

你还可以使用 darkenlightensaturate 和 desaturate 等 Less 内置的函数。


排版

通过几个变量就能轻松的设置字体、字号、行距等。

Bootstrap 利用这些变量提供了简单地定制排版的功能。

@font-family-sans-serif:  "Helvetica Neue", Helvetica, Arial, sans-serif;
@font-family-serif:       Georgia, "Times New Roman", Times, serif;
@font-family-monospace:   Menlo, Monaco, Consolas, "Courier New", monospace;
@font-family-base:        @font-family-sans-serif;

@font-size-base:          14px;
@font-size-large:         ceil((@font-size-base * 1.25)); // ~18px
@font-size-small:         ceil((@font-size-base * 0.85)); // ~12px

@font-size-h1:            floor((@font-size-base * 2.6)); // ~36px
@font-size-h2:            floor((@font-size-base * 2.15)); // ~30px
@font-size-h3:            ceil((@font-size-base * 1.7)); // ~24px
@font-size-h4:            ceil((@font-size-base * 1.25)); // ~18px
@font-size-h5:            @font-size-base;
@font-size-h6:            ceil((@font-size-base * 0.85)); // ~12px

@line-height-base:        1.428571429; // 20/14
@line-height-computed:    floor((@font-size-base * @line-height-base)); // ~20px

@headings-font-family:    inherit;
@headings-font-weight:    500;
@headings-line-height:    1.1;
@headings-color:          inherit;


图标

以下两个变量用于设置图标文件的位置和文件名。

@icon-font-path:          "../fonts/";
@icon-font-name:          "glyphicons-halflings-regular";


组件

组件贯穿整个 Bootstrap 框架,他们通过一些变量来设置默认值。

下面列出的是常用的几个。

@padding-base-vertical:          6px;
@padding-base-horizontal:        12px;

@padding-large-vertical:         10px;
@padding-large-horizontal:       16px;

@padding-small-vertical:         5px;
@padding-small-horizontal:       10px;

@padding-xs-vertical:            1px;
@padding-xs-horizontal:          5px;

@line-height-large:              1.33;
@line-height-small:              1.5;

@border-radius-base:             4px;
@border-radius-large:            6px;
@border-radius-small:            3px;

@component-active-color:         #fff;
@component-active-bg:            @brand-primary;

@caret-width-base:               4px;
@caret-width-large:              5px;


特定浏览器厂商的 mixin

特定浏览器厂商的 mixin 用于为不同厂商的浏览器使用相应的 CSS 属性前缀来支持各厂商的浏览器。


Box-sizing

通过这一个 mixin 来为所有组件设置盒模型。请参考这篇 来自 Mozilla 的文章

此 mixin 从 v3.2.0 版本开始就被列为 不建议使用 了,取而代之的是使用 Autoprefixer。

为了保持向后兼容,在 v4 版本之前,Bootstrap 将在内部继续使用这些 mixin。

.box-sizing(@box-model) {
  -webkit-box-sizing: @box-model; // Safari <= 5
     -moz-box-sizing: @box-model; // Firefox <= 19
          box-sizing: @box-model;
}


圆角

现在,所有现代浏览器都支持不带厂商前缀的 border-radius 属性了。

有鉴于此,我们没有提供 .border-radius() mixin,

但是,Bootstrap does 提供了用于快速设置同一侧圆角的 mixin 。

.border-top-radius(@radius) {
  border-top-right-radius: @radius;
   border-top-left-radius: @radius;
}
.border-right-radius(@radius) {
  border-bottom-right-radius: @radius;
     border-top-right-radius: @radius;
}
.border-bottom-radius(@radius) {
  border-bottom-right-radius: @radius;
   border-bottom-left-radius: @radius;
}
.border-left-radius(@radius) {
  border-bottom-left-radius: @radius;
     border-top-left-radius: @radius;
}


Box (Drop) 隐形

如果你的目标用户使用的是最新版本和更高级的浏览器和设备,只需单独使用 box-shadow 属性即可。

如果你需要兼容较老的 Android (低于 v4) 和 iOS 设备 (低于 iOS 5),

可以使用下面这个 不建议使用 的 mixin,便于帮你添加 -webkit 前缀。

由于 Bootstrap 并未官方提供对过时(不支持标准属性)平台的支持,此 mixin 从 v3.1.0 版本期就 不建议使用 了。

为了保持向后兼容,Bootstrap 将继续在内部使用此 mixin, 直到 Bootstrap v4。

在设置 box 阴影时务必使用 rgba() 颜色,这样可以使他们尽可能地与背景无缝融入。

.box-shadow(@shadow: 0 1px 3px rgba(0,0,0,.25)) {
  -webkit-box-shadow: @shadow; // iOS <4.3 & Android <4.1
          box-shadow: @shadow;
}


过渡效果

有多个 mixin 供你灵活使用。

可以一次性设置所有的过渡效果的属性,或者根据需要只是指定延时和持续时间。

此 mixin 从 v3.2.0 版本开始就被列为 不建议使用 了,取而代之的是使用 Autoprefixer。

为了保持向后兼容,在 v4 版本之前,Bootstrap 将在内部继续使用这些 mixin。

.transition(@transition) {
  -webkit-transition: @transition;
          transition: @transition;
}
.transition-property(@transition-property) {
  -webkit-transition-property: @transition-property;
          transition-property: @transition-property;
}
.transition-delay(@transition-delay) {
  -webkit-transition-delay: @transition-delay;
          transition-delay: @transition-delay;
}
.transition-duration(@transition-duration) {
  -webkit-transition-duration: @transition-duration;
          transition-duration: @transition-duration;
}
.transition-timing-function(@timing-function) {
  -webkit-transition-timing-function: @timing-function;
          transition-timing-function: @timing-function;
}
.transition-transform(@transition) {
  -webkit-transition: -webkit-transform @transition;
     -moz-transition: -moz-transform @transition;
       -o-transition: -o-transform @transition;
          transition: transform @transition;
}


变形

旋转、缩放、平移(移动)或倾斜任何对象。

此 mixin 从 v3.2.0 版本开始就被列为 不建议使用 了,取而代之的是使用 Autoprefixer。

为了保持向后兼容,在 v4 版本之前,Bootstrap 将在内部继续使用这些 mixin。

.rotate(@degrees) {
  -webkit-transform: rotate(@degrees);
      -ms-transform: rotate(@degrees); // IE9 only
          transform: rotate(@degrees);
}
.scale(@ratio; @ratio-y...) {
  -webkit-transform: scale(@ratio, @ratio-y);
      -ms-transform: scale(@ratio, @ratio-y); // IE9 only
          transform: scale(@ratio, @ratio-y);
}
.translate(@x; @y) {
  -webkit-transform: translate(@x, @y);
      -ms-transform: translate(@x, @y); // IE9 only
          transform: translate(@x, @y);
}
.skew(@x; @y) {
  -webkit-transform: skew(@x, @y);
      -ms-transform: skewX(@x) skewY(@y); // See https://github.com/twbs/bootstrap/issues/4885; IE9+
          transform: skew(@x, @y);
}
.translate3d(@x; @y; @z) {
  -webkit-transform: translate3d(@x, @y, @z);
          transform: translate3d(@x, @y, @z);
}

.rotateX(@degrees) {
  -webkit-transform: rotateX(@degrees);
      -ms-transform: rotateX(@degrees); // IE9 only
          transform: rotateX(@degrees);
}
.rotateY(@degrees) {
  -webkit-transform: rotateY(@degrees);
      -ms-transform: rotateY(@degrees); // IE9 only
          transform: rotateY(@degrees);
}
.perspective(@perspective) {
  -webkit-perspective: @perspective;
     -moz-perspective: @perspective;
          perspective: @perspective;
}
.perspective-origin(@perspective) {
  -webkit-perspective-origin: @perspective;
     -moz-perspective-origin: @perspective;
          perspective-origin: @perspective;
}
.transform-origin(@origin) {
  -webkit-transform-origin: @origin;
     -moz-transform-origin: @origin;
      -ms-transform-origin: @origin; // IE9 only
          transform-origin: @origin;
}


动画

仅适用一个 mixin 就可以在一个声明中使用所有 CSS3 所提供的动画属性,其他 mixin 用于设置单个属性。

T此 mixin 从 v3.2.0 版本开始就 不建议使用 了,取而代之的是使用 Autoprefixer。

为了保持向后兼容,在 v4 版本之前,Bootstrap 将在内部继续使用这些 mixin。

.animation(@animation) {
  -webkit-animation: @animation;
          animation: @animation;
}
.animation-name(@name) {
  -webkit-animation-name: @name;
          animation-name: @name;
}
.animation-duration(@duration) {
  -webkit-animation-duration: @duration;
          animation-duration: @duration;
}
.animation-timing-function(@timing-function) {
  -webkit-animation-timing-function: @timing-function;
          animation-timing-function: @timing-function;
}
.animation-delay(@delay) {
  -webkit-animation-delay: @delay;
          animation-delay: @delay;
}
.animation-iteration-count(@iteration-count) {
  -webkit-animation-iteration-count: @iteration-count;
          animation-iteration-count: @iteration-count;
}
.animation-direction(@direction) {
  -webkit-animation-direction: @direction;
          animation-direction: @direction;
}


透明度

为所有浏览器设置透明度,并为IE8提供 filter 备用滤镜。

.opacity(@opacity) {
  opacity: @opacity;
  // IE8 filter
  @opacity-ie: (@opacity * 100);
  filter: ~"alpha(opacity=@{opacity-ie})";
}


占位符文本

为表单控件中每个文本域提供占位符(Placeholder)文本的颜色。

.placeholder(@color: @input-color-placeholder) {
  &::-moz-placeholder           { color: @color; } // Firefox
  &:-ms-input-placeholder       { color: @color; } // Internet Explorer 10+
  &::-webkit-input-placeholder  { color: @color; } // Safari and Chrome
}


通过CSS在一个单独的元素中生成列。

.content-columns(@width; @count; @gap) {
  -webkit-column-width: @width;
     -moz-column-width: @width;
          column-width: @width;
  -webkit-column-count: @count;
     -moz-column-count: @count;
          column-count: @count;
  -webkit-column-gap: @gap;
     -moz-column-gap: @gap;
          column-gap: @gap;
}


渐变

便于把任何两种颜色变成背景渐变色。

想要使他更高级些,可以设置一个direction(方向),使用三种颜色,

也可以使用径向(radial)渐变。使用一个mixin(混入),你就可以得到所有需要的前缀语法。

#gradient > .vertical(#333; #000);
#gradient > .horizontal(#333; #000);
#gradient > .radial(#333; #000);

你也可以为标准的里两颜色线性渐变指定角度:

#gradient > .directional(#333; #000; 45deg);

如果你需要一个条纹风格的渐变,这也很容易。

只要指定一个颜色,我们将该颜色半透明的条纹覆盖其上。

#gradient > .striped(#333; 45deg);

再来试试三种颜色。利用此 mixin ,并为其设置第一种颜色、第二种颜色、

第二种颜色的色标(例如 25%),还有第三种颜色:

#gradient > .vertical-three-colors(#777; #333; 25%; #000);
#gradient > .horizontal-three-colors(#777; #333; 25%; #000);

当心! 如果你想删除某个渐变,确保将你所添加的针对 IE 的 filter 一并删除。

你可以通过使用 .reset-filter() mixin 和 background-image: none; 达到目的。


实用工具 mixin

实用工具 mixin 用于与不相关的 CSS 结合以达到特定目的或任务。


Clearfix -- 清除浮动

建议为需要清除浮动的元素使用 .clearfix() mixin ,尽量不要直接添加 class="clearfix" 类。

基于 Nicolas Gallagher 的 micro clearfix 代码。

// Mixin
.clearfix() {
  &:before,
  &:after {
    content: " ";
    display: table;
  }
  &:after {
    clear: both;
  }
}

// Usage
.container {
  .clearfix();
}


水平居中

让元素在其父元素中水平居中。

需要设置 width 或 max-width 属性。

// Mixin
.center-block() {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

// Usage
.container {
  width: 940px;
  .center-block();
}


尺寸助手 mixin

用于方便的指定对象的尺寸。

// Mixins
.size(@width; @height) {
  width: @width;
  height: @height;
}
.square(@size) {
  .size(@size; @size);
}

// Usage
.image { .size(400px; 300px); }
.avatar { .square(48px); }


可调整大小的文本域

方便设置任何文本域或其他元素的尺寸可调整。

默认依循浏览器默认行为 (both),即垂直、水平都可以调整。

.resizable(@direction: both) {
  // Options: horizontal, vertical, both
  resize: @direction;
  // Safari fix
  overflow: auto;
}


截断文本

此 mixin 用来以省略号代替被截断的文本。

元素必须是 block 或 inline-block 级。

// Mixin
.text-overflow() {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

// Usage
.branch-name {
  display: inline-block;
  max-width: 200px;
  .text-overflow();
}


视网膜屏幕(Retina)下的图片

通过指定两个图片路径和 @1x 图片尺寸,Bootstrap 还提供了对 @2x 媒体查询的支持。

 如果你的页面上有很多图片,建议在一个单独的媒体查询中手工编写针对视网膜屏幕的 CSS 代码。

.img-retina(@file-1x; @file-2x; @width-1x; @height-1x) {
  background-image: url("@{file-1x}");

  @media
  only screen and (-webkit-min-device-pixel-ratio: 2),
  only screen and (   min--moz-device-pixel-ratio: 2),
  only screen and (     -o-min-device-pixel-ratio: 2/1),
  only screen and (        min-device-pixel-ratio: 2),
  only screen and (                min-resolution: 192dpi),
  only screen and (                min-resolution: 2dppx) {
    background-image: url("@{file-2x}");
    background-size: @width-1x @height-1x;
  }
}

// Usage
.jumbotron {
  .img-retina("/img/bg-1x.png", "/img/bg-2x.png", 100px, 100px);
}

使用 Sass

虽然 Bootstrap 是基于 Less 构建的,我们还提供了一套官方支持的 Sass 移植版代码。

我们将这个版本放在单独的 GitHub 仓库中进行维护,并通过脚本处理源码更新。


包含的内容

由于 Sass 移植版存放于单独的仓库,并针对不同的使用群体,这个项目中的内容与 Bootstrap 主项目有很大不同。

这也是为了保证 Sass 移植版与更多基于 Sass 的系统相兼容。

路径 描述
lib/ Ruby gem code (Sass configuration, Rails and Compass integrations)
tasks/ Converter scripts (turning upstream Less to Sass)
test/ Compilation tests
templates/ Compass package manifest
vendor/assets/ Sass, JavaScript, and font files
Rakefile Internal tasks, such as rake and convert

请访问 Sass 移植版在 GitHub 上的仓库 来了解这些文件。


安装

关于如何安装并使用 Bootstrap 的 Sass 移植版,请参考GitHub 仓库中的 readme 文件。 

此仓库中包含了最新的源码以及如何与 Rails、Compass 以及标准 Sass 项目一同使用的详细信息。
























未完待续,下一章节,つづく

你可能感兴趣的:(Bootstrap)