# Bootstrap V4 ## 概述 ### V4 变更项 Migration * 写法上最大的一个改变是,将原先的多层选择器压平了,子项上需要添加类,这增加了使用灵活性,但需要多写几个类名了。 * 浏览器支持方面,IE8 将不再被支持 * 好多附加属性统一移到了 utilise 模块下 ### 浏览器/设备支持 Browsers & devices 移动端的 hover 都是模拟出来的,实际体验并不好,所以默认是关闭的。 出于兼容和优化不同浏览器表现,bootstrap 采用了一些 hack,这些地方验证器会报错。 ### JS插件 JavaScript 所有插件都依赖 jQuery,如果单独使用一些插件的话,可能插件之间还有依赖。 几乎所有的插件都通过 data 属性来开启和配置插件功能,注意,一个元素只能开启一种插件功能。 ## 布局 Layout ### Grid system * 分3个主要的组件 — containers, rows, columns. * 容器 — `.container` 其 `max-width` 会在不同断点发生变化,而 `.container-fluid` 宽度始终是 100%;容器支持嵌套 * Rows 用于组织行,.row { margin-left: -15px; margin-right: -15px; &::after{ .clearfix } } * 内容应该放置于列中,但如果只有一列,那么可以直接置于 .row 下。 * Column classes 用于指示在不同尺寸屏幕下所占的宽度比例,比例是以百分比形式出现的,列间存在 gutter(padding) * V4版将屏幕大小分成了5个等级:extra small, small, medium, large, and extra large。 * Grid tiers 是基于 min-width 的,所以小屏的定义包含了大屏的情形,如果大屏有定义则会覆盖小屏的定义。 ```html
One of three columns
One of three columns
One of three columns
```
Extra small
<544px
Small
≥544px
Medium
≥768px
Large
≥992px
Extra large
≥1200px
Grid behaviorHorizontal at all timesCollapsed to start, horizontal above breakpoints
Container width None (auto) 576px 720px 940px 1140px
Class prefix.col-xs-.col-sm-.col-md-.col-lg-.col-xl-
# of columns12
Gutter width1.875rem / 30px (15px on each side of a column)
NestableYes
OffsetsYes
Column orderingYes
Class | Description ----- | ----------- .container | 根据不同屏幕宽度会有不同的 `max-width` .container-fluid | 宽度始终是 100% .row | 定义一个行,如果各列的数值加起来大于12,那么会换行,但与其他行还是会有分隔的 .col-[]-[] | 如 `col-sm-4`,定义不同宽度时列所占的比例,注意列之间存在 gutter .offset-[]-[] | 偏移,如 `.offset-lg-0` `margin-left: percentage(($columns / $grid-columns)` .push-[]-[] | 换序,如 `push-md-3` `left: percentage(($columns / $grid-columns)` .pull-[]-[] | 换序,如 `pull-md-9` `right: percentage(($columns / $grid-columns)` ### Flexbox grid system 采用 CSS’s Flexible Box module,提供更高级的布局方式,默认未开启,因为 IE9 不支持。 ### Media Object The media object is an abstract element used as the basis for building more complex and repetitive components (like blog comments, Tweets, etc). Included is support for left and right aligned content, content alignment options, nesting, and more. The default media allow to float a media object (images, video, audio) to the left or right of a content block. Class | Description ----- | ----------- .media | .media-left | .media-object | .media-heading | .media-body | ### Responsive utilities * `.hidden-[]-down` 如 `.hidden-md-down` 表示 md 时隐藏,然后小于 md 的情形也隐藏 * `.hidden-[]-up` 如 `.hidden-md-up` 表示 md 时隐藏,然后大于 md 的情形也隐藏 ## 内容 Content ### Reboot Bootstrap 在 Normalize 基础上做了增强和调整就成了 Reboot,这部分只有元素选择器,不涉及任何类定义。 ### Typography 排版方面的定义,如标题、文本、列表等的详细定义: * 对各元素的基本定义在 `_reboot.scss` * 增强的各种类定义在 `_type.scss` * 对齐、变形、样式、颜色等额外功能在 `_text.scss`。 ### Codes 定义文件在 `_code.scss` * `` 行内代码 * `
` 代码块,可以添加一个可选类 `.pre-scrollable { max-height: 340px; overflow-y: scroll;}`
* `` 变量
* `` 输入
* `` 输出 sample output from a computer program

### Images

对图形的修改都是通过添加类来实现的,在 V2 版本中曾试图修改 `` 的默认行为为“不超出父元素”,但出现了问题。

* `.img-fluid {max-width: 100%; and height: auto;}`
* `.img-rounded`
* `.img-thumbnail` {padding; background-color; border; border-radius; display: inline-block; max-width: 100%;}
* `.img-circle`

图片的布局没有专门的类,而是采用通用布局类来实现的。   
Align images with the helper float classes or text alignment classes. block-level images can be centered using the `.m-x-auto` margin utility class.

### Tables

因为表格使用过于普遍,所以我们没敢修改 `` 标签的默认行为,因此任何时候都需要添加 `.table` 类来应用样式。

* `.table` { width: 100%; background-color;th,td { padding } ... }
* `.table-inverse` 黑底白字
* `.table-striped` 斑马纹
* `.table-bordered` 显示边框
* `.table-hover` 鼠标滑过时加底色
* `.table-sm` 将 padding 减半
* `.table-active / success / info / warning / danger`
* `.table-responsive` 在小于 768px 的屏幕上开启表格横向滚动
* `.table-reflow` 使行与列对调,除非表格很规整,否则慎用
* `.thead-default` 灰底黑字表头,不加的话没有底色,这个类名取得不怎么好,带有欺骗性
* `.thead-inverse` 黑底白字表头 .thead-inverse th {color: #fff; background-color: #373a3c;}

### Figures

如果要显示一块内容(如图片)且需要带标题,那么请使用 `
` 标签。 The `
` tag specifies self-contained content, like illustrations, diagrams, photos, code listings, etc. While the content of the `
` element is related to the main flow, its position is independent of the main flow, and if removed it should not affect the flow of the document. ```html
a figure.
A caption for the above image.
``` ## 组件 Components ### 警告框 Alerts 警告框组件通过提供一些灵活的预定义消息,为常见的用户动作提供反馈消息。 * `.alert` { padding: 15px; margin-bottom: 1rem; border: 1px solid transparent; border-radius: .25rem;} * `.alert-success / info / warning / danger` * `.alert-link` { font-weight: 700; } * `.alert-heading` { color: inherit; } * `.alert-dismissible { padding-right: 35px; }` 调整关闭标签位置 * `.fade { opacity: 0; transition: opacity .15s linear; }` * `.fade.in { opacity: 1; }` 使用 fade 主要是增加关闭效果,不用不影响动作 ```html

Well done!

Well done! You successfully read this important alert message.
Holy guacamole! You should check in on some of those fields below.
``` ### 路径导航 Breadcrumb 在一个带有层次的导航结构中标明当前页面的位置。各路径间的分隔符会通过 `::before` 自动添加。 * `.breadcrumb` { padding; margin-bottom: 1rem; list-style: none; background-color; border-radius } * `.breadcrumb-item` { float: left; } .breadcrumb-item + .breadcrumb-item::before { content: "/"; } * `.active` .breadcrumb-item.active { color: #818a91; } ```html ``` ### 按钮 Buttons * `.btn` { display: inline-block; ...; vertical-align: middle; cursor: pointer; border; padding; } * `.btn-primary / secondary / success / info / warning / danger` * `.btn-outline-primary / secondary / success / info / warning / danger` * `.btn-link` 去掉底色和边框,只显示跟普通链接一样的样式 * `.btn-lg / sm` .btn-group-lg>.btn, .btn-lg { padding; border-radius; font-size: 1.25rem; } / 0.875rem * `.btn-block` { display: block; width: 100%; } 100%宽,适合小屏 * `.active / :active` 没什么特别的效果,重在语义吧 * `.disabled` 如果是 button 元素,则可以直接添加 disabled 属性,而其他元素则可以使用 .disabled 类。
.btn.disabled, .btn:disabled { cursor: not-allowed; opacity: .65; } ```html Link ``` ### 按钮组 Button group * `.btn-group / .btn-group-vertical` 使按钮紧密地叠在一起,然后再在两侧加圆角,做成一个按钮组 * `.btn-toolbar` 用来将多个 group 组织到一起的,没添加啥特别的效果(往左靠了靠,组间加了点间距),重在结构组织吧 * `.btn-group-lg / sm` 加大 / 缩小 标签组 ```html
``` ### 卡片 Cards V4 版新引入的组件,用于替换原先的 panels, wells, and thumbnails. * `.card` 相对定位 + 带圆角的边框 * `.card-img` 带4个圆角 * `.card-img-top` 左上、右上带圆角 * `.card-img-bottom` 左下、右下带圆角 * `.card-img-overlay` 描述的不是图片,而是将图片作为底色时,上面的叠加层
.card-img-overlay { position: absolute; top: 0; right: 0; bottom: 0; left: 0; padding: 1.25rem; } * `.card-block` { padding: 1.25rem; &::after {.clearfix} } card 内的区块,用于组织内容 * `.card-title` { margin-bottom: .75rem; } 给标题加下边距 * `.card-subtitle` { margin-top: -.375rem; margin-bottom: 0; } 副标题 * `.card-text` .card-text:last-child { margin-bottom: 0; } * `.card-link` .card-link+.card-link { margin-left: 1.25rem; } :hover { text-decoration:none; } * `.card-header` 底色 + 分隔线 * `.card-header-tabs` 为应用 nav-tabs 把边距去掉了 * `.card-header-pills` 为应用 nav-pills 去掉了相应的边距 * `.card-footer` 底色 + 分隔线 * * `.card-inverse` 反色 * `.card-primary / success / info / warning / danger` * `.card-outline-primary / success / info / warning / danger` 无底色版本 * `.card-group` 无间隔多列,但 .card 有边框用于分隔 * `.card-deck-wrapper` { margin-right; margin-left: -1.25rem; } 套在 .card-deck 外层,flex 布局中不需要这个类 * `.card-deck` 甲板,有间隔、变宽的多列 { display: table; width: 100%; border-spacing: 1.25rem 0; ... } * `.card-columns` 采用分栏技术的多列 { column-count: 3; ... } * `.card-blockquote` ```html
Card image

Card title

This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.

Last updated 3 mins ago

...
...
...

Special title treatment

With supporting text below as a natural lead-in to additional content.

Go somewhere
``` ### 图片轮播 Carousel * `.carousel` 不带滑动效果的轮播 * `.carousel.slide` 带左右滑动效果的轮播 * `.carousel-inner` 组件 - 内容 * `.carousel-item` 组件 - 内容 - 单个 slide * `.carousel-caption` 组件 - 内容 - 单个 slide - 说明 * `.carousel-indicators` 组件 - 位置指示器 * `.carousel-control.left / .right` 组件 - 控制按钮,加了浅灰过渡效果,难看 * `.icon-prev / .icon-next` 组件 - 控制按钮 - 箭头 * `.active` 需要给 indicator 和 item 分别添加该类来初始化轮播 API: * `.carousel(options)` * `.carousel('cycle')` * `.carousel('pause')` * `.carousel(number)` * `.carousel('prev')` * `.carousel('next')` 配置项:可以通过 data 属性或 JS代码 来配置轮播,如 `data-interval="8000"` > 注意1:轮播采用的是 CSS3 动画,虽然 IE9 不支持 transition,但 BS 团队并不打算引入 jQuery fallback。 > 注意2:必须要有一个 `.carousel-item.active` 来进行初始化,否则轮播不显示 ```html ``` ### 折叠 Collapse 通过 `data-toggle` 指定折叠按钮,通过 `href` 或 `data-target` 指定控制目标。该组件主要就是 js 代码。 ```html Link with href
Paragraph1

Paragraph2

Collapsible Group Item #1

Anim pariatur cliche reprehenderit, enim eiusmod high life.

Anim accusamus terry richardson ad squid.
``` ### 下拉列表 Dropdowns * `.dropdown` .dropdown, .dropup { position: relative; } * `.dropdown-toggle` 添加向下箭头 * `.dropdown-toggle-split` 采用箭头分离式下拉列表时,减小 padding 使其看起来更紧凑 * `.dropdown-menu` { position: absolute; top: 100%; left: 0; z-index: 1000; display: none; float: left; min-width: 160px; text-align: left; list-style: none; background-color: #fff; ... } * `.dropdown-item` { display: block; width: 100%; clear: both; white-space: nowrap; ... } * `.dropdown-divider` 添加分隔条 ```html
// .btn-group 带了 position: relative 所以可以不用 .dropdown
``` ### 表单 Forms * `.form-inline` 表单类 - 单行布局 .form-inline .form-group { display: inline-block; } * `.form-group` 表单组类 { margin-bottom: 1rem; } 可配合 `
` `
` 使用 * `.form-control` 表单控件类 { width: 100%; border: 1px solid rgba(0,0,0,.15); border-radius: .25rem; } * `.form-control-lg / sm` * `.form-control-static` 静态表单控件,即以文本替换表单控件 `

some text

` * `.input-group` 输入框组 * `.input-group-addon` 作为输入项的附件,放置于前后可分别用于提示或单位显示,具体实现比较复杂 * // 布局类,与 grid 配合使用 * `.col-form-label` 微调标签上下间距 { padding-top: .5rem; padding-bottom: .5rem; } * `.col-form-label-lg / sm` * `.col-form-legend` 左右分布时,将 `` 的字体调小成普通 label 的样式 * // 单选/复选类 * `.form-check` div.form-check 用于组织 单选/多选框 + label,提供整体的显示样式 * `.form-check-label` 与 div.form-check 配合使用 * `.form-check-input` 与 div.form-check 配合使用 * `.form-check-inline` 选项框横向排列,需要去掉 div.form-check 并用其替换 `.form-check-label` * // 表单验证类 * `.has-success / warning / danger` 添加边框效果 `.has-success .form-control { border-color }` * `.form-control-success / warning / danger` 添加图标 `.has-success .form-control-success {background-image}` * `.form-control-feedback` 根据验证样式修改文字颜色 .has-[] .form-control-feedback { color } 注意:所有单选和复选框必须用 label 包裹,这样不仅增大了可点击区域,还使得可以不用借助 js 就能实现一些样式调整。 Bootstrap 还提供了更进一步的定制类型,本笔记暂未收录。 ```html
Example help text that remains unchanged.
Radios
Checkbox
$
.00
``` ### 巨屏 Jumbotron * `jumbotron` { padding: 2rem 1rem; margin-bottom: 2rem; background-color: #eceeef; border-radius: .3rem; } * `jumbotron-fluid` 在 jumbotron 基础上 { padding-right: 0; padding-left: 0; border-radius: 0; } ### 列表 List group * `.list-group` 列表 * `.list-group-item` 列表项 * `.list-group-item.disabled` 将列表项转换成不可点击状态 * `.list-group-item-action` 将列表项转换成可点击的效果 * `.list-group-item-heading` 列表项内容 - 标题 * `.list-group-item-text` 列表项内容 - 文本 ```html ``` ### 模态弹窗 Modal * `.modal` { position: fixed; top right bottom left: 0; z-index: 1050; display: none; overflow: hidden; outline: 0; } * `.fade / .fade.in` 控制隐藏和显示过渡效果,如果不用也能正常工作 * `.modal-dialog` 弹窗 { max-width: [px]; margin: [px] auto; position: relative; } * `.modal-dialog.modal-lg / sm` * `.modal-content` 弹窗 - 内容 * `.modal-header` 弹窗 - 内容 - 标题栏,常包含一个标题和一个关闭按钮 * `.modal-title` 弹窗 - 内容 - 标题栏 - 标题 * `.modal-body` 弹窗 - 内容 - 主题内容区 * `.modal-body > container-fluid` 弹窗 - 内容 - 主题内容区 - 建立一个容器以利用 grid 布局 * `.modal-footer` 弹窗 - 内容 - 尾栏,常包含一些按钮 默认只支持单个弹窗显示,如果需要多弹窗支持,需要自己编写代码; 请将弹窗代码置于顶层,如果放到其他元素里面,可能会影响其外观和行为; 更多的应用细节可能需要 js 代码配合,本笔记暂未涉及。 Modals are streamlined, but flexible, dialog prompts with the minimum required functionality and smart defaults. ```html ``` ### 导航 Navs * `.nav` 导航 { list-style: none; padding-left: 0; margin-bottom: 0;} * `.nav-inline` 导航 - 横向排列,统一 li+li 与 a+a 的风格: .nav-item + .nav-item {display: inline-block;} / .nav-link + .nav-link {margin-left: 1rem;} * `.nav-tabs` 应用 tab 样式 * `.nav-pills` 应用 pill 样式 * `.nav.nav-pills.nav-stacked` tab 和 pill 样式都是横向显示的,如果想变回竖向排列,加 .nav-stacked * `.nav-item` 导航分项 * `.nav-link` { display: inline-block; } * `.nav-link.active` * `.nav-link.disabled` ```html
1...
2...
``` ### 导航条 Navbar 导航条在普通导航的基础上添加了其他元素,主要目的就是用来做顶部导航条的。 * `.navbar` 需要同时再指定一个颜色主题 .navbar { position: relative; padding: .5rem 1rem; border-radius } * `.navbar-dark / light` 颜色主题 * `.navbar-full` 去掉圆角 * `.navbar-fixed-top / bottom` 固定到顶部 / 底部 { position: fixed; right: 0; left: 0; z-index: 1030; border-radius: 0; } + top / bottom: 0; * `.navbar-brand` 内容 - 标记 * `.navbar-nav` 内容 - 导航,使用列表时需要 .nav 配合来去边距和列表符 `