蓝色天空.知识 · 实践 · 创造
我的收藏
Bootstrap5 教程目录 38章
Bootstrap5 / LEARN BY DOING

Bootstrap5 文字排版

核对 Bootstrap 5 默认字体与段落间距、h1–h6 及 .h1–.h6、Display 大标题、行内与块级语义标签(small/mark/abbr/blockquote/dl/code/kbd/pre)及常用文本排版工具类。

学完这一章,你能够

  • 核对 Bootstrap 5 默认字体与段落间距、h1–h6 及 .h1–.h6、Display 大标题、行内与块级语义标签(small/mark/abbr/blockquote/dl/code/kbd/pre)及常用文本排版工具类。

全局默认排版、标题等级与 Display 大标题

Bootstrap 5 全局默认正文 `font-size` 为 `1rem`(常规根字号下 `16px`),`line-height` 为 `1.5`(计算行高 `24px`);无衬线字体栈在经典配置中以 `"Helvetica Neue", Helvetica, Arial, sans-serif` 为兜底,官方 Bootstrap 5 默认进一步优先采用 `system-ui` 原生系统无衬线字体栈;所有 `<p>` 段落均重置为 `margin-top: 0`、`margin-bottom: 1rem`(`16px`)。

HTML 标题标签 `<h1>` 到 `<h6>` 均内置了统一字号与行高规范;若希望普通段落等非标题元素呈现相同视觉层级而不破坏语义大纲,可直接使用 `.h1` 到 `.h6` 类。本站离线 Chromium 实测(`1200px` 视口)确认 `h1`–`h6` 与 `.h1`–`.h6` 计算字号完全一致,依次为 `40px`、`32px`、`28px`、`24px`、`20px`、`16px`。

需要更醒目的展示型大标题时,可使用 Display 标题类。基础教程重点展示了 `.display-1` 至 `.display-4` 四个常用级别,而 Bootstrap 5 完整规范实际扩展至 6 级(补充了 `.display-5` 与 `.display-6`),统一采用较细字重(`font-weight: 300`)与更紧凑行高(`1.2`)。本站实测 `.display-1` 至 `.display-6` 字号依次为 `80px`、`72px`、`64px`、`56px`、`48px`、`40px`。

行内语义标签:small、mark 与 abbr

针对行内补充与强调,Bootstrap 5 定制了三类常用标签:`<small>`(或 `.small`)以 `0.875em` 相对父元素缩小字号,常用于副标题或注释(在 `16px` 正文中为 `14px`,嵌套于 `40px` 的 `h1` 中按比例缩至 `35px`);`<mark>`(或 `.mark`)以浅黄底色与内边距高亮关键词;带 `title` 属性的 `<abbr>` 呈现底部虚线装饰与 `help` 问号光标,搭配 `.initialism` 时进一步以小号大写呈现。本站实测 `<mark>` 背景为 `rgb(252, 248, 227)`,`<abbr>` 装饰样式为 `dotted`、光标 `help`,`.initialism` 计算为 `14px` 与 `uppercase`。

块级引用、描述列表与代码排版元素

引用长段内容时,为 `<blockquote>` 添加 `.blockquote`(字号提升至 `20px`),来源说明使用 `.blockquote-footer`(灰色 `rgb(108, 117, 125)` 并通过 `::before` 自动前置破折号 `— `);描述列表 `<dl>` 保留清晰的术语层级,`<dt>` 默认加粗(`font-weight: 700`)而 `<dd>` 去除多余左缩进并保留底部间距。

技术文档中的代码与按键样式由三类元素分工:`<code>` 以洋红色(`rgb(214, 51, 132)`)等宽字体标示行内代码片段;`<kbd>` 以深色圆角徽章(背景 `rgb(33, 37, 41)`、白字)标示键盘快捷键输入;`<pre>` 则通过 `white-space: pre` 完整保留多行代码的空格缩进与换行。

