概览

了解 Bootstrap 基础设施的关键部分,包括我们更好的、更快的、更强的 Web 开发方法。

HTML5 doctype

Bootstrap 使用某些 HTML 元素和 CSS 属性,这些需要 HTML5 doctype。请在所有项目开始时包含它。

<!doctype html>
<html lang="zh-CN">
  ...
</html>

移动优先

在 Bootstrap 2 中,我们为框架的关键方面添加了可选的移动友好样式。在 Bootstrap 3 中,我们将项目重写为从一开始就移动友好。不是添加可选的移动样式,而是直接烘焙到核心中。事实上,Bootstrap 是移动优先的。移动优先样式可以在整个库中找到,而不是在单独的文件中。

为确保正确的渲染和触摸缩放,将 viewport meta 标签添加到您的 <head>

<meta name="viewport" content="width=device-width, initial-scale=1">

您可以通过向 viewport meta 标签添加 user-scalable=no 来禁用移动设备上的缩放功能。这将禁用缩放,意味着用户只能滚动,并使您的网站感觉更像本机应用程序。总的来说,我们不建议在每个网站上都这样做,因此请谨慎使用!

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

Bootstrap 设置了基本的全局显示、排版和链接样式。具体来说,我们:

  • body 上设置 background-color: #fff;
  • 使用 @font-family-base@font-size-base@line-height-base 属性作为我们的排版基础
  • 通过 @link-color 设置全局链接颜色,并仅在 :hover 时应用链接下划线

这些样式可以在 scaffolding.less 中找到。

Normalize.css

为了改进跨浏览器渲染,我们使用 Normalize.css,这是一个由 Nicolas GallagherJonathan Neal 开发的项目。

容器

Bootstrap 需要一个包含元素来包装网站内容并容纳我们的网格系统。您可以在项目中选择使用以下两种容器之一。请注意,由于 padding 等原因,两个容器都不能嵌套。

使用 .container 作为响应式固定宽度容器。

<div class="container">
  ...
</div>

使用 .container-fluid 作为全宽容器,跨越视口的整个宽度。

<div class="container-fluid">
  ...
</div>

网格系统

Bootstrap 包含一个响应式的、移动优先的流体网格系统,随着设备或视口大小的增加,它会适当地扩展到 12 列。它包括预定义类,用于简单的布局选项,以及强大的mixin 用于生成更具语义的布局

简介

网格系统用于通过一系列行和列来创建页面布局,这些行和列容纳您的内容。Bootstrap 网格系统的工作方式如下:

  • 行必须放置在 .container(固定宽度)或 .container-fluid(全宽)中,以进行适当的对齐和填充。
  • 使用行来创建列的水平组。
  • 内容应放置在列中,只有列可以是行的直接子元素。
  • 预定义的网格类如 .row.col-xs-4 可用于快速制作网格布局。Less mixin 也可用于更具语义的布局。
  • 列通过 padding 创建间距(列内容之间的间隙)。该填充通过 .row 上的负边距在第一列和最后一列中偏移。
  • 负边距是以下示例缩进的原因。这是为了使网格列中的内容与非网格内容对齐。
  • 通过指定要跨越的 12 个可用列中的列数来创建网格列。例如,三个相等的列将使用三个 .col-xs-4
  • 如果在一行中放置超过 12 列,每组额外的列将作为一个单元换行到新行。
  • 网格类适用于屏幕宽度大于或等于断点大小的设备,并覆盖针对较小设备的网格类。因此,例如,将任何 .col-md-* 类应用于元素不仅会影响其在中等设备上的样式,而且如果没有 .col-lg-* 类,也会影响其在大型设备上的样式。

查看示例以了解如何将这些原则应用到您的代码中。

媒体查询

我们在 Less 文件中使用以下媒体查询来创建网格系统中的关键断点。

/* 超小设备(手机,小于 768px) */
/* 没有媒体查询,因为这是 Bootstrap 中的默认值 */

/* 小设备(平板,768px 及以上) */
@media (min-width: @screen-sm-min) { ... }

/* 中等设备(桌面,992px 及以上) */
@media (min-width: @screen-md-min) { ... }

/* 大型设备(大桌面,1200px 及以上) */
@media (min-width: @screen-lg-min) { ... }

我们偶尔会扩展这些媒体查询以包含 max-width,以将 CSS 限制在较窄的设备集上。

@media (max-width: @screen-xs-max) { ... }
@media (min-width: @screen-sm-min) and (max-width: @screen-sm-max) { ... }
@media (min-width: @screen-md-min) and (max-width: @screen-md-max) { ... }
@media (min-width: @screen-lg-min) { ... }

网格选项

通过方便的表格了解 Bootstrap 网格系统的各个方面如何在多个设备上工作。

超小设备 手机 (<768px) 小设备 平板 (≥768px) 中等设备 桌面 (≥992px) 大型设备 桌面 (≥1200px)
网格行为 始终水平 开始时折叠,断点以上水平
容器宽度 无(自动) 750px 970px 1170px
类前缀 .col-xs- .col-sm- .col-md- .col-lg-
列数 12
列宽 自动 ~62px ~81px ~97px
间距宽度 30px(每列两侧各 15px)
可嵌套
偏移
列排序

示例:堆叠到水平

使用一组 .col-md-* 网格类,您可以创建一个基本的网格系统,在移动设备和平板设备(超小范围到小范围)上开始时堆叠,然后在桌面(中等)设备上变为水平。将网格列放在任何 .row 中。

.col-md-1
.col-md-1
.col-md-1
.col-md-1
.col-md-1
.col-md-1
.col-md-1
.col-md-1
.col-md-1
.col-md-1
.col-md-1
.col-md-1
.col-md-8
.col-md-4
.col-md-4
.col-md-4
.col-md-4
.col-md-6
.col-md-6
<div class="row">
  <div class="col-md-1">.col-md-1</div>
  <div class="col-md-1">.col-md-1</div>
  <div class="col-md-1">.col-md-1</div>
  <div class="col-md-1">.col-md-1</div>
  <div class="col-md-1">.col-md-1</div>
  <div class="col-md-1">.col-md-1</div>
  <div class="col-md-1">.col-md-1</div>
  <div class="col-md-1">.col-md-1</div>
  <div class="col-md-1">.col-md-1</div>
  <div class="col-md-1">.col-md-1</div>
  <div class="col-md-1">.col-md-1</div>
  <div class="col-md-1">.col-md-1</div>
</div>
<div class="row">
  <div class="col-md-8">.col-md-8</div>
  <div class="col-md-4">.col-md-4</div>
</div>
<div class="row">
  <div class="col-md-4">.col-md-4</div>
  <div class="col-md-4">.col-md-4</div>
  <div class="col-md-4">.col-md-4</div>
</div>
<div class="row">
  <div class="col-md-6">.col-md-6</div>
  <div class="col-md-6">.col-md-6</div>
</div>

示例:流体容器

通过将最外层的 .container 更改为 .container-fluid,将任何固定宽度的网格布局转换为全宽布局。

<div class="container-fluid">
  <div class="row">
    ...
  </div>
</div>

示例:手机和桌面

不希望您的列在较小的设备上简单地堆叠吗?通过向您的列添加 .col-xs-* .col-md-* 来使用超小和中等设备网格类。请参阅下面的示例以更好地了解它的工作原理。

.col-xs-12 .col-md-8
.col-xs-6 .col-md-4
.col-xs-6 .col-md-4
.col-xs-6 .col-md-4
.col-xs-6 .col-md-4
.col-xs-6
.col-xs-6
<!-- 在移动设备上堆叠列,使一个全宽,另一个半宽 -->
<div class="row">
  <div class="col-xs-12 col-md-8">.col-xs-12 .col-md-8</div>
  <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
</div>

<!-- 列在移动设备上从 50% 宽开始,在桌面上增加到 33.3% 宽 -->
<div class="row">
  <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
  <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
  <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
</div>

<!-- 列始终为 50% 宽,在移动设备和桌面上 -->
<div class="row">
  <div class="col-xs-6">.col-xs-6</div>
  <div class="col-xs-6">.col-xs-6</div>
</div>

示例:手机、平板、桌面

通过使用平板 .col-sm-* 类创建更加动态和强大的布局,在前一个示例的基础上构建。

.col-xs-12 .col-sm-6 .col-md-8
.col-xs-6 .col-md-4
.col-xs-6 .col-sm-4
.col-xs-6 .col-sm-4
.col-xs-6 .col-sm-4
<div class="row">
  <div class="col-xs-12 col-sm-6 col-md-8">.col-xs-12 .col-sm-6 .col-md-8</div>
  <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
