跳到主要内容

Bootstrap 排版的文档和示例,包括全局设置、标题、正文、列表等。

全局设置

Bootstrap 设置了基本的全局显示、排版和链接样式。当需要更多控制时,请查看 文本工具类。

  • 使用一个 本地字体堆栈 选择最好的 font-family 对于每个操作系统和设备。
  • 为了更具包容性和可访问性的字体等级,我们使用浏览器的默认根字体 font-size (通常为16像素),以便访客根据需要自定义他们的浏览器默认设置。
  • 使用 $font-family-base, $font-size-base,和 $line-height-base 作为我们排版基础应用的属性到 <body>.
  • 通过设置全局链接颜色 $link-color.
  • 使用 $body-bg 设置一个 background-color 在……上 <body> (#fff 默认情况下)。

这些样式可以在其中找到 _reboot.scss,全局变量定义在 _variables.scss. 确保设置 $font-size-base 在 rem.

标题

所有 HTML 标题,<h1> 到 <h6>,均可使用。

标题示例
<h1></h1>h1. Bootstrap 标题
<h2></h2>h2. Bootstrap 标题
<h3></h3>h3. Bootstrap 标题
<h4></h4>h4. Bootstrap 标题
<h5></h5>h5. Bootstrap 标题
<h6></h6>h6. Bootstrap 标题
<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>

.h1 到 .h6 类也可用,当你想匹配标题的字体样式但不能使用相关的 HTML 元素时使用。

h1. Bootstrap 标题

h2. Bootstrap 标题

h3. Bootstrap 标题

h4. Bootstrap 标题

h5. Bootstrap 标题

h6. Bootstrap 标题

HTML
<p class="h1">h1. Bootstrap heading</p>
<p class="h2">h2. Bootstrap heading</p>
<p class="h3">h3. Bootstrap heading</p>
<p class="h4">h4. Bootstrap heading</p>
<p class="h5">h5. Bootstrap heading</p>
<p class="h6">h6. Bootstrap heading</p>

自定义标题

使用随附的工具类重新创建 Bootstrap 3 的小型次要标题文本。

花哨的显示标题 带褪色的二级文字

HTML
<h3>
  Fancy display heading
  <small class="text-body-secondary">With faded secondary text</small>
</h3>

显示标题

传统的标题元素设计得在页面内容的主体部分效果最佳。当你需要一个标题脱颖而出时,可以考虑使用展示标题——一种更大、更具个性色彩的标题样式。

显示 1
显示 2
显示 3
显示 4
显示 5
显示 6
<h1 class="display-1">Display 1</h1>
<h1 class="display-2">Display 2</h1>
<h1 class="display-3">Display 3</h1>
<h1 class="display-4">Display 4</h1>
<h1 class="display-5">Display 5</h1>
<h1 class="display-6">Display 6</h1>

显示标题通过 $display-font-sizes Sass 映射和两个变量 $display-font-weight 与 $display-line-height 配置。

显示标题可以通过两个变量 $display-font-family 和 $display-font-style 自定义。

$display-font-sizes: (
  1: 5rem,
  2: 4.5rem,
  3: 4rem,
  4: 3.5rem,
  5: 3rem,
  6: 2.5rem
);

$display-font-family: null; $display-font-style: null; $display-font-weight: 300; $display-line-height: $headings-line-height;

铅

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

这是一个导语段落。它与普通段落不同。

HTML
<p class="lead">
  This is a lead paragraph. It stands out from regular paragraphs.
</p>

行内文本元素

常见内联 HTML5 元素的样式。

你可以使用 mark 标签来 强调 文本。

这行文字被视为已删除的文本。

这行文字应被视为不再准确。

这行文字旨在被视为对文档的补充。

这一行文字将显示为下划线。

这行文字应被视为小字部分。

这一行呈现为粗体文本。

这一行显示为斜体文本。

HTML
<p>You can use the mark tag to <mark>highlight</mark> text.</p>
<p><del>This line of text is meant to be treated as deleted text.</del></p>
<p><s>This line of text is meant to be treated as no longer accurate.</s></p>
<p><ins>This line of text is meant to be treated as an addition to the document.</ins></p>
<p><u>This line of text will render as underlined.</u></p>
<p><small>This line of text is meant to be treated as fine print.</small></p>
<p><strong>This line rendered as bold text.</strong></p>
<p><em>This line rendered as italicized text.</em></p>

请注意,这些标签应该用于语义目的:

  • <mark> 表示用于参考或标注目的而被标记或高亮的文本。
  • <small> 表示旁注和小字部分,如版权和法律文本。
  • <s> 表示不再相关或不再准确的元素。
  • <u> 表示一段应以显示其具有非文本注释方式呈现的行内文本。

如果你想为文本设置样式,你应该使用以下类:

  • .mark 将应用与 <mark> 相同的样式。
  • .small 将应用与 <small> 相同的样式。
  • .text-decoration-underline 将应用与 <u> 相同的样式。
  • .text-decoration-line-through 将应用与 <s> 相同的样式。

虽然上面没有显示,但可以随意在 HTML5 中使用 <b> 和 <i>。<b> 用于强调单词或短语而不传达额外的重要性,而 <i> 主要用于语音、技术术语等。

文本工具

使用我们的文本工具和颜色工具可以更改文本对齐、转换、样式、粗细、行高、装饰和颜色。

缩写

HTML <abbr> 元素的风格化实现,用于缩写和首字母缩略词,在悬停时显示完整版本。缩写默认带下划线,并在悬停时显示帮助光标,以向悬停用户和辅助技术用户提供额外上下文。

在缩写中添加.initialism以稍微减小字体大小。

属性

HTML

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

引用块

用于在文档中引用来自其他来源的内容块。将<blockquote class="blockquote">包裹在任何 HTML 代码周围作为引用。

一个包含在引用块元素中的著名引用。

HTML
<blockquote class="blockquote">
  <p>A well-known quote, contained in a blockquote element.</p>
</blockquote>

命名来源

HTML 规范要求将引用出处放置在 <blockquote> 外。提供出处时,将你的 <blockquote> 包裹在 <figure> 中,并使用 <figcaption> 或块级元素(例如 <p>)并带有 .blockquote-footer 类。请确保将来源作品的名称也包裹在 <cite> 中。

一个包含在引用块元素中的著名引用。

HTML
<figure>
  <blockquote class="blockquote">
    <p>A well-known quote, contained in a blockquote element.</p>
  </blockquote>
  <figcaption class="blockquote-footer">
    Someone famous in <cite title="Source Title">Source Title</cite>
  </figcaption>
</figure>

对齐

根据需要使用文本工具类来更改引用块的对齐方式。

一个包含在引用块元素中的著名引用。

HTML
<figure class="text-center">
  <blockquote class="blockquote">
    <p>A well-known quote, contained in a blockquote element.</p>
  </blockquote>
  <figcaption class="blockquote-footer">
    Someone famous in <cite title="Source Title">Source Title</cite>
  </figcaption>
</figure>

一个包含在引用块元素中的著名引用。

HTML
<figure class="text-end">
  <blockquote class="blockquote">
    <p>A well-known quote, contained in a blockquote element.</p>
  </blockquote>
  <figcaption class="blockquote-footer">
    Someone famous in <cite title="Source Title">Source Title</cite>
  </figcaption>
</figure>

列表

无样式

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

  • 这是一个列表。
  • 它看起来完全没有样式。
  • 从结构上看,它仍然是一个列表。
  • 然而,这种样式只适用于直接子元素。
  • 嵌套列表:
    • 不受这种风格影响
    • 仍然会显示一个项目符号
    • 并具有适当的左边距
  • 在某些情况下,这仍然可能派上用场。
HTML
<ul class="list-unstyled">
  <li>This is a list.</li>
  <li>It appears completely unstyled.</li>
  <li>Structurally, it’s still a list.</li>
  <li>However, this style only applies to immediate child elements.</li>
  <li>Nested lists:
    <ul>
      <li>are unaffected by this style</li>
      <li>will still show a bullet</li>
      <li>and have appropriate left margin</li>
    </ul>
  </li>
  <li>This may still come in handy in some situations.</li>
</ul>

内联

删除列表的项目符号并应用一些浅色 margin 结合两种类, .list-inline 和 .list-inline-item.

  • 这是一个列表项。
  • 还有一个。
  • 但是它们是内联显示的。
HTML
<ul class="list-inline">
  <li class="list-inline-item">This is a list item.</li>
  <li class="list-inline-item">And another one.</li>
  <li class="list-inline-item">But they’re displayed inline.</li>
</ul>

描述列表对齐

使用我们的网格系统预定义的类(或语义mixin)将术语和描述水平对齐。对于较长的术语,你可以选择添加一个 .text-truncate 类,以用省略号截断文本。

描述列表
描述列表非常适合定义术语。
术语

该术语的定义。

以及一些更多的占位符定义文本。

另一个术语
这个定义很短,所以没有额外的段落或任何内容。
被截断的术语已被截断
当空间紧张时,这可能会很有用。在末尾添加省略号。
巢
嵌套定义列表
我听说你喜欢定义列表。让我在你的定义列表里放一个定义列表。
HTML
<dl class="row">
  <dt class="col-sm-3">Description lists</dt>
  <dd class="col-sm-9">A description list is perfect for defining terms.</dd>

<dt class="col-sm-3">Term</dt> <dd class="col-sm-9"> <p>Definition for the term.</p> <p>And some more placeholder definition text.</p> </dd>

<dt class="col-sm-3">Another term</dt> <dd class="col-sm-9">This definition is short, so no extra paragraphs or anything.</dd>

<dt class="col-sm-3 text-truncate">Truncated term is truncated</dt> <dd class="col-sm-9">This can be useful when space is tight. Adds an ellipsis at the end.</dd>

<dt class="col-sm-3">Nesting</dt> <dd class="col-sm-9"> <dl class="row"> <dt class="col-sm-4">Nested definition list</dt> <dd class="col-sm-8">I heard you like definition lists. Let me put a definition list inside your definition list.</dd> </dl> </dd> </dl>

响应式字体大小

在 Bootstrap 5 中,我们默认启用了响应式字体大小,使文本能够在不同设备和视口尺寸上更自然地缩放。查看 RFS 页面 以了解其工作原理。

CSS

Sass 变量

标题有一些专门用于调整大小和间距的变量。

$headings-margin-bottom:      $spacer * .5;
$headings-font-family:        null;
$headings-font-style:         null;
$headings-font-weight:        500;
$headings-line-height:        1.2;
$headings-color:              inherit;

此处以及在 Reboot 中介绍的杂项排版元素也有专门的变量。

$lead-font-size:              $font-size-base * 1.25;
$lead-font-weight:            300;

$small-font-size: .875em;

$sub-sup-font-size: .75em;

// fusv-disable $text-muted: var(--#{$prefix}secondary-color); // Deprecated in 5.3.0 // fusv-enable

$initialism-font-size: $small-font-size;

$blockquote-margin-y: $spacer; $blockquote-font-size: $font-size-base * 1.25; $blockquote-footer-color: $gray-600; $blockquote-footer-font-size: $small-font-size;

$hr-margin-y: $spacer; $hr-color: inherit;

// fusv-disable $hr-bg-color: null; // Deprecated in v5.2.0 $hr-height: null; // Deprecated in v5.2.0 // fusv-enable

$hr-border-color: null; // Allows for inherited colors $hr-border-width: var(--#{$prefix}border-width); $hr-opacity: .25;

// scss-docs-start vr-variables $vr-border-width: var(--#{$prefix}border-width); // scss-docs-end vr-variables

$legend-margin-bottom: .5rem; $legend-font-size: 1.5rem; $legend-font-weight: null;

$dt-font-weight: $font-weight-bold;

$list-inline-padding: .5rem;

$mark-padding: .1875em; $mark-color: $body-color; $mark-bg: $yellow-100;

Sass mixin

没有专门用于排版的 mixin,但 Bootstrap 确实使用了 响应式字体大小 (RFS)。