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

Foundation 标签

核对 Foundation 5 行内标签类 .label 的默认胶囊排版、六种语境色、.radius/.round 圆角及计数徽标写法,并通过离线 Chromium 验证自定义 CSS 字号缩放。

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

学完这一章,你能够

基础标签与六种语境颜色

`.label` 类用于在标题或正文旁附加简洁的状态提示或分类标记(如 `New` 标记)。本站离线 Chromium 实测默认 `<span class="label">` 具有 `display: inline-block`、`white-space: nowrap`、蓝色背景 `rgb(0, 140, 186)`、白色文字 `rgb(255, 255, 255)`、默认字号 `11px`(`0.6875rem`)与 `4px 8px` 内边距,因此嵌入 `<h2>`、`<h3>`、`<h4>` 时不会被父标题撑大,始终保持紧凑一致的视觉尺寸。

与按钮体系一致,标签可通过 `.secondary`、`.success`、`.info`、`.warning` 或 `.alert` 切换语境色。本站离线实测六种配色结果:默认蓝 `rgb(0, 140, 186)`/白字、`.secondary` `rgb(231, 231, 231)`/深灰字 `rgb(51, 51, 51)`、`.success` `rgb(67, 172, 106)`/白字、`.info` `rgb(160, 211, 232)`/深灰字、`.warning` `rgb(240, 138, 36)`/白字、`.alert` `rgb(240, 65, 36)`/白字。(注:编写业务示例时请将 `.warning` 标签文案与警示语义保持一致,避免误写为 Success。)

圆角标签与自定义尺寸

默认标签为直角矩形(`border-radius: 0px`),添加 `.radius` 可切换为 `3px` 圆角,添加 `.round` 则设为 `1000px` 胶囊圆角。将语境色与 `.round` 叠加(如 `<span class="label success round">5</span>`)常用于制作数字计数徽标,本站实测其兼具 `rgb(67, 172, 106)` 绿底与 `1000px` 圆角。

由于 Foundation 5 未为 `.label` 预设独立的 `.large`/`.small` 尺寸类,若需让标签随各级标题比例放大,可直接使用 CSS 覆写 `font-size`。本站离线实测对标签分别设置 `36px`、`30px`、`20px` 与默认 `11px` 后,标签字号精确生效且盒模型高度呈严格递减关系。

离线验证 Foundation 5 标签默认盒模型、六种语境色、圆角徽标与自定义字号缩放

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