更多排版与列表工具类

Bootstrap 5 还内置了导语(`.lead`)、逻辑方向对齐(Bootstrap 5 用 `.text-start`/`.text-end` 替代了 Bootstrap 4 的 `.text-left`/`.text-right`)、换行控制、大小写转换及无样式/行内列表类。需要特别提醒:`.text-justify` 已在 Bootstrap 5 正式版中移除,如需两端对齐应自行编写 CSS;此外 `.list-unstyled` 仅作用于直接子项。本站实测 `.lead` 为 `20px / 300`、`.text-start/center/end` 对应 `left/center/right`、`.text-nowrap` 为 `nowrap`、大小写三类准确生效,且 `.list-unstyled` 内嵌套子列表仍保持默认圆点标记,`.list-inline` 与 `.list-inline-item` 令列表项水平同行排列。

Bootstrap 5 常用排版工具类速查表

类名作用说明实测要点与版本提示
.lead突出显示导语或重点段落font-size: 20px (1.25rem),font-weight: 300
.small创建相对于父元素更小的文本(约 0.875em / 85% 级)在 16px 正文中实测计算字号 14px
.text-start文本起始端对齐(LTR 下左对齐)Bootstrap 5 取代 .text-left,实测 text-align: left
.text-center文本水平居中对齐实测 text-align: center
.text-end文本末端对齐(LTR 下右对齐)Bootstrap 5 取代 .text-right,实测 text-align: right
.text-justify历史类名(原用于段落两端对齐)Bootstrap 5 官方已移除该类,需两端对齐请改用自定义 CSS
.text-nowrap禁止段落内文本自动折行实测 white-space: nowrap
.text-lowercase将英文字母全部转为小写实测 text-transform: lowercase
.text-uppercase将英文字母全部转为大写实测 text-transform: uppercase
.text-capitalize将每个英文单词首字母转为大写实测 text-transform: capitalize
.initialism配合 <abbr> 以小号全大写展示缩略词实测 14px 且 text-transform: uppercase
.list-unstyled移除 <ul>/<ol> 默认项目符号与左内边距(仅限直接子项)外层 list-style-type: none,嵌套子列表保留默认符号
.list-inline配合子项 .list-inline-item 将列表项横向排在同一行实测相邻列表项 top 相同、水平依次排列

离线 Chromium 实测 Bootstrap 5 标题层级、Display、行内/块级语义标签与排版工具类

含完整源码 · 264 行
展开查看收起
python
"""Offline Playwright Chromium lab for Bootstrap 5 Chapter 05 (typography)."""
import json
from playwright.sync_api import sync_playwright