</div>
<div class="row">
  <div class="col-xs-6 col-sm-4">.col-xs-6 .col-sm-4</div>
  <div class="col-xs-6 col-sm-4">.col-xs-6 .col-sm-4</div>
  <!-- 可选:如果 XS 列的内容高度不匹配,则清除 XS 列 -->
  <div class="clearfix visible-xs-block"></div>
  <div class="col-xs-6 col-sm-4">.col-xs-6 .col-sm-4</div>
</div>

示例:列换行

如果在一行中放置超过 12 列,每组额外的列将作为一个单元换行到新行。

.col-xs-9
.col-xs-4
因为 9 + 4 = 13 > 12,这个 4 列宽的 div 作为一个连续单元换行到新行。
.col-xs-6
后续列继续沿新行排列。
<div class="row">
  <div class="col-xs-9">.col-xs-9</div>
  <div class="col-xs-4">.col-xs-4<br>因为 9 + 4 = 13 &gt; 12,这个 4 列宽的 div 作为一个连续单元换行到新行。</div>
  <div class="col-xs-6">.col-xs-6<br>后续列继续沿新行排列。</div>
</div>

响应式列重置

有了四个可用的网格层,您必然会在某些断点处遇到问题,您的列无法正确清除,因为一个比另一个高。要解决这个问题,请使用 .clearfix 和我们的响应式工具类的组合。

.col-xs-6 .col-sm-3
调整视口大小或在手机上查看示例。
.col-xs-6 .col-sm-3
.col-xs-6 .col-sm-3
.col-xs-6 .col-sm-3
<div class="row">
  <div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
  <div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>

  <!-- 仅为所需的视口添加额外的 clearfix -->
  <div class="clearfix visible-xs-block"></div>

  <div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
  <div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
</div>

除了在响应式断点处清除列之外,您可能还需要重置偏移、推送或拉取。请在网格示例中查看实际操作。

<div class="row">
  <div class="col-sm-5 col-md-6">.col-sm-5 .col-md-6</div>
  <div class="col-sm-5 col-sm-offset-2 col-md-6 col-md-offset-0">.col-sm-5 .col-sm-offset-2 .col-md-6 .col-md-offset-0</div>
</div>

<div class="row">
  <div class="col-sm-6 col-md-5 col-lg-6">.col-sm-6 .col-md-5 .col-lg-6</div>
  <div class="col-sm-6 col-md-5 col-md-offset-2 col-lg-6 col-lg-offset-0">.col-sm-6 .col-md-5 .col-md-offset-2 .col-lg-6 .col-lg-offset-0</div>
</div>

移除间距

使用 .row-no-gutters 类从一行及其列中移除间距。

.col-xs-12 .col-md-8
.col-xs-6 .col-md-4
.col-xs-6 .col-md-4
.col-xs-6 .col-md-4
.col-xs-6 .col-md-4
.col-xs-6
.col-xs-6
<div class="row row-no-gutters">
  <div class="col-xs-12 col-md-8">.col-xs-12 .col-md-8</div>
  <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
</div>
<div class="row row-no-gutters">
  <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
  <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
  <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
</div>
<div class="row row-no-gutters">
  <div class="col-xs-6">.col-xs-6</div>
  <div class="col-xs-6">.col-xs-6</div>
</div>

列偏移

使用 .col-md-offset-* 类将列向右移动。这些类通过 * 列增加列的左边距。例如,.col-md-offset-4.col-md-4 移动四列。

.col-md-4
.col-md-4 .col-md-offset-4
.col-md-3 .col-md-offset-3
.col-md-3 .col-md-offset-3
.col-md-6 .col-md-offset-3
<div class="row">
  <div class="col-md-4">.col-md-4</div>
  <div class="col-md-4 col-md-offset-4">.col-md-4 .col-md-offset-4</div>
</div>
<div class="row">
  <div class="col-md-3 col-md-offset-3">.col-md-3 .col-md-offset-3</div>
  <div class="col-md-3 col-md-offset-3">.col-md-3 .col-md-offset-3</div>
</div>
<div class="row">
  <div class="col-md-6 col-md-offset-3">.col-md-6 .col-md-offset-3</div>
</div>

您还可以使用 .col-*-offset-0 类覆盖较低网格层的偏移。

<div class="row">
  <div class="col-xs-6 col-sm-4">
  </div>
  <div class="col-xs-6 col-sm-4">
  </div>
  <div class="col-xs-6 col-xs-offset-3 col-sm-4 col-sm-offset-0">
  </div>
</div>

嵌套列

要将内容与默认网格嵌套,请在现有的 .col-sm-* 列中添加一个新的 .row 和一组 .col-sm-* 列。嵌套行应包含一组加起来为 12 或更少的列(不要求您使用所有 12 个可用列)。

第 1 层:.col-sm-9
第 2 层:.col-xs-8 .col-sm-6
第 2 层:.col-xs-4 .col-sm-6
<div class="row">
  <div class="col-sm-9">
    第 1 层:.col-sm-9
    <div class="row">
      <div class="col-xs-8 col-sm-6">
        第 2 层:.col-xs-8 .col-sm-6
      </div>
      <div class="col-xs-4 col-sm-6">
        第 2 层:.col-xs-4 .col-sm-6
      </div>
    </div>
  </div>
</div>

列排序

使用 .col-md-push-*.col-md-pull-* 修饰符类轻松更改内置网格列的顺序。

.col-md-9 .col-md-push-3
.col-md-3 .col-md-pull-9
<div class="row">
  <div class="col-md-9 col-md-push-3">.col-md-9 .col-md-push-3</div>
  <div class="col-md-3 col-md-pull-9">.col-md-3 .col-md-pull-9</div>
</div>

Less mixin 和变量

除了用于快速布局的预构建网格类之外,Bootstrap 还包括 Less 变量和 mixin,用于快速生成您自己的简单、语义布局。

变量

变量确定列数、间距宽度和开始浮动列的媒体查询点。我们使用这些来生成上面记录的预定义网格类,以及下面列出的自定义 mixin。

@grid-columns:              12;
@grid-gutter-width:         30px;
@grid-float-breakpoint:     768px;

Mixin

Mixin 与网格变量结合使用,为单个网格列生成语义 CSS。

// 创建一系列列的包装器
.make-row(@gutter: @grid-gutter-width) {
  // 然后清除浮动列
  .clearfix();

  @media (min-width: @screen-sm-min) {
    margin-left:  (@gutter / -2);
    margin-right: (@gutter / -2);
  }

  // 负边距嵌套行以对齐列的内容
  .row {
    margin-left:  (@gutter / -2);
    margin-right: (@gutter / -2);
  }
}

// 生成超小列
.make-xs-column(@columns; @gutter: @grid-gutter-width) {
  position: relative;
  // 防止空列时折叠
  min-height: 1px;
  // 通过 padding 实现内部间距
  padding-left:  (@gutter / 2);
  padding-right: (@gutter / 2);

  // 根据可用列数计算宽度
  @media (min-width: @grid-float-breakpoint) {
    float: left;
    width: percentage((@columns / @grid-columns));
  }
}

// 生成小列
.make-sm-column(@columns; @gutter: @grid-gutter-width) {
  position: relative;
  // 防止空列时折叠
  min-height: 1px;
  // 通过 padding 实现内部间距
  padding-left:  (@gutter / 2);
  padding-right: (@gutter / 2);

  // 根据可用列数计算宽度
  @media (min-width: @screen-sm-min) {
    float: left;
    width: percentage((@columns / @grid-columns));
  }
}

// 生成小列偏移
.make-sm-column-offset(@columns) {
  @media (min-width: @screen-sm-min) {
    margin-left: percentage((@columns / @grid-columns));
  }
}
.make-sm-column-push(@columns) {
  @media (min-width: @screen-sm-min) {
    left: percentage((@columns / @grid-columns));
  }
}
.make-sm-column-pull(@columns) {
  @media (min-width: @screen-sm-min) {
    right: percentage((@columns / @grid-columns));
  }
}

// 生成中等列
.make-md-column(@columns; @gutter: @grid-gutter-width) {
  position: relative;
  // 防止空列时折叠
  min-height: 1px;
  // 通过 padding 实现内部间距
  padding-left:  (@gutter / 2);
  padding-right: (@gutter / 2);

  // 根据可用列数计算宽度
  @media (min-width: @screen-md-min) {
    float: left;
    width: percentage((@columns / @grid-columns));
  }
}

// 生成中等列偏移
.make-md-column-offset(@columns) {
  @media (min-width: @screen-md-min) {
    margin-left: percentage((@columns / @grid-columns));
  }
}
.make-md-column-push(@columns) {
  @media (min-width: @screen-md-min) {
    left: percentage((@columns / @grid-columns));
  }
}
.make-md-column-pull(@columns) {
  @media (min-width: @screen-md-min) {
    right: percentage((@columns / @grid-columns));
  }
}

