蓝色天空网mbsky.com · 知识 · 实践 · 创造Foundation · 本地阅读预览
教程 / 前端开发 / Foundation

Foundation 文本

核对 Foundation 5 的全局字体与行高默认设置、h1-h6 响应式标题字号、.subheader 与 small 副标题,以及 a/abbr/blockquote/dl/code/kbd/hr 全部排版元素的实测计算样式。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

Foundation 全局文字默认设置

Foundation 5 在 `<body>` 上沿用浏览器基准字号(`font-size: 100%`,桌面浏览器通常为 `16px`,部分移动端浏览器默认为 `12px`),首选无衬线字体为 `"Helvetica Neue"`,默认行高 `line-height` 为 `1.5`。本站离线 Chromium 在 `1024px` 视口实测 `body` 计算样式:`font-size: 16px`、`line-height: 24px`(即 `1.5 × 16px`),首字体为 `"Helvetica Neue"`。

标题层级 h1-h6、.subheader 与 <small> 副标题

Foundation 5 为 `<h1>` 至 `<h6>` 内置了响应式字号阶梯:移动端与桌面端按 `40.0625em`(`641px`)断点分档放大。本站离线实测 `<h1>`~`<h6>` 计算字号在 `480px` 视口下依次为 `34px / 27px / 22px / 18px / 18px / 16px`,在 `1024px` 桌面视口下放大为 `44px / 37px / 27px / 23px / 18px / 16px`。

若需要弱化视觉权重的浅色标题,可为标题添加 `.subheader` 类;若要在主标题同行内附加次级说明,则直接在标题内嵌入 HTML `<small>` 元素。本站实测普通 `<h2>` 颜色为深灰 `rgb(34, 34, 34)`,`.subheader` 与 `<small>` 均呈现浅灰色 `rgb(111, 111, 111)`,且 `<h1>` 内的 `<small>` 字号自动缩为父级标题的 `60%`(`26.4px`)。

链接 <a>、缩写 <abbr>、引用 <blockquote> 与定义列表 <dl>

文本行内语义元素在 Foundation 5 中均有清晰视觉区分:链接 `<a>` 默认采用主题青蓝色 `rgb(0, 140, 186)` 且无下划线(`text-decoration-line: none`);缩写 `<abbr>` 配合 `title` 属性使用时,默认转为大写(`text-transform: uppercase`)、字号缩为 `90%`、底部带 `1px dotted` 虚线且悬停光标为 `help`。

块级引用 `<blockquote>` 左侧绘制 `1px solid rgb(221, 221, 221)` 竖线,内部 `<cite>` 以块级元素(`display: block`、`13px`)显示并在 `::before` 伪元素前自动添加长破折号 `— `;定义列表 `<dl>` 中,术语 `<dt>` 加粗(`font-weight: 700`),描述项 `<dd>` 底部保留 `12px`(`0.75rem`)间距以区分条目。

代码 <code>、键盘按键 <kbd> 与水平分割线 <hr>

技术文档常用的行内标记也已预设完整样式:`<code>` 使用等宽字体并配浅灰底色 `rgb(248, 248, 248)` 与 `1px solid rgb(223, 223, 223)` 细边框;`<kbd>` 模拟键盘按键外观,呈现 `rgb(237, 237, 237)` 底色、`3px` 圆角与 `14px` 字号;`<hr>` 通过 `1px solid rgb(221, 221, 221)` 顶边框绘制分割线(在全局 `box-sizing: border-box` 下,CSS 设 `height: 0` 时 Chromium 实测含顶边框的计算高度为 `1px`)。

离线验证 Foundation 5 全局字号、响应式标题、语义标签与代码排版计算样式

python · 阅读源码
"""Offline Playwright Chromium lab for Foundation Ch 03 (typography)."""
import json
from playwright.sync_api import sync_playwright

