排版

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

Layer: content

全局设置

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

  • 使用原生字体栈,为每个操作系统和设备选择最佳的 font-family。
  • 为获得更具包容性和可访问性的字号比例,我们使用浏览器的默认根 font-size(通常为 16px),以便访客可以根据需要自定义浏览器默认值。
  • 使用 --body-font-family、--body-font-size 和 --body-line-height 属性作为应用于 <body> 的排版基础。
  • 通过 --bs-link-color 设置全局链接颜色。
  • 使用 --bs-body-bg 在 <body> 上设置 background-color(默认为 #fff)。

这些样式可在 _reboot.scss 中找到,全局变量定义在 _config.scss 中。请确保以 rem 设置 --body-font-size。

标题

正如重置中所述,所有 HTML 标题 <h1> 到 <h6> 都可用,作为元素或类选择器。

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

<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>
        

请前往我们的字号工具类,以更好地控制 font-size 和 line-height。

全局字号

标题使用我们的响应式字号根 CSS 变量进行尺寸设置。你可以自己使用这些变量,或与我们的标题类、字号工具类和文本尺寸工具类(在那里你还会获得 line-height)一起使用。

在下面的 CSS 部分了解更多关于它们如何工作的内容。

.fs-xs
.fs-sm
.fs-md
.fs-lg
.fs-xl
.fs-2xl
.fs-3xl
.fs-4xl
.fs-5xl
.fs-6xl
HTML
<div class="fs-xs">.fs-xs</div>
<div class="fs-sm">.fs-sm</div>
<div class="fs-md">.fs-md</div>
<div class="fs-lg">.fs-lg</div>
<div class="fs-xl">.fs-xl</div>
<div class="fs-2xl">.fs-2xl</div>
<div class="fs-3xl">.fs-3xl</div>
<div class="fs-4xl">.fs-4xl</div>
<div class="fs-5xl">.fs-5xl</div>
<div class="fs-6xl">.fs-6xl</div>

行内文本元素

常见行内 HTML5 元素的样式。

You can use the mark tag to highlight text.

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

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

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

This line of text will render as underlined.

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

This line rendered as bold text.

This line rendered as italicized text.

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> 主要用于声音、技术术语等。

文本工具类

大多数常见文本相关 CSS 属性都有可用的工具类。

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

缩写

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

为缩写添加 .initialism 以获得略小的字号。

attr

HTML

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

引用块

将 <blockquote class="blockquote"> 包裹在任何 HTML 周围,以在文档中引用来自其他来源的内容块。我们将 .blockquote 设为 Flex 容器以便更轻松地设置间距,这意味着你可以使用 Flex 布局工具类进行对齐、间距等。

A well-known quote, contained in a blockquote element.

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

A well-known quote, contained in a blockquote element.

And the second part of the original quote—like another paragraph—would be placed here as well. This paragraph is a little longer to show how text would wrap within the blockquote styling.

HTML
<blockquote class="blockquote">
  <p>A well-known quote, contained in a blockquote element.</p>
  <p>And the second part of the original quote—like another paragraph—would be placed here as well. This paragraph is a little longer to show how text would wrap within the blockquote styling.</p>
</blockquote>

来源

HTML 规范要求引用块的归属信息放在 <blockquote> 外部。在这种情况下,使用 <figure> 元素,并将 .blockquote 类放在其上而不是 <blockquote> 元素上。这确保可以在引用块和归属元素之间访问 CSS 变量。

对于来源归属,使用 <figcaption> 或带有 .blockquote-footer 类的块级元素(例如 <p>)。同时将任何来源作品的名称包裹在 <cite> 中。

A well-known quote, contained in a blockquote element.

HTML
<figure class="blockquote">
  <blockquote>
    <p>A well-known quote, contained in a blockquote element.</p>
  </blockquote>
  <figcaption class="blockquote-footer">
    出自某位名人的 <cite title="Source Title">来源标题</cite>
  </figcaption>
</figure>

嵌套

A well-known quote, contained in a blockquote element.

A well-known quote, contained in a blockquote element.

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

对齐

根据需要,使用文本或 Flex 布局工具类更改引用块的对齐方式。

A well-known quote, contained in a blockquote element.

HTML
<figure class="blockquote text-center">
  <blockquote>
    <p>A well-known quote, contained in a blockquote element.</p>
  </blockquote>
  <figcaption class="blockquote-footer">
    出自某位名人的 <cite title="Source Title">来源标题</cite>
  </figcaption>
</figure>

A well-known quote, contained in a blockquote element.

HTML
<figure class="blockquote align-items-end">
  <blockquote>
    <p>A well-known quote, contained in a blockquote element.</p>
  </blockquote>
  <figcaption class="blockquote-footer">
    出自某位名人的 <cite title="Source Title">来源标题</cite>
  </figcaption>
</figure>

列表

无样式

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

  • This is a list.
  • It appears completely unstyled.
  • Structurally, it’s still a list.
  • However, this style only applies to immediate child elements.
  • Nested lists:
    • are unaffected by this style
    • will still show a bullet
    • and have appropriate left margin
  • This may still come in handy in some situations.
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>

行内

通过组合两个类 .list-inline 和 .list-inline-item,移除列表的项目符号并应用一些轻微的 margin。

  • This is a list item.
  • And another one.
  • But they’re displayed inline.
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>

描述列表对齐

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

Description lists
A description list is perfect for defining terms.
Term

Definition for the term.

And some more placeholder definition text.

Another term
This definition is short, so no extra paragraphs or anything.
Truncated term is truncated
This can be useful when space is tight. Adds an ellipsis at the end.
Nesting
Nested definition list
I heard you like definition lists. Let me put a definition list inside your definition list.
HTML
<dl class="row">
  <dt class="sm:col-3">Description lists</dt>
  <dd class="sm:col-9">A description list is perfect for defining terms.</dd>

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

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

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

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

响应式字号

Bootstrap 使用 CSS 的 clamp() 函数来实现响应式字号,使文本能够在不同设备和视口尺寸下更自然地缩放。

CSS

变量

所有 --font-size-* 和 --line-height-* CSS 自定义属性都定义在 _root.scss 中,由 _config.scss 中的 $font-sizes Sass 映射生成。

CSS
          :root {
  /* Abbreviated list for example purposes… */
  --font-size-xs: 0.75rem;
  --font-size-sm: 0.875rem;
  --font-size-md: 1rem;
  --font-size-lg: 1.25rem;
  --font-size-xl: 1.5rem;
  /* Plus the rest… */
}
        

标题、段落、描述列表和 <legend> 元素——此处及重置中涵盖——使用专用 CSS 变量进行实时自定义,定义在 _reboot.scss 中。

// stylelint-disable-next-line scss/dollar-variable-default
$type-tokens: defaults(
  (
    --heading-margin-bottom: var(--spacer-2),
    --heading-font-family: null,
    --heading-font-style: null,
    --heading-line-height: 1.2,
    --paragraph-margin-bottom: var(--spacer),
    --legend-margin-bottom: var(--spacer-2),
    --legend-font-size: 1.5rem,
    --legend-font-weight: null,
    --list-padding-x: var(--spacer-7),
  ),
  $type-tokens
);

Sass 映射

_config.scss 中的 $font-sizes 映射定义了 Bootstrap 的字号比例。每个键(例如 xs、md、3xl)都包含一个嵌套映射,其中有 font-size 和 line-height 值。较大的尺寸使用 clamp() 进行响应式缩放。

$font-sizes: ();
// stylelint-disable-next-line scss/dollar-variable-default
$font-sizes: defaults(
  (
    "xs": (
      "font-size": .75rem,
      "line-height": 1.25
    ),
    "sm": (
      "font-size": .875rem,
      "line-height": 1.5
    ),
    "md": (
      "font-size": 1rem,
      "line-height": 1.5
    ),
    "lg": (
      "font-size": clamp(1.125rem, 1rem + .625vw, 1.25rem),
      "line-height": 1.5
    ),
    "xl": (
      "font-size": clamp(1.5rem, 1.1rem + .75vw, 1.75rem),
      "line-height": calc(2.5 / 1.75)
    ),
    "2xl": (
      "font-size": clamp(1.75rem, 1.3rem + 1vw, 2rem),
      "line-height": calc(3 / 2.25)
    ),
    "3xl": (
      "font-size": clamp(2rem, 1.5rem + 1.875vw, 2.5rem),
      "line-height": 1.2
    ),
    "4xl": (
      "font-size": clamp(2.25rem, 1.75rem + 2.5vw, 3rem),
      "line-height": 1.1
    ),
    "5xl": (
      "font-size": clamp(3rem, 2rem + 5vw, 4rem),
      "line-height": 1.1
    ),
    "6xl": (
      "font-size": clamp(3.75rem, 2.5rem + 6.25vw, 5rem),
      "line-height": 1
    ),
  ),
  $font-sizes
);

你可以覆盖单个键、替换整个映射,或通过将键设置为 null 来移除它们:

SCSS
          // Per-key override
@use "bootstrap/scss/config" with (
  $font-sizes: ("xs": ("font-size": .625rem, "line-height": 1.25))
);

// Remove a key
@use "bootstrap/scss/config" with (
  $font-sizes: ("6xl": null)
);
        

Sass 循环

字号映射用于在 _root.scss 中生成 --font-size-* 和 --line-height-* CSS 自定义属性,以及在 _utilities.scss 中生成 .fs-* 和 .text-* 工具类。

// Generate font-size and line-height tokens
@each $name, $props in $font-sizes {
  $root-tokens: map.set($root-tokens, --font-size-#{$name}, map.get($props, "font-size"));
  $root-tokens: map.set($root-tokens, --line-height-#{$name}, map.get($props, "line-height"));
}
"font-size": (
  property: font-size,
  class: fs,
  values: map-get-nested($font-sizes, "font-size")
),
"text-size": (
  property: (
    "font-size": 1rem,
    "line-height": 1.5
  ),
  class: text,
  values: $font-sizes
),