// 生成大列
.make-lg-column(@columns; @gutter: @grid-gutter-width) {
  position: relative;
  // 防止空列时折叠
  min-height: 1px;
  // 通过 padding 实现内部间距
  padding-left:  (@gutter / 2);
  padding-right: (@gutter / 2);

  // 根据可用列数计算宽度
  @media (min-width: @screen-lg-min) {
    float: left;
    width: percentage((@columns / @grid-columns));
  }
}

// 生成大列偏移
.make-lg-column-offset(@columns) {
  @media (min-width: @screen-lg-min) {
    margin-left: percentage((@columns / @grid-columns));
  }
}
.make-lg-column-push(@columns) {
  @media (min-width: @screen-lg-min) {
    left: percentage((@columns / @grid-columns));
  }
}
.make-lg-column-pull(@columns) {
  @media (min-width: @screen-lg-min) {
    right: percentage((@columns / @grid-columns));
  }
}

使用示例

您可以将变量修改为您自己的自定义值,或者只使用带有默认值的 mixin。以下是使用默认设置创建带有间隙的两列布局的示例。

.wrapper {
  .make-row();
}
.content-main {
  .make-lg-column(8);
}
.content-secondary {
  .make-lg-column(3);
  .make-lg-column-offset(1);
}
<div class="wrapper">
  <div class="content-main">...</div>
  <div class="content-secondary">...</div>
</div>

排版

标题

所有的 HTML 标题标签,从 <h1><h6>,均可使用。另外,还提供了 .h1.h6 类,用于当你想匹配标题的字体样式但希望文本以行内元素显示时。

h1. Bootstrap heading

Semibold 36px

h2. Bootstrap heading

Semibold 30px

h3. Bootstrap heading

Semibold 24px

h4. Bootstrap heading

Semibold 18px
h5. Bootstrap heading
Semibold 14px
h6. Bootstrap heading
Semibold 12px
<h1>h1. Bootstrap heading</h1>
<h2>h2. Bootstrap heading</h2>
<h3>h3. Bootstrap heading</h3>
<h4>h4. Bootstrap heading</h4>
<h5>h5. Bootstrap heading</h5>
<h6>h6. Bootstrap heading</h6>

使用通用的 <small> 标签或 .small 类,可以在任何标题中创建更轻、次要的文本。

h1. Bootstrap heading Secondary text

h2. Bootstrap heading Secondary text

h3. Bootstrap heading Secondary text

h4. Bootstrap heading Secondary text

h5. Bootstrap heading Secondary text
h6. Bootstrap heading Secondary text
<h1>h1. Bootstrap heading <small>Secondary text</small></h1>
<h2>h2. Bootstrap heading <small>Secondary text</small></h2>
<h3>h3. Bootstrap heading <small>Secondary text</small></h3>
<h4>h4. Bootstrap heading <small>Secondary text</small></h4>
<h5>h5. Bootstrap heading <small>Secondary text</small></h5>
<h6>h6. Bootstrap heading <small>Secondary text</small></h6>

正文

Bootstrap 的全局默认 font-size14px,line-height1.428。这适用于 <body> 和所有段落。此外,<p>(段落)默认底部边距为其计算行高的一半(默认为 10px)。

Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam id dolor id nibh ultricies vehicula.

Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec ullamcorper nulla non metus auctor fringilla. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Donec ullamcorper nulla non metus auctor fringilla.

Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.

<p>...</p>

Lead body copy

通过添加 .lead 使段落更加突出。

Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Duis mollis, est non commodo luctus.

<p class="lead">...</p>

基于 Less 构建

排版比例基于 variables.less 中的两个 Less 变量:@font-size-base@line-height-base。第一个是全局使用的基本字号,第二个是基本行高。我们使用这些变量和一些简单的数学计算来创建所有类型的边距、内边距和行高。自定义它们,Bootstrap 会相应调整。

行内文本元素

标记文本

为了在另一个上下文中突出显示一段文本的相关性,请使用 <mark> 标签。

You can use the mark tag to highlight text.

You can use the mark tag to <mark>highlight</mark> text.

删除文本

要指示已删除的文本块,请使用 <del> 标签。

This line of text is meant to be treated as deleted text.

<del>This line of text is meant to be treated as deleted text.</del>

删除线文本

要指示不再相关的文本块,请使用 <s> 标签。

This line of text is meant to be treated as no longer accurate.

<s>This line of text is meant to be treated as no longer accurate.</s>

插入文本

要指示文档的添加内容,请使用 <ins> 标签。

This line of text is meant to be treated as an addition to the document.

<ins>This line of text is meant to be treated as an addition to the document.</ins>

下划线文本

要下划线文本,请使用 <u> 标签。

This line of text will render as underlined

<u>This line of text will render as underlined</u>

使用 HTML 的默认强调标签并应用轻量级样式。

小文本

为了淡化行内或文本块,使用 <small> 标签将文本设置为父元素大小的 85%。标题元素接收它们自己嵌套 <small> 元素的 font-size

你也可以使用带有 .small 的替代行内元素代替任何 <small>

This line of text is meant to be treated as fine print.

<small>This line of text is meant to be treated as fine print.</small>

粗体

用于通过更重的字重强调一段文本。

The following snippet of text is rendered as bold text.

<strong>rendered as bold text</strong>

斜体

用于通过斜体强调一段文本。

The following snippet of text is rendered as italicized text.

<em>rendered as italicized text</em>

替代元素

在 HTML5 中随意使用 <b><i><b> 用于突出显示词语或短语而不传达额外的重要性,而 <i> 主要用于语音、技术术语等。

对齐类

使用文本对齐类轻松重新对齐文本与组件。

Left aligned text.

Center aligned text.

Right aligned text.

Justified text.

No wrap text.

<p class="text-left">Left aligned text.</p>
<p class="text-center">Center aligned text.</p>
<p class="text-right">Right aligned text.</p>
<p class="text-justify">Justified text.</p>
<p class="text-nowrap">No wrap text.</p>

变换类

使用文本大小写类转换组件中的文本。

Lowercased text.

Uppercased text.

Capitalized text.

<p class="text-lowercase">Lowercased text.</p>
<p class="text-uppercase">Uppercased text.</p>
<p class="text-capitalize">Capitalized text.</p>

缩写

HTML <abbr> 元素用于缩写和首字母缩略词的样式化实现,在悬停时显示扩展版本。带有 title 属性的缩写具有浅点状底部边框,悬停时有帮助光标,为悬停和辅助技术用户提供额外的上下文。

基本缩写

An abbreviation of the word attribute is attr.

<abbr title="attribute">attr</abbr>

首字母缩略词

在缩写词上添加 .initialism 以获得稍小的字号。

HTML is the best thing since sliced bread.

<abbr title="HyperText Markup Language" class="initialism">HTML</abbr>

地址

呈现最近祖先或整个作品的联系信息。通过在所有行末尾添加 <br> 来保留格式。

Twitter, Inc.
1355 Market Street, Suite 900
San Francisco, CA 94103
P: (123) 456-7890
Full Name
first.last@example.com
<address>
  <strong>Twitter, Inc.</strong><br>
  1355 Market Street, Suite 900<br>
  San Francisco, CA 94103<br>
  <abbr title="Phone">P:</abbr> (123) 456-7890
</address>

<address>
  <strong>Full Name</strong><br>
  <a href="mailto:#">first.last@example.com</a>
</address>

引用

用于在文档中引用另一个来源的内容块。

默认引用

<blockquote> 包裹在作为引用的任何 HTML 周围。对于直接引用,我们建议使用 <p>

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.

<blockquote>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.</p>
</blockquote>

引用选项

标准 <blockquote> 的简单变体的样式和内容更改。

命名来源

添加 <footer> 用于标识来源。将来源作品的名称包裹在 <cite> 中。

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.

Someone famous in Source Title
<blockquote>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.</p>
  <footer>Someone famous in <cite title="Source Title">Source Title</cite></footer>
</blockquote>

替代显示

添加 .blockquote-reverse 以获得右对齐内容的引用。

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.

Someone famous in Source Title
<blockquote class="blockquote-reverse">
  ...
</blockquote>

列表

无序列表

顺序 明确重要的项目列表。

  • Lorem ipsum dolor sit amet
  • Consectetur adipiscing elit
  • Integer molestie lorem at massa
  • Facilisis in pretium nisl aliquet
  • Nulla volutpat aliquam velit
    • Phasellus iaculis neque
    • Purus sodales ultricies
    • Vestibulum laoreet porttitor sem
    • Ac tristique libero volutpat at
  • Faucibus porta lacus fringilla vel
  • Aenean sit amet erat nunc
  • Eget porttitor lorem
<ul>
  <li>...</li>
</ul>

有序列表