FOUNDATION_TYPO_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 100%; }
body { margin: 0; padding: 16px; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; font-size: 1rem; line-height: 1.5; color: #222222; background: #ffffff; }
h1, h2, h3, h4, h5, h6 { font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; font-weight: normal; color: #222222; line-height: 1.4; margin-top: 0.2rem; margin-bottom: 0.5rem; }
h1 { font-size: 2.125rem; }
h2 { font-size: 1.6875rem; }
h3 { font-size: 1.375rem; }
h4 { font-size: 1.125rem; }
h5 { font-size: 1.125rem; }
h6 { font-size: 1rem; }
@media only screen and (min-width: 40.0625em) {
  h1 { font-size: 2.75rem; }
  h2 { font-size: 2.3125rem; }
  h3 { font-size: 1.6875rem; }
  h4 { font-size: 1.4375rem; }
  h5 { font-size: 1.125rem; }
  h6 { font-size: 1rem; }
}
.subheader { line-height: 1.4; color: #6f6f6f; font-weight: normal; margin-top: 0.2rem; margin-bottom: 0.5rem; }
h1 small, h2 small, h3 small, h4 small, h5 small, h6 small { font-size: 60%; color: #6f6f6f; line-height: 0; }
a { color: #008CBA; text-decoration: none; line-height: inherit; }
a:hover { color: #0078a0; }
abbr { text-transform: uppercase; font-size: 90%; color: #222222; cursor: help; border-bottom: 1px dotted #dddddd; }
blockquote { margin: 0 0 1.25rem; padding: 0.5625rem 1.25rem 0 1.1875rem; border-left: 1px solid #dddddd; }
blockquote, blockquote p { line-height: 1.6; color: #6f6f6f; }
blockquote cite { display: block; font-size: 0.8125rem; color: #555555; }
blockquote cite::before { content: "\\2014 \\0020"; }
dl dt { margin-bottom: 0.3rem; font-weight: bold; }
dl dd { margin-bottom: 0.75rem; margin-left: 0; }
code { font-family: Consolas, "Liberation Mono", Courier, monospace; font-weight: normal; color: #333333; background-color: #f8f8f8; border: 1px solid #dfdfdf; padding: 0.125rem 0.3125rem 0.0625rem; }
kbd { background-color: #ededed; border: 1px solid #dbdbdb; color: #222222; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.875rem; padding: 0.125rem 0.25rem 0; border-radius: 3px; }
hr { border: solid #dddddd; border-width: 1px 0 0; clear: both; height: 0; margin: 1.25rem 0 1.1875rem; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_TYPO_CSS}</style></head>
<body>
  <h1 id="h1">h1 标题</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>
  <h2 id="h2-sub" class="subheader">h2.subheader</h2>
  <h1 id="h1-with-small">h1 heading <small id="h1-small">secondary text</small></h1>
  <p>这是一个 <a id="demo-link" class="a" href="#">链接</a>。</p>
  <p>The <abbr id="demo-abbr" title="World Health Organization">WHO</abbr> was founded in 1948.</p>
  <blockquote id="demo-quote">
    <p id="quote-p">学以致用,夯实前端排版基础。</p>
    <cite id="quote-cite">蓝色天空网</cite>
  </blockquote>
  <dl id="demo-dl">
    <dt id="dl-dt">Coffee</dt>
    <dd id="dl-dd">- black hot drink</dd>
    <dt>Milk</dt>
    <dd>- white cold drink</dd>
  </dl>
  <p>元素示例: <code id="demo-code">span</code>, <code>section</code>, <code>div</code></p>
  <p>快捷键: <kbd id="demo-kbd">ctrl + p</kbd></p>
  <hr id="demo-hr">
</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": 1024, "height": 900})
        page.set_content(HTML_PAGE)

        # Case 1: body defaults and h1-h6 desktop vs mobile sizing
        desktop_sizes = page.evaluate("""() => {
            const b = getComputedStyle(document.body);
            const hs = ['h1','h2','h3','h4','h5','h6'].map(id => getComputedStyle(document.getElementById(id)).fontSize);
            return { body_font_size: b.fontSize, body_line_height: b.lineHeight, body_font_first: b.fontFamily.split(',')[0].trim(), desktop_headings: hs };
        }""")
        page.set_viewport_size({"width": 480, "height": 900})
        mobile_sizes = page.evaluate("""() => ['h1','h2','h3','h4','h5','h6'].map(id => getComputedStyle(document.getElementById(id)).fontSize)""")
        page.set_viewport_size({"width": 1024, "height": 900})
        actual_1 = {**desktop_sizes, "mobile_headings": mobile_sizes}
        expected_1 = {
            "body_font_size": "16px",
            "body_line_height": "24px",
            "body_font_first": '"Helvetica Neue"',
            "desktop_headings": ["44px", "37px", "27px", "23px", "18px", "16px"],
            "mobile_headings": ["34px", "27px", "22px", "18px", "18px", "16px"],
        }
        cases.append({
            "name": "body_defaults_and_responsive_headings",
            "expected": expected_1,
            "actual": actual_1,
            "passed": actual_1 == expected_1,
        })

        # Case 2: .subheader and heading <small> secondary text
        actual_2 = page.evaluate("""() => {
            const h2 = getComputedStyle(document.getElementById('h2'));
            const sub = getComputedStyle(document.getElementById('h2-sub'));
            const sm = getComputedStyle(document.getElementById('h1-small'));
            return {
                normal_h2_color: h2.color,
                subheader_color: sub.color,
                small_color: sm.color,
                small_font_size: sm.fontSize,
                small_ratio_to_h1: Number((parseFloat(sm.fontSize) / 44).toFixed(2))
            };
        }""")
        expected_2 = {
            "normal_h2_color": "rgb(34, 34, 34)",
            "subheader_color": "rgb(111, 111, 111)",
            "small_color": "rgb(111, 111, 111)",
            "small_font_size": "26.4px",
            "small_ratio_to_h1": 0.6,
        }
        cases.append({
            "name": "subheader_and_small_secondary_heading",
            "expected": expected_2,
            "actual": actual_2,
            "passed": actual_2 == expected_2,
        })

        # Case 3: <a> link & <abbr> abbreviation
        actual_3 = page.evaluate("""() => {
            const a = getComputedStyle(document.getElementById('demo-link'));
            const abbr = getComputedStyle(document.getElementById('demo-abbr'));
            return {
                link_color: a.color,
                link_decoration: a.textDecorationLine,
                abbr_cursor: abbr.cursor,
                abbr_border_bottom: abbr.borderBottomWidth + ' ' + abbr.borderBottomStyle,
                abbr_text_transform: abbr.textTransform
            };
        }""")
        expected_3 = {
            "link_color": "rgb(0, 140, 186)",
            "link_decoration": "none",
            "abbr_cursor": "help",
            "abbr_border_bottom": "1px dotted",
            "abbr_text_transform": "uppercase",
        }
        cases.append({
            "name": "link_and_abbr_typography",
            "expected": expected_3,
            "actual": actual_3,
            "passed": actual_3 == expected_3,
        })

        # Case 4: <blockquote> + <cite> and <dl>/<dt>/<dd>
        actual_4 = page.evaluate("""() => {
            const bq = getComputedStyle(document.getElementById('demo-quote'));
            const cite = getComputedStyle(document.getElementById('quote-cite'));
            const citeBefore = getComputedStyle(document.getElementById('quote-cite'), '::before').content;
            const dt = getComputedStyle(document.getElementById('dl-dt'));
            const dd = getComputedStyle(document.getElementById('dl-dd'));
            return {
                blockquote_border_left: bq.borderLeftWidth + ' ' + bq.borderLeftStyle + ' ' + bq.borderLeftColor,
                cite_display: cite.display,
                cite_font_size: cite.fontSize,
                cite_prefix_has_emdash: citeBefore.includes('\\u2014'),
                dt_font_weight: dt.fontWeight,
                dd_margin_bottom: dd.marginBottom
            };
        }""")
        expected_4 = {
            "blockquote_border_left": "1px solid rgb(221, 221, 221)",
            "cite_display": "block",
            "cite_font_size": "13px",
            "cite_prefix_has_emdash": True,
            "dt_font_weight": "700",
            "dd_margin_bottom": "12px",
        }
        cases.append({
            "name": "blockquote_cite_and_definition_list",
            "expected": expected_4,
            "actual": actual_4,
            "passed": actual_4 == expected_4,
        })

        # Case 5: <code>, <kbd>, <hr>
        actual_5 = page.evaluate("""() => {
            const c = getComputedStyle(document.getElementById('demo-code'));
            const k = getComputedStyle(document.getElementById('demo-kbd'));
            const hr = getComputedStyle(document.getElementById('demo-hr'));
            return {
                code_bg: c.backgroundColor,
                code_border: c.borderTopWidth + ' ' + c.borderTopStyle + ' ' + c.borderTopColor,
                kbd_bg: k.backgroundColor,
                kbd_radius: k.borderRadius,
                kbd_font_size: k.fontSize,
                hr_border_top: hr.borderTopWidth + ' ' + hr.borderTopStyle + ' ' + hr.borderTopColor,
                hr_height: hr.height
            };
        }""")
        expected_5 = {
            "code_bg": "rgb(248, 248, 248)",
            "code_border": "1px solid rgb(223, 223, 223)",
            "kbd_bg": "rgb(237, 237, 237)",
            "kbd_radius": "3px",
            "kbd_font_size": "14px",
            "hr_border_top": "1px solid rgb(221, 221, 221)",
            "hr_height": "1px",
        }
        cases.append({
            "name": "code_kbd_and_hr_elements",
            "expected": expected_5,
            "actual": actual_5,
            "passed": actual_5 == expected_5,
        })

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


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 在离线 Chromium 中核对 body 16px/24px 默认值、h1-h6 在 480px 与 1024px 下的字号阶梯、.subheader/small 灰色样式、a/abbr/blockquote/dl/code/kbd/hr 的真实计算样式。
离线验证 Foundation 5 全局字号、响应式标题、语义标签与代码排版计算样式

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

当前源码 SHA256:441b564649c8ff27fc2820c5a6d4f342808da79d4961eae316af73494f546d92

下载本站示例源码

本地运行

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

下载Foundation 文本实验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

ZIP SHA256:15e4a1d784cdbfb06da6352b985da514d72f9e55beb0c6fd69641430ec74293c

常见错误与边界

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

练习与解答

在 Foundation 5 中,标题添加 .subheader 类与在标题内部嵌套 <small> 标签在视觉与结构上有何区别?

展开参考解答

.subheader 作用于整个标题元素,将整行标题变为浅灰色(#6f6f6f)但保持原有字号;<small> 嵌套在主标题内部,只将其包裹的副标题文字变为浅灰色并自动缩小为主标题字号的 60%,实现同一行内的主副标题层次区分。

规范与官方依据