Bootstrap
按钮
本章将通过实例讲解如何使用 Bootstrap 按钮。任何带有 class
.btn
的元素都会继承圆角灰色按钮的默认外观。但是 Bootstrap 提供了一些选项来定义按钮的样式,具体如下表所示:
以下样式可用于<a>, <button>, 或 <input> 元素上:
为按钮添加基本样式
.btn-default
默认/标准按钮
.btn-primary
原始按钮样式(未被操作)
.btn-success
表示成功的动作
.btn-info
该样式可用于要弹出信息的按钮
.btn-warning
表示需要谨慎操作的按钮
.btn-danger
表示一个危险动作的按钮操作
.btn-link
让按钮看起来像个链接 (仍然保留按钮行为)
.btn-lg
制作一个大按钮
.btn-sm
制作一个小按钮
.btn-xs
制作一个超小按钮
.btn-block
块级按钮(拉伸至父元素100%的宽度)
.active
按钮被点击
.disabled
下面的实例演示了上面所有的按钮 class:
<
button
type
=
"
button
"
class
=
"
btn btn-default
"
>
默认按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
原始按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-success
"
>
成功按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-info
"
>
信息按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-warning
"
>
警告按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-danger
"
>
危险按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-link
"
>
链接按钮
</
button
>
尝试一下 »
结果如下所示:
<
button
type
=
"
button
"
class
=
"
btn btn-primary btn-lg
"
>
大的原始按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-default btn-lg
"
>
大的按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
默认大小的原始按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-default
"
>
默认大小的按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary btn-sm
"
>
小的原始按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-default btn-sm
"
>
小的按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary btn-xs
"
>
特别小的原始按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-default btn-xs
"
>
特别小的按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary btn-lg btn-block
"
>
块级的原始按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-default btn-lg btn-block
"
>
块级的按钮
</
button
>
尝试一下 »
结果如下所示:
<
button
type
=
"
button
"
class
=
"
btn btn-default btn-lg
"
>
默认按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-default btn-lg active
"
>
激活按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary btn-lg
"
>
原始按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary btn-lg active
"
>
激活的原始按钮
</
button
>
尝试一下 »
结果如下所示:
当您禁用一个按钮时,它的颜色会变淡 50%,并失去渐变。
下表列出了让按钮元素和锚元素呈禁用状态的 class:
元素Class
按钮元素添加
disabled
属性
到 <button> 按钮。
锚元素添加
disabled
class
到 <a> 按钮。
下面的实例演示了这点:
<
button
type
=
"
button
"
class
=
"
btn btn-default btn-lg
"
>
默认按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-default btn-lg
"
disabled
=
"
disabled
"
>
禁用按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary btn-lg
"
>
原始按钮
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary btn-lg
"
disabled
=
"
disabled
"
>
禁用的原始按钮
</
button
>
<
a
href
=
"
#
"
class
=
"
btn btn-default btn-lg
"
role
=
"
button
"
>
链接
</
a
>
<
a
href
=
"
#
"
class
=
"
btn btn-default btn-lg disabled
"
role
=
"
button
"
>
禁用链接
</
a
>
<
a
href
=
"
#
"
class
=
"
btn btn-primary btn-lg
"
role
=
"
button
"
>
原始链接
</
a
>
<
a
href
=
"
#
"
class
=
"
btn btn-primary btn-lg disabled
"
role
=
"
button
"
>
禁用的原始链接
</
a
>
尝试一下 »
结果如下所示:
您可以在 <a>、<button> 或 <input> 元素上使用按钮 class。但是建议您在 <button> 元素上使用按钮 class,避免跨浏览器的不一致性问题。
下面的实例演示了这点:
<
a
class
=
"
btn btn-default
"
href
=
"
#
"
role
=
"
button
"
>
链接
</
a
>
<
button
class
=
"
btn btn-default
"
type
=
"
submit
"
>
按钮
</
button
>
<
input
class
=
"
btn btn-default
"
type
=
"
button
"
value
=
"
输入
"
>
<
input
class
=
"
btn btn-default
"
type
=
"
submit
"
value
=
"
提交
"
>
尝试一下 »
结果如下所示:
<
div
class
=
"
btn-group
"
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Apple
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Samsung
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Sony
</
button
>
</
div
>
尝试一下 »
使用 .btn-group-lg|sm|xs 来控制按钮组的大小:
<
div
class
=
"
btn-group btn-group-lg
"
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Apple
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Samsung
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Sony
</
button
>
</
div
>
尝试一下 »
如果要设置垂直方向的按钮可以通过 .btn-group-vertical 类来设置:
<
div
class
=
"
btn-group-vertical
"
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Apple
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Samsung
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Sony
</
button
>
</
div
>
尝试一下 »
自适应大小的按钮组
可以通过 .btn-group-justified 类来设置自适应大小的按钮组。
以下实例使用 a 标签来展示:
<
div
class
=
"
btn-group btn-group-justified
"
>
<
a
href
=
"
#
"
class
=
"
btn btn-primary
"
>
Apple
</
a
>
<
a
href
=
"
#
"
class
=
"
btn btn-primary
"
>
Samsung
</
a
>
<
a
href
=
"
#
"
class
=
"
btn btn-primary
"
>
Sony
</
a
>
</
div
>
尝试一下 »
注意:
如果是
<button>
元素, 你需要在外层使用
.btn-group
类来包裹:
<
div
class
=
"
btn-group btn-group-justified
"
>
<
div
class
=
"
btn-group
"
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Apple
</
button
>
</
div
>
<
div
class
=
"
btn-group
"
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Samsung
</
button
>
</
div
>
<
div
class
=
"
btn-group
"
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Sony
</
button
>
</
div
>
</
div
>
尝试一下 »
内嵌下拉菜单的按钮组
按钮组内嵌的按钮可以设置下拉菜单,如下实例:
<
div
class
=
"
btn-group
"
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Apple
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Samsung
</
button
>
<
div
class
=
"
btn-group
"
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary dropdown-toggle
"
data-toggle
=
"
dropdown
"
>
Sony
<
span
class
=
"
caret
"
>
</
span
>
</
button
>
<
ul
class
=
"
dropdown-menu
"
role
=
"
menu
"
>
<
li
>
<
a
href
=
"
#
"
>
Tablet
</
a
>
</
li
>
<
li
>
<
a
href
=
"
#
"
>
Smartphone
</
a
>
</
li
>
</
div
>
</
div
>
尝试一下 »
<
div
class
=
"
btn-group
"
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary
"
>
Sony
</
button
>
<
button
type
=
"
button
"
class
=
"
btn btn-primary dropdown-toggle
"
data-toggle
=
"
dropdown
"
>
<
span
class
=
"
caret
"
>
</
span
>
</
button
>
<
ul
class
=
"
dropdown-menu
"
role
=
"
menu
"
>
<
li
>
<
a
href
=
"
#
"
>
Tablet
</
a
>
</
li
>
<
li
>
<
a
href
=
"
#
"
>
Smartphone
</
a
>
</
li
>
</
div
>
尝试一下 »