顺序 明确 重要的项目列表。

  1. Lorem ipsum dolor sit amet
  2. Consectetur adipiscing elit
  3. Integer molestie lorem at massa
  4. Facilisis in pretium nisl aliquet
  5. Nulla volutpat aliquam velit
  6. Faucibus porta lacus fringilla vel
  7. Aenean sit amet erat nunc
  8. Eget porttitor lorem
<ol>
  <li>...</li>
</ol>

无样式列表

移除列表项上的默认 list-style 和左边距(仅直接子项)。这只适用于直接子列表项,意味着你也需要对任何嵌套列表添加此类。

  • Lorem ipsum dolor sit amet
  • Consectetur adipiscing elit
  • Integer molestie lorem at massa
  • Facilisis in pretium nisl aliquet
  • Nulla volutpat aliquam velit
    • Phasellus iaculis neque
    • Purus sodales ultricies
    • Vestibulum laoreet porttitor sem
    • Ac tristique libero volutpat at
  • Faucibus porta lacus fringilla vel
  • Aenean sit amet erat nunc
  • Eget porttitor lorem
<ul class="list-unstyled">
  <li>...</li>
</ul>

内联列表

使用 display: inline-block; 和一些轻量级内边距将所有列表项放在单行上。

  • Lorem ipsum
  • Phasellus iaculis
  • Nulla volutpat
<ul class="list-inline">
  <li>...</li>
</ul>

描述列表

带有相关描述的术语列表。

Description lists
A description list is perfect for defining terms.
Euismod
Vestibulum id ligula porta felis euismod semper eget lacinia odio sem nec elit.
Donec id elit non mi porta gravida at eget metus.
Malesuada porta
Etiam porta sem malesuada magna mollis euismod.
<dl>
  <dt>...</dt>
  <dd>...</dd>
</dl>

水平描述

使 <dl> 中的术语和描述并排对齐。开始时像默认 <dl> 一样堆叠,但当导航栏展开时,这些也会展开。

Description lists
A description list is perfect for defining terms.
Euismod
Vestibulum id ligula porta felis euismod semper eget lacinia odio sem nec elit.
Donec id elit non mi porta gravida at eget metus.
Malesuada porta
Etiam porta sem malesuada magna mollis euismod.
Felis euismod semper eget lacinia
Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.
<dl class="dl-horizontal">
  <dt>...</dt>
  <dd>...</dd>
</dl>

自动截断

水平描述列表将使用 text-overflow 截断左列中太长的术语。在较窄的视口中,它们将更改为默认的堆叠布局。

代码

内联代码

使用 <code> 包裹内联代码片段。

例如,<section> 应该作为内联代码包裹。
For example, <code>&lt;section&gt;</code> should be wrapped as inline.

用户输入

使用 <kbd> 表示通常通过键盘输入的内容。

要切换目录,输入 cd 后跟目录名称。
要编辑设置,按 ctrl + ,
To switch directories, type <kbd>cd</kbd> followed by the name of the directory.<br>
To edit settings, press <kbd><kbd>ctrl</kbd> + <kbd>,</kbd></kbd>

基本块

使用 <pre> 表示多行代码。请确保转义代码中的任何尖括号以正确渲染。

<p>Sample text here...</p>
<pre>&lt;p&gt;Sample text here...&lt;/p&gt;</pre>

您可以选择添加 .pre-scrollable 类,这将设置最大高度为 350px 并提供 y 轴滚动条。

变量

使用 <var> 标签表示变量。

y = mx + b

<var>y</var> = <var>m</var><var>x</var> + <var>b</var>

示例输出

使用 <samp> 标签表示程序的示例输出块。

此文本应被视为计算机程序的示例输出。

<samp>This text is meant to be treated as sample output from a computer program.</samp>

表格

基本示例

对于基本样式——轻量内边距和仅水平分隔线——将基本类 .table 添加到任何 <table>。这可能看起来有些多余,但考虑到表格在其他插件(如日历和日期选择器)中的广泛使用,我们选择隔离我们的自定义表格样式。

Optional table caption.
# First Name Last Name Username
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 Larry the Bird @twitter
<table class="table">
  ...
</table>

条纹行

使用 .table-striped<tbody> 中的任何表格行添加斑马条纹。

跨浏览器兼容性

条纹表格通过 :nth-child CSS 选择器进行样式设置,这在 Internet Explorer 8 中不可用。

# First Name Last Name Username
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 Larry the Bird @twitter
<table class="table table-striped">
  ...
</table>

边框表格

添加 .table-bordered 为表格和单元格的所有边添加边框。

# First Name Last Name Username
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 Larry the Bird @twitter
<table class="table table-bordered">
  ...
</table>

悬停行

添加 .table-hover 以在 <tbody> 内的表格行上启用悬停状态。

# First Name Last Name Username
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 Larry the Bird @twitter
<table class="table table-hover">
  ...
</table>

紧凑型表格

添加 .table-condensed 通过将单元格内边距减半来使表格更加紧凑。

# First Name Last Name Username
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 Larry the Bird @twitter
<table class="table table-condensed">
  ...
</table>

情境类

使用情境类为表格行或单个单元格着色。

Class Description
.active 将悬停颜色应用于特定行或单元格
.success 表示成功或积极的操作
.info 表示中性信息性更改或操作
.warning 表示可能需要关注的警告
.danger 表示危险或潜在负面操作
# Column heading Column heading Column heading
1 Column content Column content Column content
2 Column content Column content Column content
3 Column content Column content Column content
4 Column content Column content Column content
5 Column content Column content Column content
6 Column content Column content Column content
7 Column content Column content Column content
8 Column content Column content Column content
9 Column content Column content Column content
<!-- On rows -->
<tr class="active">...</tr>
<tr class="success">...</tr>
<tr class="warning">...</tr>
<tr class="danger">...</tr>
<tr class="info">...</tr>

<!-- On cells (`td` or `th`) -->
<tr>
  <td class="active">...</td>
  <td class="success">...</td>
  <td class="warning">...</td>
  <td class="danger">...</td>
  <td class="info">...</td>
</tr>

向辅助技术传达意义

仅使用颜色为表格行或单个单元格添加意义只提供视觉指示,不会传达给使用辅助技术的用户——例如屏幕阅读器。确保颜色表示的信息从内容本身(相关表格行/单元格中的可见文本)中显而易见,或通过替代方式包含,例如使用 .sr-only 类隐藏的附加文本。

响应式表格

通过将任何 .table 包裹在 .table-responsive 中来创建响应式表格,使其在小设备(低于 768px)上水平滚动。在超过 768px 宽的任何设备上查看时,您不会看到这些表格的任何差异。

垂直裁剪/截断

响应式表格使用 overflow-y: hidden,这会剪掉超出表格底部或顶部边缘的任何内容。特别是,这可能会剪掉下拉菜单和其他第三方小部件。

Firefox 和 fieldsets

Firefox 有一些涉及 width 的尴尬 fieldset 样式,会干扰响应式表格。这无法被覆盖,除非使用我们在 Bootstrap 中提供的 Firefox 特定 hack:

@-moz-document url-prefix() {
  fieldset { display: table-cell; }
}

更多信息,请阅读这个 Stack Overflow 答案

# Table heading Table heading Table heading Table heading Table heading Table heading
1 Table cell Table cell Table cell Table cell Table cell Table cell
2 Table cell Table cell Table cell Table cell Table cell Table cell
3 Table cell Table cell Table cell Table cell Table cell Table cell
# Table heading Table heading Table heading Table heading Table heading Table heading
1 Table cell Table cell Table cell Table cell Table cell Table cell
2 Table cell Table cell Table cell Table cell Table cell Table cell
3 Table cell Table cell Table cell Table cell Table cell Table cell
<div class="table-responsive">
  <table class="table">
    ...
  </table>
</div>

表单

基本示例

单独的表单控件会自动接收一些全局样式。所有带 .form-control 的文本 <input><textarea><select> 元素默认设置为 width: 100%;。将标签和控件包裹在 .form-group 中以获得最佳间距。

Example block-level help text here.

<form>
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
  </div>
  <div class="form-group">
    <label for="exampleInputFile">File input</label>
    <input type="file" id="exampleInputFile">
    <p class="help-block">Example block-level help text here.</p>
  </div>
  <div class="checkbox">
    <label>
      <input type="checkbox"> Check me out
    </label>
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
</form>

不要将表单组与输入组混合

不要直接将表单组与 输入组 混合。相反,将输入组嵌套在表单组内。

内联表单

在你的表单(不必是 <form>)上添加 .form-inline 以获得左对齐和内联块级控件。这仅适用于视口至少为 768px 宽的表单。

可能需要自定义宽度

