Bootstrap4 文字排版
核对 Bootstrap 4 全局默认字体排版基线、h1–h6 与 .display-1..4 标题层级、语义行内/块级元素样式,以及 18 项常用排版工具类的计算样式实测。
学完这一章,你能够
- 核对 Bootstrap 4 全局默认字体排版基线、h1–h6 与 .display-1..4 标题层级、语义行内/块级元素样式,以及 18 项常用排版工具类的计算样式实测。
全局默认排版设置与标题层级
Bootstrap 4 全局默认正文 `font-size` 为 `16px`(`1rem`)、`line-height` 为 `1.5`(实测 `24px`),默认字体栈以 `"Helvetica Neue", Helvetica, Arial, sans-serif`(及系统无衬线字体回退)为主;所有 `<p>` 段落统一重置为 `margin-top: 0`、`margin-bottom: 1rem`(`16px`)。
标准标题 `<h1>` 至 `<h6>` 依次对应 `2.5rem`(`40px`)、`2rem`(`32px`)、`1.75rem`(`28px`)、`1.5rem`(`24px`)、`1.25rem`(`20px`)与 `1rem`(`16px`)。需要更醒目的展示型大标题时,可使用 `.display-1` 至 `.display-4`,本站实测计算字号依次为 `96px`、`88px`、`72px`、`56px` 且字重均为细体 `300`。`<small>` 元素常用于标题内创建字号更小、颜色更浅的副标题:Bootstrap 4 样式规则将 `small` / `.small` 设为父字号的 `80%`(实测 `40px` `<h1>` 内的 `<small>` 为 `32px`、浅灰色 `rgb(108, 117, 125)`,`16px` 正文上的 `.small` 为 `12.8px`;部分旧版资料仍沿用 Bootstrap 3 的 85% 描述,实际以 Bootstrap 4 的 80% 规则为准)。
语义标注、引用与描述列表样式
`<mark>` 用于行内高亮文本,实测渲染淡黄背景 `rgb(252, 248, 227)`(`#fcf8e3`)与 `0.2em` 内边距;带 `title` 属性的 `<abbr>` 会呈现底部虚线装饰(`text-decoration-style: dotted`)并将鼠标指针切换为帮助样式 `cursor: help`。
引用外部内容时在 `<blockquote>` 上添加 `.blockquote`(实测字号 `20px`),并用 `<footer class="blockquote-footer">` 注明出处(实测浅灰色 `rgb(108, 117, 125)` 并通过 `::before` 自动前置破折号)。描述列表 `<dl>` 会自动加粗术语 `<dt>`(`font-weight: 700`)并为说明项 `<dd>` 设置 `8px` 下外边距。
代码片段、键盘按键与多行预格式化块
行内代码 `<code>` 使用等宽字体并呈现洋红色 `rgb(232, 62, 140)`(`14px`);键盘快捷键 `<kbd>` 渲染深色底 `rgb(33, 37, 41)`、白字 `rgb(255, 255, 255)` 与圆角内边距;多行代码 `<pre>` 保留原始空格与换行(`white-space: pre`)。当代码块较长时,附加 `.pre-scrollable` 可将最大高度限制为 `340px` 并开启纵向滚动条(实测 35 行文本时 `scrollHeight > clientHeight` 且 `overflow-y: scroll`)。
更多排版工具类汇总与实测
Bootstrap 4 还提供了一整套覆盖字重、斜体、引导段落、对齐、折行、大小写转换与列表排版的工具类。需特别注意:`.list-unstyled` 仅移除当前列表直接子项的项目符号与左内边距,不会影响嵌套子列表(本站实测外层 `<ul>` 为 `list-style-type: none`,而内嵌 `<ul>` 仍保留 `circle` 符号);`.list-inline` 需与子项 `.list-inline-item` 配合方可让全部列表项水平排列在同一行。
Bootstrap 4 常用排版工具类速查表
| 类名 | 排版作用与注意事项 | 离线实测计算样式 |
| .font-weight-bold | 设置加粗文本 | font-weight: 700 |
| .font-weight-normal | 恢复普通字重文本 | font-weight: 400 |
| .font-weight-light | 设置更细字重文本 | font-weight: 300 |
| .font-italic | 设置斜体文本 | font-style: italic |
| .lead | 突出显示导语或重点段落 | font-size: 20px; font-weight: 300 |
| .small | 缩小为更小字号文本(Bootstrap 4 规则为父级 80%,旧版资料常记作 85%) | font-size: 12.8px (16px 父级下 80%) |
| .text-left | 文本左对齐 | text-align: left |
| .text-center | 文本居中对齐 | text-align: center |
| .text-right | 文本右对齐 | text-align: right |
| .text-justify | 两端对齐,超出容器宽度自动换行 | text-align: justify |
| .text-nowrap | 禁止段落自动折行,超出部分保持单行 | white-space: nowrap |
| .text-lowercase | 将英文字母全部转为小写显示 | text-transform: lowercase |
| .text-uppercase | 将英文字母全部转为大写显示 | text-transform: uppercase |
| .text-capitalize | 将英文单词首字母转为大写 | text-transform: capitalize |
| .initialism | 配合 <abbr> 以稍小字号并转为全大写展示缩写词 | font-size: 14.4px (90%); uppercase |
| .list-unstyled | 移除直接子列表项默认项目符号与左内边距;嵌套列表需单独再加此类 | 外层 list-style: none,嵌套子列表仍保持 circle |
| .list-inline | 配合 .list-inline-item 将所有列表项横向置于同一行 | 三项 top 坐标一致、同行排布 |
| .pre-scrollable | 限制 <pre> 最大高度 340px,超出后显示 Y 轴滚动条 | max-height: 340px; overflow-y: scroll |
离线验证 Bootstrap 4 默认排版基线、标题/语义元素及 18 项排版工具类
展开查看收起
"""Offline Playwright Chromium lab for Bootstrap 4 Ch 04 (typography)."""
import json
from playwright.sync_api import sync_playwright
BS4_TYPO_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body {
margin: 0;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #212529;
background-color: #fff;
}
p { margin-top: 0; margin-bottom: 1rem; }
h1, h2, h3, h4, h5, h6 { margin-top: 0; margin-bottom: 0.5rem; font-weight: 500; line-height: 1.2; }
h1 { font-size: 2.5rem; }
h2 { font-size: 2rem; }
h3 { font-size: 1.75rem; }
h4 { font-size: 1.5rem; }
h5 { font-size: 1.25rem; }
h6 { font-size: 1rem; }
.display-1 { font-size: 6rem; font-weight: 300; line-height: 1.2; }
.display-2 { font-size: 5.5rem; font-weight: 300; line-height: 1.2; }
.display-3 { font-size: 4.5rem; font-weight: 300; line-height: 1.2; }
.display-4 { font-size: 3.5rem; font-weight: 300; line-height: 1.2; }
small, .small { font-size: 80%; font-weight: 400; }
h1 small { color: #6c757d; }
mark, .mark { padding: 0.2em; background-color: #fcf8e3; }
abbr[title] { text-decoration: underline dotted; cursor: help; border-bottom: 0; }
.initialism { font-size: 90%; text-transform: uppercase; }
.blockquote { margin-bottom: 1rem; font-size: 1.25rem; }
.blockquote-footer { display: block; font-size: 80%; color: #6c757d; }
.blockquote-footer::before { content: "\\2014\\00A0"; }
dl { margin-top: 0; margin-bottom: 1rem; }
dt { font-weight: 700; }
dd { margin-bottom: 0.5rem; margin-left: 0; }
code { font-size: 87.5%; color: #e83e8c; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; }
kbd { padding: 0.2rem 0.4rem; font-size: 87.5%; color: #fff; background-color: #212529; border-radius: 0.2rem; font-family: monospace; }
pre { display: block; font-size: 87.5%; color: #212529; font-family: monospace; white-space: pre; }
.pre-scrollable { max-height: 340px; overflow-y: scroll; }
.font-weight-bold { font-weight: 700 !important; }
.font-weight-normal { font-weight: 400 !important; }
.font-weight-light { font-weight: 300 !important; }
.font-italic { font-style: italic !important; }
.lead { font-size: 1.25rem; font-weight: 300; }
.text-left { text-align: left !important; }
.text-center { text-align: center !important; }
.text-right { text-align: right !important; }
.text-justify { text-align: justify !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; margin-right: 0.5rem; }
"""
LONG_PRE = "\n".join(f"line {i:02d}: preserved spaces" for i in range(1, 36))
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{BS4_TYPO_CSS}</style></head>
<body>
<p id="p-default">默认段落</p>
<h1 id="h1">H1 <small id="h1-small">副标题</small></h1>
<h2 id="h2">H2</h2><h3 id="h3">H3</h3><h4 id="h4">H4</h4><h5 id="h5">H5</h5><h6 id="h6">H6</h6>
<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>
<p>演示 <mark id="mk">高亮</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="bq-ft" class="blockquote-footer">来源说明</footer>
</blockquote>
<dl id="dl-box"><dt id="dt1">Coffee</dt><dd id="dd1">- black hot drink</dd></dl>
<p><code id="cd">div</code> <kbd id="kb">ctrl + p</kbd></p>
<pre id="pr">a b\nc</pre>
<pre id="pr-scroll" class="pre-scrollable">{LONG_PRE}</pre>
<p id="u-bold" class="font-weight-bold">bold</p>
<p id="u-norm" class="font-weight-normal">normal</p>
<p id="u-light" class="font-weight-light">light</p>
<p id="u-ital" class="font-italic">italic</p>
<p id="u-lead" class="lead">lead</p>
<p id="u-small" class="small">small</p>
<p id="u-left" class="text-left">left</p>
<p id="u-center" class="text-center">center</p>
<p id="u-right" class="text-right">right</p>
<p id="u-just" class="text-justify">justify</p>
<p id="u-nowrap" class="text-nowrap">nowrap text</p>
<p id="u-lower" class="text-lowercase">ABC</p>
<p id="u-upper" class="text-uppercase">abc</p>
<p id="u-cap" class="text-capitalize">hello world</p>
<ul id="ul-unsty" class="list-unstyled">
<li>A</li>
<li>B<ul id="ul-nested"><li>Nested</li></ul></li>
</ul>
<ul id="ul-inline" class="list-inline">
<li id="li-in1" class="list-inline-item">项1</li>
<li id="li-in2" class="list-inline-item">项2</li>
<li id="li-in3" class="list-inline-item">项3</li>
</ul>
</body>
</html>
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 1000, "height": 900})
page.set_content(HTML_PAGE)
# Case 1: body_defaults_and_headings
res1 = page.evaluate("""() => {
const bs = getComputedStyle(document.body);
const ps = getComputedStyle(document.getElementById('p-default'));
const hs = ['h1','h2','h3','h4','h5','h6'].map(id => parseFloat(getComputedStyle(document.getElementById(id)).fontSize));
return {
body_font_size: parseFloat(bs.fontSize),
body_line_height: parseFloat(bs.lineHeight),
p_margins: [parseFloat(ps.marginTop), parseFloat(ps.marginBottom)],
heading_px: hs,
};
}""")
expected_1 = {
"body_font_size": 16.0,
"body_line_height": 24.0,
"p_margins": [0.0, 16.0],
"heading_px": [40.0, 32.0, 28.0, 24.0, 20.0, 16.0],
}
cases.append(dict(name="body_defaults_and_headings", passed=(res1 == expected_1), expected=expected_1, actual=res1))
# Case 2: display_headings_and_small
res2 = page.evaluate("""() => {
const ds = ['d1','d2','d3','d4'].map(id => {
const s = getComputedStyle(document.getElementById(id));
return { px: parseFloat(s.fontSize), weight: s.fontWeight };
});
const sm = getComputedStyle(document.getElementById('h1-small'));
const usm = getComputedStyle(document.getElementById('u-small'));
return {
displays: ds,
h1_small_px: parseFloat(sm.fontSize),
h1_small_color: sm.color,
utility_small_px: parseFloat(usm.fontSize),
};
}""")
expected_2 = {
"displays": [
{"px": 96.0, "weight": "300"},
{"px": 88.0, "weight": "300"},
{"px": 72.0, "weight": "300"},
{"px": 56.0, "weight": "300"},
],
"h1_small_px": 32.0,
"h1_small_color": "rgb(108, 117, 125)",
"utility_small_px": 12.8,
}
cases.append(dict(name="display_headings_and_small", passed=(res2 == expected_2), expected=expected_2, actual=res2))
# Case 3: semantic_mark_abbr_blockquote_dl
res3 = page.evaluate("""() => {
const mk = getComputedStyle(document.getElementById('mk'));
const ab = getComputedStyle(document.getElementById('ab'));
const bq = getComputedStyle(document.getElementById('bq'));
const ft = getComputedStyle(document.getElementById('bq-ft'));
const dt = getComputedStyle(document.getElementById('dt1'));
const dd = getComputedStyle(document.getElementById('dd1'));
return {
mark_bg: mk.backgroundColor,
abbr_cursor: ab.cursor,
abbr_deco_style: ab.textDecorationStyle,
blockquote_px: parseFloat(bq.fontSize),
blockquote_footer_color: ft.color,
dt_weight: dt.fontWeight,
dd_margin_bottom: parseFloat(dd.marginBottom),
};
}""")
expected_3 = {
"mark_bg": "rgb(252, 248, 227)",
"abbr_cursor": "help",
"abbr_deco_style": "dotted",
"blockquote_px": 20.0,
"blockquote_footer_color": "rgb(108, 117, 125)",
"dt_weight": "700",
"dd_margin_bottom": 8.0,
}
cases.append(dict(name="semantic_mark_abbr_blockquote_dl", passed=(res3 == expected_3), expected=expected_3, actual=res3))
# Case 4: code_kbd_pre_and_scrollable
res4 = page.evaluate("""() => {
const cd = getComputedStyle(document.getElementById('cd'));
const kb = getComputedStyle(document.getElementById('kb'));
const pr = getComputedStyle(document.getElementById('pr'));
const scEl = document.getElementById('pr-scroll');
const sc = getComputedStyle(scEl);
return {
code_color: cd.color,
code_px: parseFloat(cd.fontSize),
kbd_bg: kb.backgroundColor,
kbd_color: kb.color,
pre_whitespace: pr.whiteSpace,
scrollable_max_h: sc.maxHeight,
scrollable_overflow_y: sc.overflowY,
scrollable_overflows: scEl.scrollHeight > scEl.clientHeight,
};
}""")
expected_4 = {
"code_color": "rgb(232, 62, 140)",
"code_px": 14.0,
"kbd_bg": "rgb(33, 37, 41)",
"kbd_color": "rgb(255, 255, 255)",
"pre_whitespace": "pre",
"scrollable_max_h": "340px",
"scrollable_overflow_y": "scroll",
"scrollable_overflows": True,
}
cases.append(dict(name="code_kbd_pre_and_scrollable", passed=(res4 == expected_4), expected=expected_4, actual=res4))
# Case 5: typography_utility_classes
res5 = page.evaluate("""() => {
const g = id => getComputedStyle(document.getElementById(id));
const r1 = document.getElementById('li-in1').getBoundingClientRect();
const r2 = document.getElementById('li-in2').getBoundingClientRect();
const r3 = document.getElementById('li-in3').getBoundingClientRect();
return {
weights: [g('u-bold').fontWeight, g('u-norm').fontWeight, g('u-light').fontWeight],
italic: g('u-ital').fontStyle,
lead: [parseFloat(g('u-lead').fontSize), g('u-lead').fontWeight],
aligns: [g('u-left').textAlign, g('u-center').textAlign, g('u-right').textAlign, g('u-just').textAlign],
nowrap: g('u-nowrap').whiteSpace,
transforms: [g('u-lower').textTransform, g('u-upper').textTransform, g('u-cap').textTransform],
initialism: [parseFloat(g('ab-init').fontSize), g('ab-init').textTransform],
list_unstyled: g('ul-unsty').listStyleType,
list_nested_keeps_disc: g('ul-nested').listStyleType,
list_inline_same_row: Math.round(r1.top) === Math.round(r2.top) && Math.round(r2.top) === Math.round(r3.top),
};
}""")
expected_5 = {
"weights": ["700", "400", "300"],
"italic": "italic",
"lead": [20.0, "300"],
"aligns": ["left", "center", "right", "justify"],
"nowrap": "nowrap",
"transforms": ["lowercase", "uppercase", "capitalize"],
"initialism": [14.4, "uppercase"],
"list_unstyled": "none",
"list_nested_keeps_disc": "circle",
"list_inline_same_row": True,
}
cases.append(dict(name="typography_utility_classes", passed=(res5 == expected_5), expected=expected_5, actual=res5))
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
不会。.list-unstyled 仅作用于直接挂载该类的列表及其直接子项,若要清除嵌套子列表的默认符号,必须在内层 <ul> 或 <ol> 上再次添加 .list-unstyled。