表单
表单中常见的元素主要包括:文本输入框、下拉选择框、单选按钮、复选按钮、文本域和按钮等。
Bootstrap 框架默认的表单是垂直显示风格,但很多时候我们需要的水平表单风格(标签居左,表单控件居右)
在 Bootstrap 框架中要实现水平表单效果,必须满足以下两个条件:
1、在<form>
元素是使用类名“form-horizontal”。
2、配合 Bootstrap 框架的网格系统。
在<form>
元素上使用类名“form-horizontal”主要有以下几个作用:
1、设置表单控件padding
和margin
值。
2、改变“form-group”的表现形式,类似于网格系统的“row”。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 < form class = "form-horizontal" role = "form" >
< div class = "form-group" >
< label for = "inputEmail3" class = "col-sm-2 control-label" > 邮箱</ label >
< div class = "col-sm-10" >
< input type = "email" class = "form-control" id = "inputEmail3" placeholder = "请输入您的邮箱地址" >
</ div >
</ div >
< div class = "form-group" >
< label for = "inputPassword3" class = "col-sm-2 control-label" > 密码</ label >
< div class = "col-sm-10" >
< input type = "password" class = "form-control" id = "inputPassword3" placeholder = "请输入您的邮箱密码" >
</ div >
</ div >
< div class = "form-group" >
< div class = "col-sm-offset-2 col-sm-10" >
< div class = "checkbox" >
< label >
< input type = "checkbox" > 记住密码
</ label >
</ div >
</ div >
</ div >
< div class = "form-group" >
< div class = "col-sm-offset-2 col-sm-10" >
< button type = "submit" class = "btn btn-default" > 进入邮箱</ button >
</ div >
</ div >
</ form >
有时候我们需要将表单的控件都在一行内显示,只需要在<form>
元素中添加类名“form-inline”即可。
如果你要在input前面添加一个label标签时,会导致input换行显示。如果你必须添加这样的一个label标签,并且不想让input换行,你需要将label标签也放在容器“form-group”中
表单控件
单行输入框,常见的文本输入框,也就是 input 的 type 属性值为 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 >
Bootstrap框架中的下拉选择框使用和原始的一致,多行选择设置multiple属性的值为multiple。Bootstrap框架会为这些元素提供统一的样式风格。如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 < 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 >
文本域和原始使用方法一样,设置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 >
在 Bootstrap 框架中,主要借助“.checkbox”和“.radio”样式,来处理复选框、单选按钮与标签的对齐方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 < 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 >
有时候,为了布局的需要,将复选框和单选按钮需要水平排列。Bootstrap 框架也做了这方面的考虑:
1、如果 checkbox 需要水平排列,只需要在 label 标签上添加类名“checkbox-inline”
2、如果 radio 需要水平排列,只需要在 label 标签上添加类名“radio-inline”
按钮也是表单重要控件之一,制作按钮通常使用下面代码来实现:
input[type=“submit”]
input[type=“button”]
input[type=“reset”]
<button>
在 Bootstrap 框架中的按钮都是采用<button>
来实现。
Bootstrap 有它自己的 button 按钮风格,看起来要比默认的按钮好看得多。即 btn
class 属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 < 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 >
Bootstrap 的 btn-block
class,使按钮成为块级元素,你的按钮将会伸展并填满页面整个水平空间,任何在它之下的元素都会跟着浮动至该区块的下一行。
Bootstrap 使用一种响应式网格布局——可轻松实现将多个元素放入一行并指定各个元素的相对宽度的需求。Bootstrap 中大多数的class属性都可以设置于 div 元素中。
前面看到的表单控件都正常的大小。可以通过设置控件的height,line-height,padding和font-size等属性来实现控件的高度设置。不过Bootstrap框架还提供了两个不同的类名,用来控制表单控件的高度。这两个类名是:
1、input-sm:让控件比正常大小更小
2、input-lg:让控件比正常大小更大
这两个类适用于表单中的 input,textarea 和 select 控件
不管是“input-sm”还是“input-lg”仅对控件高度做了处理。但往往很多时候,我们需要控件宽度也要做一定的变化处理。这个时候就要借住Bootstrap框架的网格系统
1
2
3
4
5
6
7
8
9
10
11
12
13
14 < 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 >
表单控件状态
表单状态的作用:
每一种状态都能给用户传递不同的信息,比如表单有焦点的状态可以告诉用户可以输入或选择东西,禁用状态可以告诉用户不可以输入或选择东西,还有就是表单控件验证状态,可以告诉用户的操作是否正确等
焦点状态是通过伪类“:focus”来实现。Bootstrap 框架中表单控件的焦点状态删除了 outline 的默认样式,重新添加阴影效果。
. form-control : focus {
border-color : #66afe9 ;
outline : 0 ;
-webkit- box-shadow : inset 0 1 px 1 pxrgba ( 0 , 0 , 0 , .075 ), 0 0 8 px rgba ( 102 , 175 , 233 , .6 );
box-shadow : inset 0 1 px 1 pxrgba ( 0 , 0 , 0 , .075 ), 0 0 8 px 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框架中,file、radio和checkbox控件在焦点状态下的效果也与普通的input控件不太一样,主要是因为Bootstrap对他们做了一些特殊处理:
Bootstrap框架的表单控件的禁用状态和普通的表单禁用状态实现方法是一样的,在相应的表单控件上添加属性“disabled”
< input class = "form-control" type = "text" placeholder = "表单已禁用,不能输入" disabled >
在使用了“form-control”的表单控件中,样式设置了禁用表单背景色为灰色,而且手型变成了不准输入的形状。如果控件中不使用类名“form-control”,禁用的控件只会有一个不准输入的手型出来
在Bootstrap框架中,如果fieldset设置了disabled属性,整个域都将处于被禁用状态。
对于整个禁用的域中,如果legend中有输入框的话,这个输入框是无法被禁用的
在制作表单时,不免要做表单验证。同样也需要提供验证状态样式,在Bootstrap框架中同样提供这几种效果。
1、.has-warning:警告状态(黄色)
2、.has-error:错误状态(红色)
3、.has-success:成功状态(绿色)
使用的时候只需要在form-group容器上对应添加状态类名
1
2
3
4
5
6
7
8
9
10
11
12
13
14 < 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 >
从效果可以看出,三种状态下效果都是一样的,只是颜色不一样而以。
很多时候,在表单验证的时候,不同的状态会提供不同的 icon,比如成功是一个对号(√),错误是一个叉号(×)等。在Bootstrap框中也提供了这样的效果。如果你想让表单在对应的状态下显示 icon 出来,只需要在对应的状态下添加类名“has-feedback”
1
2
3
4
5
6
7
8
9
10
11
12
13 < 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" >
......
</ div >
< div class = "form-group has-error has-feedback" >
......
</ div >
</ form >
从效果图中可以看出,图标都居右。在 Bootstrap 的小图标都是使用@font-face
来制作, 而且必须在表单中添加了一个 span 元素
表单提示信息
平常在制作表单验证时,要提供不同的提示信息。在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 >
按钮
< 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 >
一般制作按钮除了使用<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框架中,对于按钮的大小,也是可以定制的。类似于input一样,通过在基础按钮“.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 >
有时候在制作按钮的时候需要按钮宽度充满整个父容器(width:100%),特别是在移动端的制作中。那么前面的方法我们都无法很好的实现,除非重新定义按钮的宽度。其实在Bootstrap中并不需要这样做,Bootstrap框架中提供了一个类名“btn-block”。按钮使用这个类名就可以让按钮充满整个容器,并且这个按钮不会有任何的padding和margin值。在实际当中,常把这种按钮称为块状按钮
使用方法和前面的类似,只需要在原按钮类名上添加“.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)几种。
而且不同风格下的按钮都具有这几种状态效果,只是颜色做了一定的调整
和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 框架中对于图像的样式风格提供以下几种风格:
1、img-responsive:响应式图片,主要针对于响应式设计
2、img-rounded:圆角图片
3、img-circle:圆形图片
4、img-thumbnail:缩略图片
< 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" >
设置图片大小:
由于样式没有对图片做大小上的样式限制,所以在实际使用的时候,需要通过其他的方式来处理图片大小。比如说控制图片容器大小。(注意不可以通过css样式直接修改img图片的大小,这样操作就不响应了)
图标
Web制作中常看到的小icon图标是一个优秀Web中不可缺少的一部分,起到画龙点睛的效果
在Bootstrap框架中提供了近200个不同的icon图片,而这些图标都是使用CSS3的@font-face
属性配合字体来实现的icon效果。
在网页中使用图标也非常的简单,在任何内联元素上应用所对应的样式即可:
< 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-”前缀的类名开始,然后后缀表示图标的名称
除了使用glyphicon.com提供的图标之外,还可以使用第三方为Bootstrap框架设计的图标字体,如Font Awesome(http://www.bootcss.com/p/font-awesome/)
Font Awesome 是一个非常方便的图标库。这些图标都是矢量图形,被保存在 .svg 的文件格式中。这些图标就和字体一样,你可以通过像素单位指定它们的大小,它们将会继承其父HTML元素的字体大小。
你可以将 Font Awesome 图标库增添至任何一个应用中,方法很简单,只需要在你的 HTML 头部增加下列代码即可:
<link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.2.0/css/font-awesome.min.css"/>
i 元素起初一般是让其它元素有斜体(italic)的功能,不过现在一般用来指代图标。你可以将 Font Awesome 中的 class 属性添加到 i 元素中,把它变成一个图标,比如:
<i class="fa fa-info-circle"></i>
就是图标