本文最后更新于:December 3, 2021 pm
Bootstrap 是最受欢迎的 HTML、CSS 和 JS 框架,用于开发响应式布局、移动设备优先的 WEB 项目。Bootstrap是基于HTML、CSS、JavaScript 开发的简洁、直观、强悍的前端开发框架,使得 Web 开发更加快捷。提供了优雅的HTML和CSS规范。
目录 1.字体图标 1.1 用法 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 //一<span class ="glyphicon glyphicon-search" > </span > //二<button type ="button" class ="btn btn-default" aria-label ="Left Align" > <span class ="glyphicon glyphicon-align-left" aria-hidden ="true" > </span > </button > <button type ="button" class ="btn btn-default btn-lg" > <span class ="glyphicon glyphicon-star" aria-hidden ="true" > </span > Star</button > //示例<p > <button type ="button" class ="btn btn-default" > <span class ="glyphicon glyphicon-sort-by-attributes" > </span > </button > <button type ="button" class ="btn btn-default" > <span class ="glyphicon glyphicon-sort-by-attributes-alt" > </span > </button > <button type ="button" class ="btn btn-default" > <span class ="glyphicon glyphicon-sort-by-order" > </span > </button > <button type ="button" class ="btn btn-default" > <span class ="glyphicon glyphicon-sort-by-order-alt" > </span > </button > </p > <button type ="button" class ="btn btn-default btn-lg" > <span class ="glyphicon glyphicon-user" > </span > User</button > <button type ="button" class ="btn btn-default btn-sm" > <span class ="glyphicon glyphicon-user" > </span > User</button > <button type ="button" class ="btn btn-default btn-xs" > <span class ="glyphicon glyphicon-user" > </span > User</button >
1.2 定制字体图标 <button type ="button" class ="btn btn-primary btn-lg" > <span class ="glyphicon glyphicon-user" > </span > User</button >
1.2.1 定制字体尺寸 <button type ="button" class ="btn btn-primary btn-lg" style ="font-size: 60px" > <span class ="glyphicon glyphicon-user" > </span > User</button >
1.2.2 定制字体颜色 <button type ="button" class ="btn btn-primary btn-lg" style ="color: rgb(212, 106, 64);" > <span class ="glyphicon glyphicon-user" > </span > User</button >
1.2.3 应用文本阴影 <button type ="button" class ="btn btn-primary btn-lg" style ="text-shadow: black 5px 3px 3px;" > <span class ="glyphicon glyphicon-user" > </span > User</button >
2.下拉菜单
类
描述
.dropdown
指定下拉菜单,下拉菜单都包裹在 .dropdown 里
.dropdown-menu
创建下拉菜单
.dropdown-menu-right
下拉菜单右对齐
.dropdown-header
下拉菜单中添加标题
.dropup
指定向上弹出的下拉菜单
.disabled
下拉菜单中的禁用项
.divider
下拉菜单中的分割线
将下拉菜单触发器和下拉菜单都包裹在 .dropdown
里,或者另一个声明了 position: relative;
的元素。然后加入组成菜单的 HTML 代码。
示例:
<div class ="dropdown" > <button class ="btn btn-default dropdown-toggle" type ="button" id ="dropdownMenu1" data-toggle ="dropdown" aria-haspopup ="true" aria-expanded ="true" > Dropdown <span class ="caret" > </span > </button > <ul class ="dropdown-menu" aria-labelledby ="dropdownMenu1" > <li > <a href ="#" > Action</a > </li > <li > <a href ="#" > Another action</a > </li > <li > <a href ="#" > Something else here</a > </li > <li role ="separator" class ="divider" > </li > <li > <a href ="#" > Separated link</a > </li > </ul > </div >
通过为下拉菜单的父元素设置 .dropup
类,可以让菜单向上弹出(默认是向下弹出的)。
<div class ="dropup" > <button class ="btn btn-default dropdown-toggle" type ="button" id ="dropdownMenu2" data-toggle ="dropdown" aria-haspopup ="true" aria-expanded ="false" > Dropup <span class ="caret" > </span > </button > <ul class ="dropdown-menu" aria-labelledby ="dropdownMenu2" > <li > <a href ="#" > Action</a > </li > <li > <a href ="#" > Another action</a > </li > <li > <a href ="#" > Something else here</a > </li > <li role ="separator" class ="divider" > </li > <li > <a href ="#" > Separated link</a > </li > </ul > </div >
2.1 对齐 通过向 .dropdown-menu
添加 类 .pull-right
来向右对齐下拉菜单。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <div class ="dropdown" > <button type ="button" class ="btn dropdown-toggle" id ="dropdownMenu1" data-toggle ="dropdown" > 主题 <span class ="caret" > </span > </button > <ul class ="dropdown-menu pull-right" role ="menu" aria-labelledby ="dropdownMenu1" > <li role ="presentation" > <a role ="menuitem" tabindex ="-1" href ="#" > Java</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" class ="divider" > </li > <li role ="presentation" > <a role ="menuitem" tabindex ="-1" href ="#" > 分离的链接</a > </li > </ul > </div >
但是,不再建议对下拉菜单使用 .pull-right
类。如需将菜单右对齐,请使用 .dropdown-menu-right
类。如需左对齐,请使用 .dropdown-menu-left
类。
<ul class ="dropdown-menu dropdown-menu-right" aria-labelledby ="dLabel" > ...</ul >
2.2 标题 可以使用 .dropdown-header
向下拉菜单的标签区域添加标题。
2.3 分割线 为下拉菜单添加一条分割线,用于将多个链接分组。
<li role ="separator" class ="divider" > </li >
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <div class ="dropdown" > <button type ="button" class ="btn dropdown-toggle" 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 ="#" > Java</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" class ="divider" > </li > //分割线 <li role ="presentation" class ="dropdown-header" > 下拉菜单标题</li > <li role ="presentation" > <a role ="menuitem" tabindex ="-1" href ="#" > 分离的链接</a > </li > </ul > </div >
2.4 禁用的菜单项 为下拉菜单中的 <li>
元素添加 .disabled
类,从而禁用相应的菜单项。
<ul class ="dropdown-menu" aria-labelledby ="dropdownMenu4" > <li > <a href ="#" > Regular link</a > </li > <li class ="disabled" > <a href ="#" > Disabled link</a > </li > <li > <a href ="#" > Another link</a > </li > </ul >
3.按钮组 通过按钮组容器把一组按钮放在同一行里。通过与按钮的 JavaScript 插件联合使用,可以设置为单选框或多选框的样式和行为。
当为 .btn-group
中的元素应用工具提示或弹出框时,必须指定 container: 'body'
选项,这样可以避免不必要的副作用(例如工具提示或弹出框触发时,会让页面元素变宽和/或失去圆角)。
为了向使用辅助技术 - 如屏幕阅读器 - 的用户正确传达一正确的按钮分组,需要提供一个合适的 role
属性。对于按钮组合,应该是 role="group"
,对于toolbar(工具栏)应该是 role="toolbar"
。使用 aria-label
,但是, aria-labelledby
也可以使用。
<div class ="btn-group" role ="group" aria-label ="..." > <button type ="button" class ="btn btn-default" > Left</button > <button type ="button" class ="btn btn-default" > Middle</button > <button type ="button" class ="btn btn-default" > Right</button > </div >
3.1 按钮工具栏 把一组 <div class="btn-group">
组合进一个 <div class="btn-toolbar">
中就可以做成更复杂的组件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <div class ="btn-toolbar" role ="toolbar" > <div class ="btn-group" > <button type ="button" class ="btn btn-default" > 按钮 1</button > <button type ="button" class ="btn btn-default" > 按钮 2</button > <button type ="button" class ="btn btn-default" > 按钮 3</button > </div > <div class ="btn-group" > <button type ="button" class ="btn btn-default" > 按钮 4</button > <button type ="button" class ="btn btn-default" > 按钮 5</button > <button type ="button" class ="btn btn-default" > 按钮 6</button > </div > <div class ="btn-group" > <button type ="button" class ="btn btn-default" > 按钮 7</button > <button type ="button" class ="btn btn-default" > 按钮 8</button > <button type ="button" class ="btn btn-default" > 按钮 9</button > </div > </div >
3.2 按钮大小 只要给 .btn-group
加上 .btn-group-*
类,就省去为按钮组中的每个按钮都赋予尺寸类了,如果包含了多个按钮组时也适用。
<div class ="btn-group btn-group-lg" > <button type ="button" class ="btn btn-default" > 按钮 1</button > <button type ="button" class ="btn btn-default" > 按钮 2</button > <button type ="button" class ="btn btn-default" > 按钮 3</button > </div > <div class ="btn-group btn-group-sm" > <button type ="button" class ="btn btn-default" > 按钮 4</button > <button type ="button" class ="btn btn-default" > 按钮 5</button > <button type ="button" class ="btn btn-default" > 按钮 6</button > </div > <div class ="btn-group btn-group-xs" > <button type ="button" class ="btn btn-default" > 按钮 7</button > <button type ="button" class ="btn btn-default" > 按钮 8</button > <button type ="button" class ="btn btn-default" > 按钮 9</button > </div >
3.3 嵌套 想要把下拉菜单混合到一系列按钮中,只须把 .btn-group
放入另一个 .btn-group
中。
<div class ="btn-group" > <button type ="button" class ="btn btn-default" > 按钮 1</button > <button type ="button" class ="btn btn-default" > 按钮 2</button > <div class ="btn-group" > <button type ="button" class ="btn btn-default dropdown-toggle" data-toggle ="dropdown" > 下拉 <span class ="caret" > </span > </button > <ul class ="dropdown-menu" > <li > <a href ="#" > 下拉链接 1</a > </li > <li > <a href ="#" > 下拉链接 2</a > </li > </ul > </div > </div >
3.4 垂直的按钮组 可以使用类 .btn-group-vertical
。
<div class ="btn-group-vertical" > <button type ="button" class ="btn btn-default" > 按钮 1</button > <button type ="button" class ="btn btn-default" > 按钮 2</button > <div class ="btn-group-vertical" > <button type ="button" class ="btn btn-default dropdown-toggle" data-toggle ="dropdown" > 下拉 <span class ="caret" > </span > </button > <ul class ="dropdown-menu" > <li > <a href ="#" > 下拉链接 1</a > </li > <li > <a href ="#" > 下拉链接 2</a > </li > </ul > </div > </div >
4.按钮式下拉菜单 把任意一个按钮放入 .btn-group
中,然后加入适当的菜单标签,就可以让按钮作为菜单的触发器了。也可以使用 <span class="caret"></span>
来指示按钮作为下拉菜单。
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 47 48 49 50 51 52 53 54 55 //1<div class ="btn-group" > <button type ="button" class ="btn btn-default dropdown-toggle" data-toggle ="dropdown" aria-haspopup ="true" aria-expanded ="false" > Action <span class ="caret" > </span > </button > <ul class ="dropdown-menu" > <li > <a href ="#" > Action</a > </li > <li > <a href ="#" > Another action</a > </li > <li > <a href ="#" > Something else here</a > </li > <li role ="separator" class ="divider" > </li > <li > <a href ="#" > Separated link</a > </li > </ul > </div > //2<div class ="btn-group" > <button type ="button" class ="btn btn-default dropdown-toggle" data-toggle ="dropdown" > 默认 <span class ="caret" > </span > </button > <ul class ="dropdown-menu" role ="menu" > <li > <a href ="#" > 功能</a > </li > <li > <a href ="#" > 另一个功能</a > </li > <li > <a href ="#" > 其他</a > </li > <li class ="divider" > </li > <li > <a href ="#" > 分离的链接</a > </li > </ul > </div > <div class ="btn-group" > <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 ="#" > 功能</a > </li > <li > <a href ="#" > 另一个功能</a > </li > <li > <a href ="#" > 其他</a > </li > <li class ="divider" > </li > <li > <a href ="#" > 分离的链接</a > </li > </ul > </div >
4.1 分裂式按钮下拉菜单 分割的按钮下拉菜单使用与下拉菜单按钮大致相同的样式,但是对下拉菜单添加了原始的功能。分割按钮的左边是原始的功能,右边是显示下拉菜单的切换。只是多一个分开的按钮。
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 //1<div class ="btn-group" > <button type ="button" class ="btn btn-danger" > Action</button > <button type ="button" class ="btn btn-danger dropdown-toggle" data-toggle ="dropdown" aria-haspopup ="true" aria-expanded ="false" > <span class ="caret" > </span > <span class ="sr-only" > Toggle Dropdown</span > </button > <ul class ="dropdown-menu" > <li > <a href ="#" > Action</a > </li > <li > <a href ="#" > Another action</a > </li > <li > <a href ="#" > Something else here</a > </li > <li role ="separator" class ="divider" > </li > <li > <a href ="#" > Separated link</a > </li > </ul > </div > //2<div class ="btn-group" > <button type ="button" class ="btn btn-default" > 默认</button > <button type ="button" class ="btn btn-default dropdown-toggle" data-toggle ="dropdown" > <span class ="caret" > </span > <span class ="sr-only" > 切换下拉菜单</span > </button > <ul class ="dropdown-menu" role ="menu" > <li > <a href ="#" > 功能</a > </li > <li > <a href ="#" > 另一个功能</a > </li > <li > <a href ="#" > 其他</a > </li > <li class ="divider" > </li > <li > <a href ="#" > 分离的链接</a > </li > </ul > </div > <div class ="btn-group" > <button type ="button" class ="btn btn-primary" > 原始</button > <button type ="button" class ="btn btn-primary dropdown-toggle" data-toggle ="dropdown" > <span class ="caret" > </span > <span class ="sr-only" > 切换下拉菜单</span > </button > <ul class ="dropdown-menu" role ="menu" > <li > <a href ="#" > 功能</a > </li > <li > <a href ="#" > 另一个功能</a > </li > <li > <a href ="#" > 其他</a > </li > <li class ="divider" > </li > <li > <a href ="#" > 分离的链接</a > </li > </ul > </div >
4.2 按钮下拉菜单的大小 可以使用带有各种大小按钮的下拉菜单:.btn-lg
、.btn-sm
或 .btn-xs
。
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 47 48 49 50 51 52 53 //1<div class ="btn-group" > <button type ="button" class ="btn btn-default dropdown-toggle btn-lg" data-toggle ="dropdown" > 默认 <span class ="caret" > </span > </button > <ul class ="dropdown-menu" role ="menu" > <li > <a href ="#" > 功能</a > </li > <li > <a href ="#" > 另一个功能</a > </li > <li > <a href ="#" > 其他</a > </li > <li class ="divider" > </li > <li > <a href ="#" > 分离的链接</a > </li > </ul > </div > //2<div class ="btn-group" > <button class ="btn btn-default btn-lg dropdown-toggle" type ="button" data-toggle ="dropdown" aria-haspopup ="true" aria-expanded ="false" > Large button <span class ="caret" > </span > </button > <ul class ="dropdown-menu" > ... </ul > </div > <div class ="btn-group" > <button class ="btn btn-default btn-sm dropdown-toggle" type ="button" data-toggle ="dropdown" aria-haspopup ="true" aria-expanded ="false" > Small button <span class ="caret" > </span > </button > <ul class ="dropdown-menu" > ... </ul > </div > <div class ="btn-group" > <button class ="btn btn-default btn-xs dropdown-toggle" type ="button" data-toggle ="dropdown" aria-haspopup ="true" aria-expanded ="false" > Extra small button <span class ="caret" > </span > </button > <ul class ="dropdown-menu" > ... </ul > </div >
4.3 向上弹出式菜单 给父元素添加 .dropup
类就能使触发的下拉菜单朝上方打开。
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 <div class ="row" style ="margin-left:50px; margin-top:200px" > <div class ="btn-group dropup" > <button type ="button" class ="btn btn-default dropdown-toggle" data-toggle ="dropdown" > 默认 <span class ="caret" > </span > </button > <ul class ="dropdown-menu" role ="menu" > <li > <a href ="#" > 功能</a > </li > <li > <a href ="#" > 另一个功能</a > </li > <li > <a href ="#" > 其他</a > </li > <li class ="divider" > </li > <li > <a href ="#" > 分离的链接</a > </li > </ul > </div > <div class ="btn-group dropup" > <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 ="#" > 功能</a > </li > <li > <a href ="#" > 另一个功能</a > </li > <li > <a href ="#" > 其他</a > </li > <li class ="divider" > </li > <li > <a href ="#" > 分离的链接</a > </li > </ul > </div > </div >