输入框和选择框在 Bootstrap 中默认应用了 width: 100%;。在内联表单中,我们将其重置为 width: auto;,以便多个控件可以位于同一行。根据你的布局,可能需要额外的自定义宽度。

始终添加标签

如果你不为每个输入框包含标签,屏幕阅读器将难以理解你的表单。对于这些内联表单,你可以使用 .sr-only 类隐藏标签。还有其他为辅助技术提供标签的替代方法,例如 aria-labelaria-labelledbytitle 属性。如果这些都不存在,屏幕阅读器可能会使用 placeholder 属性(如果存在),但请注意,不建议使用 placeholder 替代其他标签方法。

<form class="form-inline">
  <div class="form-group">
    <label for="exampleInputName2">Name</label>
    <input type="text" class="form-control" id="exampleInputName2" placeholder="Jane Doe">
  </div>
  <div class="form-group">
    <label for="exampleInputEmail2">Email</label>
    <input type="email" class="form-control" id="exampleInputEmail2" placeholder="jane.doe@example.com">
  </div>
  <button type="submit" class="btn btn-default">Send invitation</button>
</form>
<form class="form-inline">
  <div class="form-group">
    <label class="sr-only" for="exampleInputEmail3">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail3" placeholder="Email">
  </div>
  <div class="form-group">
    <label class="sr-only" for="exampleInputPassword3">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword3" placeholder="Password">
  </div>
  <div class="checkbox">
    <label>
      <input type="checkbox"> Remember me
    </label>
  </div>
  <button type="submit" class="btn btn-default">Sign in</button>
</form>
$
.00
<form class="form-inline">
  <div class="form-group">
    <label class="sr-only" for="exampleInputAmount">Amount (in dollars)</label>
    <div class="input-group">
      <div class="input-group-addon">$</div>
      <input type="text" class="form-control" id="exampleInputAmount" placeholder="Amount">
      <div class="input-group-addon">.00</div>
    </div>
  </div>
  <button type="submit" class="btn btn-primary">Transfer cash</button>
</form>

水平表单

使用 Bootstrap 的预定义网格类,通过向表单(不必是 <form>)添加 .form-horizontal 来在水平布局中对齐标签和表单控件组。这样做会将 .form-group 更改为表现为网格行,因此不需要 .row

<form class="form-horizontal">
  <div class="form-group">
    <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="inputEmail3" placeholder="Email">
    </div>
  </div>
  <div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Password</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="inputPassword3" placeholder="Password">
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <div class="checkbox">
        <label>
          <input type="checkbox"> Remember me
        </label>
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-default">Sign in</button>
    </div>
  </div>
</form>

支持的控件

示例表单布局中支持的标准表单控件示例。

输入框

最常见的表单控件,基于文本的输入字段。包括对所有 HTML5 类型的支持:textpassworddatetimedatetime-localdatemonthtimeweeknumberemailurlsearchtelcolor

需要类型声明

只有正确声明了 type,输入框才会被完全样式化。

<input type="text" class="form-control" placeholder="Text input">

输入组

要在任何基于文本的 <input> 之前和/或之后添加集成文本或按钮,查看输入组组件

文本域

支持多行文本的表单控件。根据需要更改 rows 属性。

<textarea class="form-control" rows="3"></textarea>

复选框和单选按钮

复选框用于在列表中选择一个或多个选项,而单选按钮用于从多个选项中选择一个。

支持禁用复选框和单选按钮,但要在父级 <label> 悬停时提供"不允许"光标,你需要向父级 .radio.radio-inline.checkbox.checkbox-inline 添加 .disabled 类。

默认(堆叠)


<div class="checkbox">
  <label>
    <input type="checkbox" value="">
    Option one is this and that&mdash;be sure to include why it's great
  </label>
</div>
<div class="checkbox disabled">
  <label>
    <input type="checkbox" value="" disabled>
    Option two is disabled
  </label>
</div>

<div class="radio">
  <label>
    <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
    Option one is this and that&mdash;be sure to include why it's great
  </label>
</div>
<div class="radio">
  <label>
    <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2">
    Option two can be something else and selecting it will deselect option one
  </label>
</div>
<div class="radio disabled">
  <label>
    <input type="radio" name="optionsRadios" id="optionsRadios3" value="option3" disabled>
    Option three is disabled
  </label>
</div>

内联复选框和单选按钮

在一系列复选框或单选按钮上使用 .checkbox-inline.radio-inline 类,使控件显示在同一行上。


<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox1" value="option1"> 1
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox2" value="option2"> 2
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox3" value="option3"> 3
</label>

<label class="radio-inline">
  <input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1"> 1
</label>
<label class="radio-inline">
  <input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2"> 2
</label>
<label class="radio-inline">
  <input type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3"> 3
</label>

无标签文本的复选框和单选按钮

如果你在 <label> 中没有文本,输入框的位置会如你所预期。目前仅适用于非内联复选框和单选按钮。请记住仍然为辅助技术提供某种形式的标签(例如,使用 aria-label)。

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

选择框

请注意,许多原生选择菜单——特别是在 Safari 和 Chrome 中——具有圆角,无法通过 border-radius 属性修改。

<select class="form-control">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>

对于带有 multiple 属性的 <select> 控件,默认显示多个选项。

<select multiple class="form-control">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>

静态控件

当你需要在表单内的表单标签旁边放置纯文本时,请在 <p> 上使用 .form-control-static 类。

email@example.com

<form class="form-horizontal">
  <div class="form-group">
    <label class="col-sm-2 control-label">Email</label>
    <div class="col-sm-10">
      <p class="form-control-static">email@example.com</p>
    </div>
  </div>
  <div class="form-group">
    <label for="inputPassword" class="col-sm-2 control-label">Password</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="inputPassword" placeholder="Password">
    </div>
  </div>
</form>

email@example.com

<form class="form-inline">
  <div class="form-group">
    <label class="sr-only">Email</label>
    <p class="form-control-static">email@example.com</p>
  </div>
  <div class="form-group">
    <label for="inputPassword2" class="sr-only">Password</label>
    <input type="password" class="form-control" id="inputPassword2" placeholder="Password">
  </div>
  <button type="submit" class="btn btn-default">Confirm identity</button>
</form>

焦点状态

我们移除了某些表单控件上的默认 outline 样式,并用 box-shadow 代替用于 :focus

演示 :focus 状态

上述示例输入框在我们的文档中使用自定义样式来演示 .form-control 上的 :focus 状态。

禁用状态

在输入框上添加 disabled 布尔属性以防止用户交互。禁用的输入框显示为浅色并添加 not-allowed 光标。

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

禁用 fieldsets

<fieldset> 添加 disabled 属性以一次性禁用 <fieldset> 内的所有控件。

关于 <a> 链接功能的注意事项

默认情况下,浏览器会将 <fieldset disabled> 内的所有原生表单控件(<input><select><button> 元素)视为禁用,防止对它们进行键盘和鼠标交互。但是,如果你的表单还包含 <a ... class="btn btn-*"> 元素,这些只会被赋予 pointer-events: none 样式。如关于按钮禁用状态的部分(特别是关于锚元素的子部分)所述,此 CSS 属性尚未标准化,在 Opera 18 及以下版本或 Internet Explorer 11 中不完全受支持,并且不会阻止键盘用户能够聚焦或激活这些链接。所以为了安全起见,使用自定义 JavaScript 来禁用此类链接。

跨浏览器兼容性

虽然 Bootstrap 会在所有浏览器中应用这些样式,但 Internet Explorer 11 及以下版本不完全支持 <fieldset> 上的 disabled 属性。在这些浏览器中使用自定义 JavaScript 来禁用 fieldset。

<form>
  <fieldset disabled>
    <div class="form-group">
      <label for="disabledTextInput">Disabled input</label>
      <input type="text" id="disabledTextInput" class="form-control" placeholder="Disabled input">
    </div>
    <div class="form-group">
      <label for="disabledSelect">Disabled select menu</label>
      <select id="disabledSelect" class="form-control">
        <option>Disabled select</option>
      </select>
    </div>
    <div class="checkbox">
      <label>
        <input type="checkbox"> Can't check this
      </label>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
  </fieldset>
</form>

只读状态

在输入框上添加 readonly 布尔属性以防止修改输入框的值。只读输入框显示为浅色(就像禁用的输入框),但保留标准光标。

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

帮助文本

表单控件的块级帮助文本。

将帮助文本与表单控件关联

帮助文本应使用 aria-describedby 属性显式关联到其相关的表单控件。这将确保辅助技术——例如屏幕阅读器——在用户聚焦或进入控件时宣布此帮助文本。

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

验证状态

Bootstrap 包含表单控件的错误、警告和成功状态的验证样式。要使用,将 .has-warning.has-error.has-success 添加到父元素。该元素内的任何 .control-label.form-control.help-block 都将接收验证样式。