BS5_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  color: #212529;
}
p { margin-top: 0; margin-bottom: 1rem; }
h1, .h1, h2, .h2, h3, .h3, h4, .h4, h5, .h5, h6, .h6 {
  margin-top: 0; margin-bottom: 0.5rem; font-weight: 500; line-height: 1.2;
}
h1, .h1 { font-size: 2.5rem; }
h2, .h2 { font-size: 2rem; }
h3, .h3 { font-size: 1.75rem; }
h4, .h4 { font-size: 1.5rem; }
h5, .h5 { font-size: 1.25rem; }
h6, .h6 { font-size: 1rem; }
.display-1 { font-size: 5rem; font-weight: 300; line-height: 1.2; }
.display-2 { font-size: 4.5rem; font-weight: 300; line-height: 1.2; }
.display-3 { font-size: 4rem; font-weight: 300; line-height: 1.2; }
.display-4 { font-size: 3.5rem; font-weight: 300; line-height: 1.2; }
.display-5 { font-size: 3rem; font-weight: 300; line-height: 1.2; }
.display-6 { font-size: 2.5rem; font-weight: 300; line-height: 1.2; }
small, .small { font-size: 0.875em; }
mark, .mark { padding: 0.2em; background-color: #fcf8e3; }
abbr[title] { text-decoration: underline dotted; cursor: help; }
.initialism { font-size: 0.875em; text-transform: uppercase; }
.blockquote { margin-bottom: 1rem; font-size: 1.25rem; }
.blockquote-footer { margin-top: -1rem; margin-bottom: 1rem; font-size: 0.875em; color: #6c757d; }
.blockquote-footer::before { content: "\\2014\\00A0"; }
dt { font-weight: 700; }
dd { margin-bottom: 0.5rem; margin-left: 0; }
code { font-size: 0.875em; color: #d63384; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; }
kbd { padding: 0.2rem 0.4rem; font-size: 0.875em; color: #fff; background-color: #212529; border-radius: 0.2rem; }
pre { display: block; margin-top: 0; margin-bottom: 1rem; overflow: auto; font-size: 0.875em; white-space: pre; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; }
.lead { font-size: 1.25rem; font-weight: 300; }
.text-start { text-align: left !important; }
.text-center { text-align: center !important; }
.text-end { text-align: right !important; }
.text-nowrap { white-space: nowrap !important; }
.text-lowercase { text-transform: lowercase !important; }
.text-uppercase { text-transform: uppercase !important; }
.text-capitalize { text-transform: capitalize !important; }
.list-unstyled { padding-left: 0; list-style: none; }
.list-inline { padding-left: 0; list-style: none; }
.list-inline-item { display: inline-block; }
.list-inline-item:not(:last-child) { margin-right: 0.5rem; }
"""

HTML_DOC = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{BS5_CSS}</style></head>
<body>
  <p id="p-def">默认段落</p>
  <h1 id="t-h1">H1 <small id="h1-small">sub</small></h1>
  <h2 id="t-h2">H2</h2><h3 id="t-h3">H3</h3><h4 id="t-h4">H4</h4><h5 id="t-h5">H5</h5><h6 id="t-h6">H6</h6>
  <p id="c-h1" class="h1">p.h1</p><p id="c-h2" class="h2">p.h2</p><p id="c-h3" class="h3">p.h3</p>
  <p id="c-h4" class="h4">p.h4</p><p id="c-h5" class="h5">p.h5</p><p id="c-h6" class="h6">p.h6</p>
  <h1 id="d1" class="display-1">D1</h1><h1 id="d2" class="display-2">D2</h1>
  <h1 id="d3" class="display-3">D3</h1><h1 id="d4" class="display-4">D4</h1>
  <h1 id="d5" class="display-5">D5</h1><h1 id="d6" class="display-6">D6</h1>

  <p><small id="sm-p">small</small> <mark id="mk">mark</mark>
     <abbr id="ab" title="World Health Organization">WHO</abbr>
     <abbr id="ab-init" class="initialism" title="HyperText Markup Language">html</abbr></p>

  <blockquote id="bq" class="blockquote">
    <p>引用正文</p>
    <footer id="bqf" class="blockquote-footer">出处说明</footer>
  </blockquote>
  <dl><dt id="dt1">Coffee</dt><dd id="dd1">black hot drink</dd></dl>
  <p><code id="cd1">div</code> <kbd id="kb1">ctrl + p</kbd></p>
  <pre id="pr1">line 1
  line   2</pre>

  <p id="u-lead" class="lead">导语段落</p>
  <p id="u-start" class="text-start">start</p>
  <p id="u-center" class="text-center">center</p>
  <p id="u-end" class="text-end">end</p>
  <div style="width:80px"><p id="u-nowrap" class="text-nowrap">long text without wrapping</p></div>
  <p id="u-low" class="text-lowercase">ABC</p>
  <p id="u-up" class="text-uppercase">abc</p>
  <p id="u-cap" class="text-capitalize">hello world</p>
  <ul id="u-unstyle" class="list-unstyled">
    <li>A<ul id="u-nested"><li>A1</li></ul></li>
  </ul>
  <ul id="u-inline" class="list-inline">
    <li class="list-inline-item">One</li>
    <li class="list-inline-item">Two</li>
  </ul>
</body>
</html>
"""


def launch_browser(pw):
    try:
        return pw.chromium.launch(headless=True)
    except Exception:
        return pw.chromium.launch(headless=True, channel="chrome")


def main():
    cases = []
    with sync_playwright() as p:
        browser = launch_browser(p)
        page = browser.new_page(viewport={"width": 1200, "height": 900})
        page.set_content(HTML_DOC)

        # Case 1: Defaults, h1-h6 vs .h1-.h6, and .display-1..6
        c1 = page.evaluate("""() => {
            const b = getComputedStyle(document.body);
            const pd = getComputedStyle(document.getElementById('p-def'));
            const tagSizes = [1,2,3,4,5,6].map(i => getComputedStyle(document.getElementById('t-h'+i)).fontSize);
            const clsSizes = [1,2,3,4,5,6].map(i => getComputedStyle(document.getElementById('c-h'+i)).fontSize);
            const dispSizes = [1,2,3,4,5,6].map(i => getComputedStyle(document.getElementById('d'+i)).fontSize);
            return {
                body_font_size: b.fontSize,
                body_line_height: b.lineHeight,
                p_margins: [pd.marginTop, pd.marginBottom],
                heading_sizes: tagSizes,
                heading_class_matches_tag: JSON.stringify(tagSizes) === JSON.stringify(clsSizes),
                display_sizes: dispSizes,
                display_weight: getComputedStyle(document.getElementById('d1')).fontWeight
            };
        }""")
        exp1 = {
            "body_font_size": "16px",
            "body_line_height": "24px",
            "p_margins": ["0px", "16px"],
            "heading_sizes": ["40px", "32px", "28px", "24px", "20px", "16px"],
            "heading_class_matches_tag": True,
            "display_sizes": ["80px", "72px", "64px", "56px", "48px", "40px"],
            "display_weight": "300",
        }
        cases.append({
            "name": "defaults_headings_and_display",
            "passed": c1 == exp1,
            "expected": exp1,
            "actual": c1,
        })

        # Case 2: Inline semantic tags (<small>, <mark>, <abbr>, .initialism)
        c2 = page.evaluate("""() => {
            const sm = getComputedStyle(document.getElementById('sm-p'));
            const h1sm = getComputedStyle(document.getElementById('h1-small'));
            const mk = getComputedStyle(document.getElementById('mk'));
            const ab = getComputedStyle(document.getElementById('ab'));
            const abi = getComputedStyle(document.getElementById('ab-init'));
            return {
                small_px: sm.fontSize,
                h1_small_px: h1sm.fontSize,
                mark_bg: mk.backgroundColor,
                abbr_deco_style: ab.textDecorationStyle,
                abbr_cursor: ab.cursor,
                initialism: [abi.fontSize, abi.textTransform]
            };
        }""")
        exp2 = {
            "small_px": "14px",
            "h1_small_px": "35px",
            "mark_bg": "rgb(252, 248, 227)",
            "abbr_deco_style": "dotted",
            "abbr_cursor": "help",
            "initialism": ["14px", "uppercase"],
        }
        cases.append({
            "name": "inline_semantic_elements",
            "passed": c2 == exp2,
            "expected": exp2,
            "actual": c2,
        })

        # Case 3: Blockquote, dl, code, kbd, pre
        c3 = page.evaluate("""() => {
            const bq = getComputedStyle(document.getElementById('bq'));
            const bqf = getComputedStyle(document.getElementById('bqf'));
            const bqfBefore = getComputedStyle(document.getElementById('bqf'), '::before').content;
            const dt = getComputedStyle(document.getElementById('dt1'));
            const cd = getComputedStyle(document.getElementById('cd1'));
            const kb = getComputedStyle(document.getElementById('kb1'));
            const pr = document.getElementById('pr1');
            return {
                blockquote_size: bq.fontSize,
                footer_color: bqf.color,
                footer_has_emdash: bqfBefore.includes('\\u2014'),
                dt_weight: dt.fontWeight,
                code_color: cd.color,
                kbd_colors: [kb.backgroundColor, kb.color],
                pre_whitespace: getComputedStyle(pr).whiteSpace,
                pre_preserves_multiline: pr.textContent.includes('\\n  line   2')
            };
        }""")
        exp3 = {
            "blockquote_size": "20px",
            "footer_color": "rgb(108, 117, 125)",
            "footer_has_emdash": True,
            "dt_weight": "700",
            "code_color": "rgb(214, 51, 132)",
            "kbd_colors": ["rgb(33, 37, 41)", "rgb(255, 255, 255)"],
            "pre_whitespace": "pre",
            "pre_preserves_multiline": True,
        }
        cases.append({
            "name": "blockquote_dl_code_kbd_pre",
            "passed": c3 == exp3,
            "expected": exp3,
            "actual": c3,
        })

        # Case 4: Additional typography utilities
        c4 = page.evaluate("""() => {
            const inlineItems = [...document.querySelectorAll('#u-inline > li')];
            const r0 = inlineItems[0].getBoundingClientRect();
            const r1 = inlineItems[1].getBoundingClientRect();
            return {
                lead: [getComputedStyle(document.getElementById('u-lead')).fontSize, getComputedStyle(document.getElementById('u-lead')).fontWeight],
                align: [
                    getComputedStyle(document.getElementById('u-start')).textAlign,
                    getComputedStyle(document.getElementById('u-center')).textAlign,
                    getComputedStyle(document.getElementById('u-end')).textAlign
                ],
                nowrap: getComputedStyle(document.getElementById('u-nowrap')).whiteSpace,
                transforms: [
                    getComputedStyle(document.getElementById('u-low')).textTransform,
                    getComputedStyle(document.getElementById('u-up')).textTransform,
                    getComputedStyle(document.getElementById('u-cap')).textTransform
                ],
                list_unstyled: getComputedStyle(document.getElementById('u-unstyle')).listStyleType,
                nested_list_keeps_marker: getComputedStyle(document.getElementById('u-nested')).listStyleType !== 'none',
                inline_same_row: Math.abs(r0.top - r1.top) < 1 && r1.left > r0.left
            };
        }""")
        exp4 = {
            "lead": ["20px", "300"],
            "align": ["left", "center", "right"],
            "nowrap": "nowrap",
            "transforms": ["lowercase", "uppercase", "capitalize"],
            "list_unstyled": "none",
            "nested_list_keeps_marker": True,
            "inline_same_row": True,
        }
        cases.append({
            "name": "more_typography_utilities",
            "passed": c4 == exp4,
            "expected": exp4,
            "actual": c4,
        })

        browser.close()
    print(json.dumps({"cases": cases}, ensure_ascii=False))


if __name__ == "__main__":
    main()
核对 16px/24px 全局默认、h1–h6 与 .h1–.h6 字号一致、.display-1..6、small/mark/abbr/blockquote/dl/code/kbd/pre 及 12 项排版工具类计算样式。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载本章实验包 (mbskycom202612a700.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:Bootstrap5

课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a700.zip

下载课程关联工程包:Bootstrap5

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a736.zip

常见错误与边界检查

  • 实际端口、文档资源与应用配置都应和测试条件一致。

技术规范与官方资料

练习与参考解答

查看参考解答

start/end 属于逻辑方向命名,能自动适配从左到右(LTR)和从右到左(RTL)书写体系;.list-unstyled 仅对当前列表的直接子项生效,若嵌套子列表也需去除圆点与缩进,必须在内层 <ul>/<ol> 上同样添加 .list-unstyled。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习