FOUNDATION_LABEL_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; padding: 16px; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; color: #222222; }
.label {
  font-weight: normal; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif;
  text-align: center; text-decoration: none; line-height: 1; white-space: nowrap;
  display: inline-block; position: relative; padding: 0.25rem 0.5rem 0.25rem;
  font-size: 0.6875rem; background-color: #008CBA; color: #ffffff; border-radius: 0px;
}
.label.secondary { background-color: #e7e7e7; color: #333333; }
.label.success { background-color: #43AC6A; color: #ffffff; }
.label.info { background-color: #a0d3e8; color: #333333; }
.label.warning { background-color: #f08a24; color: #ffffff; }
.label.alert { background-color: #f04124; color: #ffffff; }
.label.radius { border-radius: 3px; }
.label.round { border-radius: 1000px; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_LABEL_CSS}</style></head>
<body>
  <h2>Example <span id="lbl-h2" class="label">New</span></h2>
  <h3>Example <span id="lbl-h3" class="label">New</span></h3>
  <h4>Example <span id="lbl-h4" class="label">New</span></h4>

  <div id="color-labels">
    <span id="l-default" class="label">Default Label</span>
    <span id="l-secondary" class="label secondary">Secondary Label</span>
    <span id="l-success" class="label success">Success Label</span>
    <span id="l-info" class="label info">Info Label</span>
    <span id="l-warning" class="label warning">Warning Label</span>
    <span id="l-alert" class="label alert">Alert Label</span>
  </div>

  <div id="corner-labels">
    <span id="c-default" class="label">Default Label</span>
    <span id="c-radius" class="label radius">Radius Label</span>
    <span id="c-round" class="label round">Round Label</span>
    <span id="c-badge" class="label success round">5</span>
  </div>

  <div id="size-labels">
    <h1>Example <span id="sz-36" class="label" style="font-size:36px;">New</span></h1>
    <h2>Example <span id="sz-30" class="label" style="font-size:30px;">New</span></h2>
    <h3>Example <span id="sz-20" class="label" style="font-size:20px;">New</span></h3>
    <h4>Example <span id="sz-def" class="label">New</span></h4>
  </div>
</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": 800, "height": 800})
        page.set_content(HTML_PAGE)

        # Case 1: default .label properties inside headings
        actual_1 = page.evaluate("""() => {
            const s = getComputedStyle(document.getElementById('lbl-h2'));
            return {
                display: s.display,
                bg: s.backgroundColor,
                color: s.color,
                font_size: s.fontSize,
                padding: s.paddingTop + ' ' + s.paddingRight,
                white_space: s.whiteSpace
            };
        }""")
        expected_1 = {
            "display": "inline-block",
            "bg": "rgb(0, 140, 186)",
            "color": "rgb(255, 255, 255)",
            "font_size": "11px",
            "padding": "4px 8px",
            "white_space": "nowrap",
        }
        cases.append({
            "name": "label_default_and_heading_nesting",
            "expected": expected_1,
            "actual": actual_1,
            "passed": actual_1 == expected_1,
        })

        # Case 2: 6 contextual label colors
        actual_2 = page.evaluate("""() => {
            const ids = ['l-default','l-secondary','l-success','l-info','l-warning','l-alert'];
            const out = {};
            for (const id of ids) {
                const s = getComputedStyle(document.getElementById(id));
                out[id.slice(2)] = { bg: s.backgroundColor, color: s.color };
            }
            return out;
        }""")
        expected_2 = {
            "default": {"bg": "rgb(0, 140, 186)", "color": "rgb(255, 255, 255)"},
            "secondary": {"bg": "rgb(231, 231, 231)", "color": "rgb(51, 51, 51)"},
            "success": {"bg": "rgb(67, 172, 106)", "color": "rgb(255, 255, 255)"},
            "info": {"bg": "rgb(160, 211, 232)", "color": "rgb(51, 51, 51)"},
            "warning": {"bg": "rgb(240, 138, 36)", "color": "rgb(255, 255, 255)"},
            "alert": {"bg": "rgb(240, 65, 36)", "color": "rgb(255, 255, 255)"},
        }
        cases.append({
            "name": "label_six_color_variants",
            "expected": expected_2,
            "actual": actual_2,
            "passed": actual_2 == expected_2,
        })

        # Case 3: .radius / .round corners & combined .success.round badge
        actual_3 = page.evaluate("""() => ({
            default_radius: getComputedStyle(document.getElementById('c-default')).borderRadius,
            radius_class: getComputedStyle(document.getElementById('c-radius')).borderRadius,
            round_class: getComputedStyle(document.getElementById('c-round')).borderRadius,
            badge_bg: getComputedStyle(document.getElementById('c-badge')).backgroundColor,
            badge_radius: getComputedStyle(document.getElementById('c-badge')).borderRadius
        })""")
        expected_3 = {
            "default_radius": "0px",
            "radius_class": "3px",
            "round_class": "1000px",
            "badge_bg": "rgb(67, 172, 106)",
            "badge_radius": "1000px",
        }
        cases.append({
            "name": "label_radius_and_round_corners",
            "expected": expected_3,
            "actual": actual_3,
            "passed": actual_3 == expected_3,
        })

        # Case 4: custom CSS font-size overrides
        actual_4 = page.evaluate("""() => {
            const ids = ['sz-36', 'sz-30', 'sz-20', 'sz-def'];
            const sizes = ids.map(id => getComputedStyle(document.getElementById(id)).fontSize);
            const heights = ids.map(id => Math.round(document.getElementById(id).getBoundingClientRect().height));
            return {
                font_sizes: sizes,
                heights_strictly_decreasing: heights[0] > heights[1] && heights[1] > heights[2] && heights[2] > heights[3]
            };
        }""")
        expected_4 = {
            "font_sizes": ["36px", "30px", "20px", "11px"],
            "heights_strictly_decreasing": True,
        }
        cases.append({
            "name": "label_custom_css_font_sizes",
            "expected": expected_4,
            "actual": actual_4,
            "passed": actual_4 == expected_4,
        })

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


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 在离线 Chromium 中核对 .label 的 inline-block/11px/4px 8px 样式、6 种语境配色、0px/3px/1000px 圆角与绿色数字圆角徽标,以及 36px/30px/20px/11px 高度递减缩放。
离线验证 Foundation 5 标签默认盒模型、六种语境色、圆角徽标与自定义字号缩放

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

当前源码 SHA256:8cce521da7153c9cf096e978b05b240d816bc431ac3e6899e4bd2cf74f7e305f

下载本站示例源码

本地运行

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

下载Foundation 标签实验

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

ZIP SHA256:855973d4bc0f91c0f5f34502fa1f0305543b928a82be658a1a9fb8a7f3c085a3

常见错误与边界

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

练习与解答

将 <span class="label">New</span> 直接放在 <h2> 和 <h4> 中时,标签字体大小会自动跟随标题变化吗?如何调整标签大小?

展开参考解答

不会。Foundation 5 为 .label 显式设置了基于根字号的 0.6875rem(11px)字号,因此在不同级别标题中默认保持统一大小;如需按标题层级放大,直接通过 CSS 覆盖该标签的 font-size(及必要时的 padding)即可。

规范与官方依据