向辅助技术和色盲用户传达验证状态

使用这些验证样式来表示表单控件的状态仅提供基于视觉、颜色的指示,不会传达给使用辅助技术的用户——例如屏幕阅读器——或色盲用户。

确保还提供了状态的替代指示。例如,你可以在表单控件的 <label> 文本本身中包含状态提示(如下面的代码示例中所示),包含一个 Glyphicon(使用 .sr-only 类和适当的替代文本——参见 Glyphicon 示例),或提供额外的帮助文本块。特别是对于辅助技术,还可以为无效的表单控件分配一个 aria-invalid="true" 属性。

A block of help text that breaks onto a new line and may extend beyond one line.
<div class="form-group has-success">
  <label class="control-label" for="inputSuccess1">Input with success</label>
  <input type="text" class="form-control" id="inputSuccess1" aria-describedby="helpBlock2">
  <span id="helpBlock2" class="help-block">A block of help text that breaks onto a new line and may extend beyond one line.</span>
</div>
<div class="form-group has-warning">
  <label class="control-label" for="inputWarning1">Input with warning</label>
  <input type="text" class="form-control" id="inputWarning1">
</div>
<div class="form-group has-error">
  <label class="control-label" for="inputError1">Input with error</label>
  <input type="text" class="form-control" id="inputError1">
</div>
<div class="has-success">
  <div class="checkbox">
    <label>
      <input type="checkbox" id="checkboxSuccess" value="option1">
      Checkbox with success
    </label>
  </div>
</div>
<div class="has-warning">
  <div class="checkbox">
    <label>
      <input type="checkbox" id="checkboxWarning" value="option1">
      Checkbox with warning
    </label>
  </div>
</div>
<div class="has-error">
  <div class="checkbox">
    <label>
      <input type="checkbox" id="checkboxError" value="option1">
      Checkbox with error
    </label>
  </div>
</div>

带可选图标

你还可以通过添加 .has-feedback 和右侧图标来添加可选的反馈图标。

反馈图标仅适用于文本 <input class="form-control"> 元素。

图标、标签和输入组

需要手动定位没有标签的输入框的反馈图标以及右侧带有附加组件的 输入组。出于可访问性原因,强烈建议为所有输入框提供标签。如果你希望阻止标签显示,请使用 .sr-only 类隐藏它们。如果你必须不使用标签,请调整反馈图标的 top 值。对于输入组,请将 right 值调整为适当的像素值,具体取决于附加组件的宽度。

向辅助技术传达图标的含义

为了确保辅助技术——例如屏幕阅读器——正确传达图标的含义,应使用 .sr-only 类包含额外的隐藏文本,并使用 aria-describedby 显式关联到其相关的表单控件。或者,确保以某种其他形式传达含义(例如,特定文本输入字段存在警告的事实),例如更改与表单控件关联的实际 <label> 的文本。

虽然以下示例已经在其各自的 <label> 文本本身中提到了表单控件的验证状态,但上述技术(使用 .sr-only 文本和 aria-describedby)已包含用于说明目的。

(success)
(warning)
(error)
@
(success)
<div class="form-group has-success has-feedback">
  <label class="control-label" for="inputSuccess2">Input with success</label>
  <input type="text" class="form-control" id="inputSuccess2" aria-describedby="inputSuccess2Status">
  <span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
  <span id="inputSuccess2Status" class="sr-only">(success)</span>
</div>
<div class="form-group has-warning has-feedback">
  <label class="control-label" for="inputWarning2">Input with warning</label>
  <input type="text" class="form-control" id="inputWarning2" aria-describedby="inputWarning2Status">
  <span class="glyphicon glyphicon-warning-sign form-control-feedback" aria-hidden="true"></span>
  <span id="inputWarning2Status" class="sr-only">(warning)</span>
</div>
<div class="form-group has-error has-feedback">
  <label class="control-label" for="inputError2">Input with error</label>
  <input type="text" class="form-control" id="inputError2" aria-describedby="inputError2Status">
  <span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>
  <span id="inputError2Status" class="sr-only">(error)</span>
</div>
<div class="form-group has-success has-feedback">
  <label class="control-label" for="inputGroupSuccess1">Input group with success</label>
  <div class="input-group">
    <span class="input-group-addon">@</span>
    <input type="text" class="form-control" id="inputGroupSuccess1" aria-describedby="inputGroupSuccess1Status">
  </div>
  <span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
  <span id="inputGroupSuccess1Status" class="sr-only">(success)</span>
</div>

水平和内联表单中的可选图标

(success)
@
(success)
<form class="form-horizontal">
  <div class="form-group has-success has-feedback">
    <label class="control-label col-sm-3" for="inputSuccess3">Input with success</label>
    <div class="col-sm-9">
      <input type="text" class="form-control" id="inputSuccess3" aria-describedby="inputSuccess3Status">
      <span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
      <span id="inputSuccess3Status" class="sr-only">(success)</span>
    </div>
  </div>
  <div class="form-group has-success has-feedback">
    <label class="control-label col-sm-3" for="inputGroupSuccess2">Input group with success</label>
    <div class="col-sm-9">
      <div class="input-group">
        <span class="input-group-addon">@</span>
        <input type="text" class="form-control" id="inputGroupSuccess2" aria-describedby="inputGroupSuccess2Status">
      </div>
      <span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
      <span id="inputGroupSuccess2Status" class="sr-only">(success)</span>
    </div>
  </div>
</form>
(success)

@
(success)
<form class="form-inline">
  <div class="form-group has-success has-feedback">
    <label class="control-label" for="inputSuccess4">Input with success</label>
    <input type="text" class="form-control" id="inputSuccess4" aria-describedby="inputSuccess4Status">
    <span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
    <span id="inputSuccess4Status" class="sr-only">(success)</span>
  </div>
</form>
<form class="form-inline">
  <div class="form-group has-success has-feedback">
    <label class="control-label" for="inputGroupSuccess3">Input group with success</label>
    <div class="input-group">
      <span class="input-group-addon">@</span>
      <input type="text" class="form-control" id="inputGroupSuccess3" aria-describedby="inputGroupSuccess3Status">
    </div>
    <span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
    <span id="inputGroupSuccess3Status" class="sr-only">(success)</span>
  </div>
</form>

带隐藏 .sr-only 标签的可选图标

如果你使用 .sr-only 类隐藏表单控件的 <label>(而不是使用其他标签选项,例如 aria-label 属性),Bootstrap 会在添加图标后自动调整图标的位置。

(success)
@
(success)
<div class="form-group has-success has-feedback">
  <label class="control-label sr-only" for="inputSuccess5">Hidden label</label>
  <input type="text" class="form-control" id="inputSuccess5" aria-describedby="inputSuccess5Status">
  <span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
  <span id="inputSuccess5Status" class="sr-only">(success)</span>
</div>
<div class="form-group has-success has-feedback">
  <label class="control-label sr-only" for="inputGroupSuccess4">Input group with success</label>
  <div class="input-group">
    <span class="input-group-addon">@</span>
    <input type="text" class="form-control" id="inputGroupSuccess4" aria-describedby="inputGroupSuccess4Status">
  </div>
  <span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
  <span id="inputGroupSuccess4Status" class="sr-only">(success)</span>
</div>

控件大小

使用 .input-lg 等类设置高度,使用网格列类如 .col-lg-* 设置宽度。

高度大小

创建与按钮大小相匹配的更高或更短的表单控件。

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

<select class="form-control input-lg">...</select>
<select class="form-control">...</select>
<select class="form-control input-sm">...</select>

水平表单组大小

通过添加 .form-group-lg.form-group-sm 快速在 .form-horizontal 中设置标签和表单控件的大小。

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

列大小

将输入框包裹在网格列或任何自定义父元素中,以轻松强制执行所需的宽度。

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

按钮

按钮标签

<a><button><input> 元素上使用按钮类。

链接
<a class="btn btn-default" href="#" role="button">Link</a>
<button class="btn btn-default" type="submit">Button</button>
<input class="btn btn-default" type="button" value="Input">
<input class="btn btn-default" type="submit" value="Submit">

特定上下文使用

虽然按钮类可以在 <a><button> 元素上使用,但在我们的导航和导航栏组件中仅支持 <button> 元素。

作为按钮的链接

如果 <a> 元素被用作按钮 – 触发页面内功能,而不是导航到另一个文档或当前页面的某个部分 – 它们还应该被赋予适当的 role="button"

跨浏览器渲染

作为最佳实践,我们强烈建议尽可能使用 <button> 元素,以确保匹配的跨浏览器渲染。

除此之外,Firefox <30 中存在 一个 bug,阻止我们设置基于 <input> 的按钮的 line-height,导致它们在 Firefox 上与其他按钮的高度不完全匹配。

