Foundation 进度条
核对 Foundation 5 进度条(.progress 与 .meter)的百分比宽度控制、四种语境色、.radius/.round 内外圆角、12 列网格宽度类跨视口响应,以及居中百分比文字标签叠加技巧。
学完这一章,你能够
- 核对 Foundation 5 进度条(.progress 与 .meter)的百分比宽度控制、四种语境色、.radius/.round 内外圆角、12 列网格宽度类跨视口响应,以及居中百分比文字标签叠加技巧。
基础进度条结构与颜色修饰
Foundation 5 进度条采用双层结构:外层 `<div>` 添加 `.progress` 作为浅灰轨道,内层 `<span>` 添加 `.meter` 并通过行内 `style="width:70%"` 指定完成百分比。本站离线 Playwright Chromium 实测轨道背景为 `rgb(246, 246, 246)`、高度 `25px`(`1.5625rem`),内层 `.meter` 为块级元素、默认蓝色 `rgb(0, 140, 186)` 且实际渲染宽度精确占内部轨道宽度的 `70%`。
除默认蓝色外,可在外层 `.progress` 上附加 `.secondary`、`.success` 或 `.alert` 切换内层填充条颜色。本站实测四类 `.meter` 背景色分别为:默认蓝 `rgb(0, 140, 186)`、`.secondary` 浅灰 `rgb(231, 231, 231)`、`.success` 绿色 `rgb(67, 172, 106)` 与 `.alert` 红色 `rgb(240, 65, 36)`。
圆角进度条与 12 列网格宽度控制
在外层 `.progress` 上添加 `.radius` 或 `.round` 可同步为外轨道与内层 `.meter` 施加圆角效果。本站离线实测外轨道/内填充圆角组合依次为:默认 `0px / 0px`、`.radius` `3px / 2px`、`.round` `1000px / 999px`。
若不想进度条占满整行,可直接结合 Foundation 12 列网格类 `.small-num` 或 `.large-num`(`num` 取 `1` 到 `12`,对应 `8.33%` 到 `100%`)限定进度条整体宽度。本站在 `1200px` 桌面视口与 `480px` 移动端视口分别实测:在 `1200px` 下(达到 Foundation 5 `large` 断点 `64.0625em / 1025px`),`.large-1`、`.large-6`、`.large-9`、`.large-11`、`.large-12` 与 `.small-6` 的宽度精确对应 `[100, 600, 900, 1100, 1200, 600]px`;切至 `480px` 移动端后,五个 `.large-*` 全部自动回退为 `100%`(`480px`),而 `.small-6` 始终保持半宽 `240px`。
在进度条内叠加百分比文字标签
Foundation 5 进度条默认不带文字,可通过标准 CSS 定位在 `.meter` 中嵌入百分比标签:先为 `.meter` 设置 `position: relative`,再在内部放置 `<span class="percentage">75%</span>` 并配合 `position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: white; font-size: 12px;` 实现水平垂直居中。本站离线实测 `75%`、`50%`、`25%` 三组进度条上的白色 `12px` 文本中心点与各自 `.meter` 中心偏差均在 `1px` 以内。
离线验证 Foundation 5 进度条占比、四色变体、内外圆角、12 列断点宽度与居中百分比标签
"""Offline Playwright Chromium lab for Foundation Ch 10 (progressbars)."""
import json
from playwright.sync_api import sync_playwright
FOUNDATION_PROG_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; padding: 0; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; }
.progress {
background-color: #f6f6f6; height: 1.5625rem; border: 1px solid #ffffff;
padding: 0.125rem; margin-bottom: 0.625rem; width: 100%;
}
.progress .meter { background: #008CBA; height: 100%; display: block; }
.progress.secondary .meter { background: #e7e7e7; }
.progress.success .meter { background: #43AC6A; }
.progress.alert .meter { background: #f04124; }
.progress.radius { border-radius: 3px; }
.progress.radius .meter { border-radius: 2px; }
.progress.round { border-radius: 1000px; }
.progress.round .meter { border-radius: 999px; }
.small-6 { width: 50%; }
@media only screen and (min-width: 64.0625em) {
.large-1 { width: 8.33333%; }
.large-6 { width: 50%; }
.large-9 { width: 75%; }
.large-11 { width: 91.66667%; }
.large-12 { width: 100%; }
}
.percentage {
position: absolute; top: 50%; left: 50%; color: white;
transform: translate(-50%, -50%); font-size: 12px; line-height: 1;
}
"""
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_PROG_CSS}</style></head>
<body>
<div id="p-basic" class="progress"><span id="m-basic" class="meter" style="width:70%"></span></div>
<div id="p-def" class="progress"><span class="meter" style="width:50%"></span></div>
<div id="p-sec" class="progress secondary"><span class="meter" style="width:50%"></span></div>
<div id="p-suc" class="progress success"><span class="meter" style="width:50%"></span></div>
<div id="p-alr" class="progress alert"><span class="meter" style="width:50%"></span></div>
<div id="p-rad" class="progress radius"><span class="meter" style="width:50%"></span></div>
<div id="p-rnd" class="progress round"><span class="meter" style="width:50%"></span></div>
<div id="p-l1" class="progress large-1"><span class="meter" style="width:50%"></span></div>
<div id="p-l6" class="progress large-6"><span class="meter" style="width:50%"></span></div>
<div id="p-l9" class="progress large-9"><span class="meter" style="width:50%"></span></div>
<div id="p-l11" class="progress large-11"><span class="meter" style="width:50%"></span></div>
<div id="p-l12" class="progress large-12"><span class="meter" style="width:50%"></span></div>
<div id="p-s6" class="progress small-6"><span class="meter" style="width:50%"></span></div>
<div id="p-lbl-75" class="progress"><span id="m-lbl-75" class="meter" style="position:relative;width:75%"><span id="t-75" class="percentage">75%</span></span></div>
<div id="p-lbl-50" class="progress success"><span id="m-lbl-50" class="meter" style="position:relative;width:50%"><span id="t-50" class="percentage">50%</span></span></div>
<div id="p-lbl-25" class="progress alert"><span id="m-lbl-25" class="meter" style="position:relative;width:25%"><span id="t-25" class="percentage">25%</span></span></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": 1200, "height": 900})
page.set_content(HTML_PAGE)
# Case 1: basic .progress container and 70% .meter width
actual_1 = page.evaluate("""() => {
const pr = document.getElementById('p-basic');
const mt = document.getElementById('m-basic');
const ps = getComputedStyle(pr);
const ms = getComputedStyle(mt);
const innerW = pr.getBoundingClientRect().width - 6; // 1px border + 2px padding each side
return {
track_bg: ps.backgroundColor,
track_height: ps.height,
meter_bg: ms.backgroundColor,
meter_display: ms.display,
meter_pct_actual: Math.round((mt.getBoundingClientRect().width / innerW) * 100)
};
}""")
expected_1 = {
"track_bg": "rgb(246, 246, 246)",
"track_height": "25px",
"meter_bg": "rgb(0, 140, 186)",
"meter_display": "block",
"meter_pct_actual": 70,
}
cases.append({
"name": "basic_progress_and_meter_percentage",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: 4 contextual progress meter colors
actual_2 = page.evaluate("""() => ({
default: getComputedStyle(document.querySelector('#p-def .meter')).backgroundColor,
secondary: getComputedStyle(document.querySelector('#p-sec .meter')).backgroundColor,
success: getComputedStyle(document.querySelector('#p-suc .meter')).backgroundColor,
alert: getComputedStyle(document.querySelector('#p-alr .meter')).backgroundColor
})""")
expected_2 = {
"default": "rgb(0, 140, 186)",
"secondary": "rgb(231, 231, 231)",
"success": "rgb(67, 172, 106)",
"alert": "rgb(240, 65, 36)",
}
cases.append({
"name": "progress_color_variants",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: .radius and .round outer + inner .meter border-radius
actual_3 = page.evaluate("""() => ({
default: [getComputedStyle(document.getElementById('p-def')).borderRadius, getComputedStyle(document.querySelector('#p-def .meter')).borderRadius],
radius: [getComputedStyle(document.getElementById('p-rad')).borderRadius, getComputedStyle(document.querySelector('#p-rad .meter')).borderRadius],
round: [getComputedStyle(document.getElementById('p-rnd')).borderRadius, getComputedStyle(document.querySelector('#p-rnd .meter')).borderRadius]
})""")
expected_3 = {
"default": ["0px", "0px"],
"radius": ["3px", "2px"],
"round": ["1000px", "999px"],
}
cases.append({
"name": "progress_radius_and_round_corners",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
# Case 4: .large-1/6/9/11/12 & .small-6 widths at 1200px vs 480px
desktop_w = page.evaluate("""() => ['p-l1','p-l6','p-l9','p-l11','p-l12','p-s6'].map(id => Math.round(document.getElementById(id).getBoundingClientRect().width))""")
page.set_viewport_size({"width": 480, "height": 900})
mobile_w = page.evaluate("""() => ['p-l1','p-l6','p-l9','p-l11','p-l12','p-s6'].map(id => Math.round(document.getElementById(id).getBoundingClientRect().width))""")
page.set_viewport_size({"width": 1200, "height": 900})
actual_4 = {"desktop_1200": desktop_w, "mobile_480": mobile_w}
expected_4 = {
"desktop_1200": [100, 600, 900, 1100, 1200, 600],
"mobile_480": [480, 480, 480, 480, 480, 240],
}
cases.append({
"name": "progress_grid_width_classes",
"expected": expected_4,
"actual": actual_4,
"passed": actual_4 == expected_4,
})
# Case 5: centered .percentage text inside relative .meter
actual_5 = page.evaluate("""() => {
const check = (mId, tId) => {
const mr = document.getElementById(mId).getBoundingClientRect();
const tr = document.getElementById(tId).getBoundingClientRect();
const ts = getComputedStyle(document.getElementById(tId));
const dx = Math.abs((mr.left + mr.width / 2) - (tr.left + tr.width / 2));
const dy = Math.abs((mr.top + mr.height / 2) - (tr.top + tr.height / 2));
return { text: document.getElementById(tId).textContent, centered: dx <= 1 && dy <= 1, color: ts.color, font_size: ts.fontSize };
};
return { p75: check('m-lbl-75','t-75'), p50: check('m-lbl-50','t-50'), p25: check('m-lbl-25','t-25') };
}""")
expected_5 = {
"p75": {"text": "75%", "centered": True, "color": "rgb(255, 255, 255)", "font_size": "12px"},
"p50": {"text": "50%", "centered": True, "color": "rgb(255, 255, 255)", "font_size": "12px"},
"p25": {"text": "25%", "centered": True, "color": "rgb(255, 255, 255)", "font_size": "12px"},
}
cases.append({
"name": "progress_percentage_label_overlay",
"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 中核对 70% .meter 宽度与 25px 轨道高度、4 种语境色、0/3/1000px 圆角、1200px 与 480px 下 .large-1/6/9/11/12 与 .small-6 宽度及 75%/50%/25% 绝对定位居中标签。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:d094b713ff6355ef47ca2a7eaec601117a1e54cb29fc68fd89d57246573f4e90
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:163add0b8f1c252215f228441a8e2c28514bd45023e8aed416a3f313433c8f3c
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
在 <div class="progress large-6"><span class="meter" style="width:50%"></span></div> 中,当视口宽度为 1200px 与 480px 时,进度条轨道和内部填充条分别呈现多宽?
展开参考解答
在 1200px(≥1025px large 断点)下,.large-6 使外层轨道宽为父容器的 50%(600px),内层 .meter 再占轨道内部宽度的 50%;在 480px 移动端视口下,.large-6 媒体查询未激活,轨道回退为 100%(480px),内层 .meter 仍占该轨道宽度的 50%。