选项

使用任何可用的按钮类来快速创建样式化按钮。

<!-- Standard button -->
<button type="button" class="btn btn-default">Default</button>

<!-- Provides extra visual weight and identifies the primary action in a set of buttons -->
<button type="button" class="btn btn-primary">Primary</button>

<!-- Indicates a successful or positive action -->
<button type="button" class="btn btn-success">Success</button>

<!-- Contextual button for informational alert messages -->
<button type="button" class="btn btn-info">Info</button>

<!-- Indicates caution should be taken with this action -->
<button type="button" class="btn btn-warning">Warning</button>

<!-- Indicates a dangerous or potentially negative action -->
<button type="button" class="btn btn-danger">Danger</button>

<!-- Deemphasize a button by making it look like a link while maintaining button behavior -->
<button type="button" class="btn btn-link">Link</button>

向辅助技术传达含义

使用颜色为按钮添加含义仅提供视觉指示,这不会传达给辅助技术的用户 – 例如屏幕阅读器。确保通过颜色表示的信息要么从内容本身(按钮的可见文本)中显而易见,要么通过其他方式包含,例如使用 .sr-only 类隐藏的额外文本。

尺寸

想要更大或更小的按钮?添加 .btn-lg.btn-sm.btn-xs 以获得额外的尺寸。

<p>
  <button type="button" class="btn btn-primary btn-lg">Large button</button>
  <button type="button" class="btn btn-default btn-lg">Large button</button>
</p>
<p>
  <button type="button" class="btn btn-primary">Default button</button>
  <button type="button" class="btn btn-default">Default button</button>
</p>
<p>
  <button type="button" class="btn btn-primary btn-sm">Small button</button>
  <button type="button" class="btn btn-default btn-sm">Small button</button>
</p>
<p>
  <button type="button" class="btn btn-primary btn-xs">Extra small button</button>
  <button type="button" class="btn btn-default btn-xs">Extra small button</button>
</p>

通过添加 .btn-block 创建块级按钮——那些跨越父元素全宽的按钮。

<button type="button" class="btn btn-primary btn-lg btn-block">Block level button</button>
<button type="button" class="btn btn-default btn-lg btn-block">Block level button</button>

活动状态

按钮在活动时会显示为被按下的状态(具有较暗的背景、较暗的边框和内阴影)。对于 <button> 元素,这是通过 :active 完成的。对于 <a> 元素,这是通过 .active 完成的。但是,如果您需要以编程方式复制活动状态,您可以在 <button> 上使用 .active(并包含 aria-pressed="true" 属性)。

按钮元素

不需要添加 :active,因为它是一个伪类,但如果您需要强制相同的外观,请继续添加 .active

<button type="button" class="btn btn-primary btn-lg active">Primary button</button>
<button type="button" class="btn btn-default btn-lg active">Button</button>

锚点元素

<a> 按钮添加 .active 类。

主要链接 链接

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

禁用状态

通过使用 opacity 使按钮变淡,使其看起来不可点击。

按钮元素

<button> 按钮添加 disabled 属性。

<button type="button" class="btn btn-lg btn-primary" disabled="disabled">Primary button</button>
<button type="button" class="btn btn-default btn-lg" disabled="disabled">Button</button>

跨浏览器兼容性

如果您向 <button> 添加 disabled 属性,Internet Explorer 9 及以下版本会将文本渲染为灰色,并带有我们无法修复的难看的文本阴影。

锚点元素

<a> 按钮添加 .disabled 类。

主要链接 链接

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

我们在这里使用 .disabled 作为实用类,类似于常见的 .active 类,因此不需要前缀。

链接功能注意事项

此类使用 pointer-events: none 尝试禁用 <a> 的链接功能,但该 CSS 属性尚未标准化,在 Opera 18 及以下版本或 Internet Explorer 11 中不完全支持。此外,即使在支持 pointer-events: none 的浏览器中,键盘导航仍然不受影响,这意味着视力正常的键盘用户和辅助技术的用户仍然能够激活这些链接。因此,为了安全起见,请使用自定义 JavaScript 来禁用此类链接。

图片

响应式图片

通过添加 .img-responsive 类,可以使 Bootstrap 3 中的图片具有响应式特性。这会将 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 不会自动应用此设置,因为它会导致其他图片格式出现复杂问题。

<img src="..." class="img-responsive" alt="响应式图片">

图片形状

<img> 元素添加类,以便在任何项目中轻松设置图片样式。

跨浏览器兼容性

请记住,Internet Explorer 8 不支持圆角。

带有圆角的通用方形占位图 通用方形占位图,仅显示内切圆内的部分 带有白色边框的通用方形占位图,使其类似于用旧即时相机拍摄的照片
<img src="..." alt="..." class="img-rounded">
<img src="..." alt="..." class="img-circle">
<img src="..." alt="..." class="img-thumbnail">

辅助类

上下文颜色

通过少量强调实用类来传达颜色的含义。这些也可以应用于链接,并且会像我们的默认链接样式一样在悬停时变暗。

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.

<p class="text-muted">...</p>
<p class="text-primary">...</p>
<p class="text-success">...</p>
<p class="text-info">...</p>
<p class="text-warning">...</p>
<p class="text-danger">...</p>

处理特异性

有时由于另一个选择器的特异性,无法应用强调类。在大多数情况下,一个足够的解决方法是用带有该类的 <span> 包裹您的文本。

向辅助技术传达含义

使用颜色添加含义仅提供视觉指示,这不会传达给辅助技术的用户 – 例如屏幕阅读器。确保通过颜色表示的信息要么从内容本身中显而易见(上下文颜色仅用于加强文本/标记中已有的含义),要么通过其他方式包含,例如使用 .sr-only 类隐藏的额外文本。

上下文背景

类似于上下文文本颜色类,轻松将元素的背景设置为任何上下文类。锚点组件将在悬停时变暗,就像文本类一样。

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.

<p class="bg-primary">...</p>
<p class="bg-success">...</p>
<p class="bg-info">...</p>
<p class="bg-warning">...</p>
<p class="bg-danger">...</p>

处理特异性

有时由于另一个选择器的特异性,无法应用上下文背景类。在某些情况下,一个足够的解决方法是用带有该类的 <div> 包裹元素的内容。

向辅助技术传达含义

上下文颜色一样,确保通过颜色传达的任何含义也以非纯呈现性格式传达。

关闭图标

使用通用关闭图标来关闭内容,如模态框和警告框。

<button type="button" class="close" aria-label="关闭"><span aria-hidden="true">&times;</span></button>

插入符

使用插入符来指示下拉菜单功能和方向。请注意,默认插入符会在上拉菜单中自动反转。

<span class="caret"></span>

快速浮动

使用类将元素向左或向右浮动。!important 包含在内以避免特异性问题。类也可以用作 mixin。

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

// 用作 mixin
.element {
  .pull-left();
}
.another-element {
  .pull-right();
}

不用于导航栏

要使用实用类在导航栏中对齐组件,请改用 .navbar-left.navbar-right。有关详细信息,请参阅导航栏文档

居中内容块

将元素设置为 display: block 并通过 margin 居中。可用作 mixin 和类。

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

// 用作 mixin
.element {
  .center-block();
}

清除浮动

通过向父元素添加 .clearfix 来轻松清除 float。利用 Nicolas Gallagher 推广的 micro clearfix。也可以用作 mixin。

<!-- 用作类 -->
<div class="clearfix">...</div>
// Mixin 本身
.clearfix() {
  &:before,
  &:after {
    content: " ";
    display: table;
  }
  &:after {
    clear: both;
  }
}

// 用作 mixin
.element {
  .clearfix();
}

显示和隐藏内容

使用 .show.hidden 类强制显示或隐藏元素(包括对屏幕阅读器)。这些类使用 !important 来避免特异性冲突,就像快速浮动一样。它们仅适用于块级切换。它们也可以用作 mixin。

.hide 可用,但它并不总是影响屏幕阅读器,并且自 v3.0.1 起已弃用。请改用 .hidden.sr-only

此外,.invisible 可用于仅切换元素的可见性,这意味着它的 display 不会被修改,元素仍然可以影响文档的流动。

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

// 用作 mixin
.element {
  .show();
}
.another-element {
  .hidden();
}

屏幕阅读器和键盘导航内容

使用 .sr-only 对所有设备(屏幕阅读器除外)隐藏元素。将 .sr-only.sr-only-focusable 结合使用,以便在元素获得焦点时再次显示它(例如,通过仅键盘用户)。遵循无障碍最佳实践所必需的。也可以用作 mixin。

<a class="sr-only sr-only-focusable" href="#content">跳转到主内容</a>
// 用作 mixin
.skip-navigation {
  .sr-only();
  .sr-only-focusable();
}

图片替换

利用 .text-hide 类或 mixin 帮助将元素的文本内容替换为背景图片。

<h1 class="text-hide">自定义标题</h1>
// 用作 mixin
.heading {
  .text-hide();
}

响应式工具类

为了更快地进行移动友好的开发,请使用这些工具类通过媒体查询按设备显示和隐藏内容。还包括用于在打印时切换内容的工具类。

尽量有限度地使用这些工具类,避免创建同一站点的完全不同版本。相反,使用它们来补充每个设备的展示效果。

可用类

使用单个或组合可用类在视口断点之间切换内容。

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

自 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,只是为切换 <table> 相关元素提供了额外的特殊情况。

打印类

与常规响应式类类似,使用这些类来切换打印时的内容。

浏览器 打印
.visible-print-block
.visible-print-inline
.visible-print-inline-block
Visible
.hidden-print Visible

.visible-print 类也存在,但自 v3.2.0 起已弃用。它大致等同于 .visible-print-block,只是为 <table> 相关元素提供了额外的特殊情况。

测试用例

调整浏览器大小或在不同设备上加载以测试响应式工具类。

可见于...

绿色对勾标记表示元素在当前视口中可见

✔ 可见于超小
✔ 可见于小
中等 ✔ 可见于中等
✔ 可见于大
✔ 可见于超小和小
✔ 可见于中等和大
✔ 可见于超小和中等
✔ 可见于小和大
✔ 可见于超小和大
✔ 可见于小和中等

隐藏于...

这里,绿色对勾标记也表示元素在当前视口中隐藏

✔ 隐藏于超小
✔ 隐藏于小
中等 ✔ 隐藏于中等
✔ 隐藏于大
✔ 隐藏于超小和小
✔ 隐藏于中等和大
✔ 隐藏于超小和中等
✔ 隐藏于小和大
✔ 隐藏于超小和大
✔ 隐藏于小和中等

使用 Less

Bootstrap 的 CSS 是基于 Less 构建的,Less 是一个预处理器,提供了变量、混合宏和函数等额外功能用于编译 CSS。那些希望使用源 Less 文件而不是编译后的 CSS 文件的人可以利用我们在整个框架中使用的众多变量和混合宏。

网格变量和混合宏在网格系统部分中介绍。

编译 Bootstrap

Bootstrap 可以通过至少两种方式使用:使用编译后的 CSS 或使用源 Less 文件。要编译 Less 文件,请参考入门指南部分了解如何设置开发环境以运行必要的命令。

第三方编译工具可能与 Bootstrap 兼容,但不受我们核心团队支持。

变量

变量在整个项目中用于集中和共享常用值,如颜色、间距或字体栈。如需完整列表,请参阅定制器

颜色

轻松使用两种配色方案:灰度色系和语义色系。灰度颜色提供常用的黑色阴影,而语义颜色包括分配给有意义上下文值的各种颜色。

@gray-darker:  lighten(#000, 13.5%); // #222
@gray-dark:    lighten(#000, 20%);   // #333
@gray:         lighten(#000, 33.5%); // #555
@gray-light:   lighten(#000, 46.7%); // #777
@gray-lighter: lighten(#000, 93.5%); // #eee
@brand-primary: darken(#428bca, 6.5%); // #337ab7
@brand-success: #5cb85c;
@brand-info:    #5bc0de;
@brand-warning: #f0ad4e;
@brand-danger:  #d9534f;

原样使用这些颜色变量,或将它们重新分配给项目中更有意义的变量。

// 直接使用
.masthead {
  background-color: @brand-primary;
}

// 在 Less 中重新分配变量
@alert-message-background: @brand-info;
.alert {
  background-color: @alert-message-background;
}

脚手架

一些变量用于快速自定义网站骨架的关键元素。

// 脚手架
@body-bg:    #fff;
@text-color: @black-50;

只需一个值即可轻松为链接设置正确的颜色。

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

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

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

注意 @link-hover-color 使用了函数,这是 Less 的另一个出色工具,可以自动创建正确的悬停颜色。你可以使用 darkenlightensaturatedesaturate

排版

通过几个快速变量轻松设置字体、文本大小、行高等。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;

供应商混合宏

供应商混合宏是在编译后的 CSS 中包含所有相关供应商前缀的混合宏,以帮助支持多个浏览器。

盒模型

使用单个混合宏重置组件的盒模型。有关背景信息,请参阅 Mozilla 的有用文章

该混合宏自 v3.2.0 起已弃用,因为引入了 Autoprefixer。为了保持向后兼容性,Bootstrap 将在内部继续使用该混合宏直到 Bootstrap v4。

.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() 混合宏,但 Bootstrap 确实包含用于快速在对象特定侧面上圆两个角的快捷方式。

.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-shadow 属性。如果你需要支持旧的 Android(v4 之前)和 iOS 设备(iOS 5 之前),请使用已弃用的混合宏来获取所需的 -webkit 前缀。

该混合宏自 v3.1.0 起弃用,因为 Bootstrap 不再正式支持不支持标准属性的过时平台。为了保持向后兼容性,Bootstrap 将在内部继续使用该混合宏直到 Bootstrap v4。

务必在盒阴影中使用 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;
}

过渡

多个混合宏提供灵活性。使用一个设置所有过渡信息,或根据需要指定单独的延迟和持续时间。

这些混合宏自 v3.2.0 起弃用,因为引入了 Autoprefixer。为了保持向后兼容性,Bootstrap 将在内部继续使用这些混合宏直到 Bootstrap v4。

.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;
}

变换

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

这些混合宏自 v3.2.0 起弃用,因为引入了 Autoprefixer。为了保持向后兼容性,Bootstrap 将在内部继续使用这些混合宏直到 Bootstrap v4。

.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); // 参见 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;
}

动画

使用单个混合宏在一个声明中使用 CSS3 的所有动画属性,以及其他用于单独属性的混合宏。

这些混合宏自 v3.2.0 起弃用,因为引入了 Autoprefixer。为了保持向后兼容性,Bootstrap 将在内部继续使用这些混合宏直到 Bootstrap v4。

.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(@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;
}

渐变

轻松将任意两种颜色转换为背景渐变。更进一步,设置方向、使用三种颜色或使用径向渐变。使用单个混合宏,你可以获得所需的所有带前缀的语法。

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

你还可以指定标准双色线性渐变的角度:

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

如果你需要一个类似理发店条纹风格的渐变,那也很简单。只需指定一种颜色,我们会覆盖一个半透明的白色条纹。

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

更进一步,使用三种颜色。使用这些混合宏设置第一种颜色、第二种颜色、第二种颜色的色标(百分比值,如 25%)和第三种颜色:

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

注意!如果你需要移除渐变,请务必移除你可能添加的任何 IE 特定的 filter。你可以通过使用 .reset-filter() 混合宏配合 background-image: none; 来实现。

实用混合宏

实用混合宏是将原本不相关的 CSS 属性组合起来以实现特定目标或任务的混合宏。

清除浮动

忘记给任何元素添加 class="clearfix",而是在适当的地方添加 .clearfix() 混合宏。使用来自 Nicolas Gallaghermicro clearfix

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

// 使用
.container {
  .clearfix();
}

水平居中

快速将任何元素在其父元素内居中。需要设置 widthmax-width

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

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

尺寸辅助

更轻松地指定对象的尺寸。

// 混合宏
.size(@width; @height) {
  width: @width;
  height: @height;
}
.square(@size) {
  .size(@size; @size);
}

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

可调整大小的文本域

轻松配置任何文本域或其他元素的调整大小选项。默认为正常浏览器行为(both)。

.resizable(@direction: both) {
  // 选项:horizontal, vertical, both
  resize: @direction;
  // Safari 修复
  overflow: auto;
}

截断文本

使用单个混合宏轻松用省略号截断文本。要求元素为 blockinline-block 级别。

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

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

Retina 图像

指定两个图像路径和 @1x 图像尺寸,Bootstrap 将提供一个 @2x 媒体查询。如果你有很多图像要提供,可以考虑在单个媒体查询中手动编写你的 Retina 图像 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;
  }
}

// 使用
.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 代码(Sass 配置、Rails 和 Compass 集成)
tasks/ 转换脚本(将上游 Less 转换为 Sass)
test/ 编译测试
templates/ Compass 包清单
vendor/assets/ Sass、JavaScript 和字体文件
Rakefile 内部任务,如 rake 和 convert

访问 Sass 移植版本的 GitHub 仓库查看这些文件的实际应用。

安装

有关如何安装和使用 Bootstrap for Sass 的信息,请参阅 GitHub 仓库的自述文件。它是最新的来源,包含与 Rails、Compass 和标准 Sass 项目一起使用的信息。

Bootstrap for Sass