Foundation 网格 - 小型设备
讲解 Foundation 5 小型设备(<40.0625em)的 .small-* 网格用法、移动优先向上继承机制,以及 25%/75% 与 33.3%/66.7% 两种经典两栏比例的实测。
学完这一章,你能够
- 讲解 Foundation 5 小型设备(<40.0625em)的 .small-* 网格用法、移动优先向上继承机制,以及 25%/75% 与 33.3%/66.7% 两种经典两栏比例的实测。
小型设备断点定义与移动优先继承机制
在 Foundation 5 中,小型设备指视口宽度小于 `40.0625em`(按 16px 根字号即 `≤640px`)的手机端屏幕,对应列类前缀为 `.small-*`。若希望在小屏幕上直接呈现左右两栏(例如 `25% / 75%`),可使用 `.small-3.columns` 与 `.small-9.columns` 组合。
由于 Foundation 5 严格遵循移动优先(Mobile-First)原则,`.small-*` 不包裹在任何 `min-width` 媒体查询中,因此若未额外添加 `.medium-*` 或 `.large-*` 覆盖规则,平板与桌面端会自动继承 `.small-*` 的列宽比例。本站离线 Chromium 跨三档视口实测 `.small-3` + `.small-9`:`375px` 下为 `[93.8, 281.3]px`、`768px` 下为 `[192, 576]px`、`1200px` 下(`.row` 宽 `1000px`)为 `[250, 750]px`,全程保持单行 `25% / 75%` 水平排列。
33.3% / 66.7% 比例配置与 12 列约束
若需要将两栏比例调整为三分之一与三分之二(`33.33% / 66.67%`),只需把列类替换为 `.small-4` 与 `.small-8`(依然满足 `4 + 8 = 12`)。本站离线实测该组合在 `375px`、`768px` 与 `1200px` 下分别渲染为 `[125, 250]px`、`[256, 512]px` 与 `[333.3, 666.7]px` 并始终同行;边界测试亦验证 `40.0625em` 对应精确 `641px` 切换点,且当同行总列数超过 12(如 `.small-5` + `.small-8` = 13)时第二列在 `375px` 下即触发折行。
小型设备网格 12 列总和提示
| 提示主题 | 核心说明 |
|---|---|
| 单行总列数保持为 12 | 使用 .small-* 划分多列时,应确保同一 .row 内的列数之和等于 12(如 3+9 或 4+8),超出 12 会导致后续列折行。 |
离线实测 Foundation 5 小型设备 .small-3/.small-9 与 .small-4/.small-8 继承及边界
"""Offline Playwright Chromium lab for Foundation Ch 33 (grid-small)."""
import json
from playwright.sync_api import sync_playwright
FOUNDATION_GRID_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; }
.row { width: 100%; margin: 0 auto; max-width: 62.5rem; }
.row::before, .row::after { content: " "; display: table; }
.row::after { clear: both; }
.column, .columns { padding-left: 0.9375rem; padding-right: 0.9375rem; width: 100%; float: left; position: relative; }
.small-3 { width: 25%; }
.small-4 { width: 33.33333%; }
.small-5 { width: 41.66667%; }
.small-8 { width: 66.66667%; }
.small-9 { width: 75%; }
@media only screen and (min-width: 40.0625em) {
.medium-probe { width: 50%; }
}
.demo-col { min-height: 28px; }
"""
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_GRID_CSS}</style></head>
<body>
<div id="row-3-9" class="row">
<div id="s3" class="small-3 columns demo-col" style="background-color:yellow;"><p>25%</p></div>
<div id="s9" class="small-9 columns demo-col" style="background-color:pink;"><p>75%</p></div>
</div>
<div id="row-4-8" class="row">
<div id="s4" class="small-4 columns demo-col" style="background-color:yellow;"><p>33.3%</p></div>
<div id="s8" class="small-8 columns demo-col" style="background-color:pink;"><p>66.7%</p></div>
</div>
<div id="row-over" class="row">
<div id="ov5" class="small-5 columns demo-col">5</div>
<div id="ov8" class="small-8 columns demo-col">8</div>
</div>
<div class="row">
<div id="probe" class="medium-probe columns demo-col">P</div>
</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": 375, "height": 800})
page.set_content(HTML_PAGE)
# Case 1: .small-3 / .small-9 (25%/75%) inherited across small, medium, large
actual_1 = {}
for vw in (375, 768, 1200):
page.set_viewport_size({"width": vw, "height": 800})
actual_1[str(vw)] = page.evaluate("""() => {
const a = document.getElementById('s3').getBoundingClientRect();
const b = document.getElementById('s9').getBoundingClientRect();
return {
widths: [Math.round(a.width * 10) / 10, Math.round(b.width * 10) / 10],
same_row: Math.abs(a.top - b.top) < 1,
};
}""")
expected_1 = {
"375": {"widths": [93.8, 281.3], "same_row": True},
"768": {"widths": [192.0, 576.0], "same_row": True},
"1200": {"widths": [250.0, 750.0], "same_row": True},
}
cases.append({
"name": "small_3_and_9_mobile_first_inheritance",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: .small-4 / .small-8 (33.3%/66.7%) across small, medium, large
actual_2 = {}
for vw in (375, 768, 1200):
page.set_viewport_size({"width": vw, "height": 800})
actual_2[str(vw)] = page.evaluate("""() => {
const a = document.getElementById('s4').getBoundingClientRect();
const b = document.getElementById('s8').getBoundingClientRect();
return {
widths: [Math.round(a.width * 10) / 10, Math.round(b.width * 10) / 10],
same_row: Math.abs(a.top - b.top) < 1,
};
}""")
expected_2 = {
"375": {"widths": [125.0, 250.0], "same_row": True},
"768": {"widths": [256.0, 512.0], "same_row": True},
"1200": {"widths": [333.3, 666.7], "same_row": True},
}
cases.append({
"name": "small_4_and_8_one_third_two_thirds",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: 40.0625em (641px) boundary & 12-column sum check
page.set_viewport_size({"width": 640, "height": 800})
w640 = page.evaluate("() => Math.round(document.getElementById('probe').getBoundingClientRect().width * 10) / 10")
page.set_viewport_size({"width": 641, "height": 800})
w641 = page.evaluate("() => Math.round(document.getElementById('probe').getBoundingClientRect().width * 10) / 10")
page.set_viewport_size({"width": 375, "height": 800})
over = page.evaluate("""() => {
const a = document.getElementById('ov5').getBoundingClientRect();
const b = document.getElementById('ov8').getBoundingClientRect();
return { sum_13_same_row: Math.abs(a.top - b.top) < 1, wrapped: b.top >= a.bottom - 1 };
}""")
actual_3 = {"probe_640_w": w640, "probe_641_w": w641, **over}
expected_3 = {"probe_640_w": 640.0, "probe_641_w": 320.5, "sum_13_same_row": False, "wrapped": True}
cases.append({
"name": "small_threshold_and_12_col_sum",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
- 在离线 Chromium 中核对 .small-3/.small-9(25%/75%)与 .small-4/.small-8(33.3%/66.7%)在 375px/768px/1200px 视口的向上继承列宽,以及 640/641px 阈值与 5+8=13 列折行。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:d8b019d0f766fee0c2317a120820cb12bc6f7fa3146d4d6d787d26fd731fc42a
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:6a562942361790e64bc0d040f6d451950466bbd111f918a6d03609dc8c9d4c5d
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
如果一个 .row 中的两列只写了 .small-3 和 .small-9,在 1200px 桌面浏览器上它们会堆叠还是保持 25%/75% 并排?原因是什么?
展开参考解答
会保持 25%/75% 并排(在 1000px 默认 .row 内分别为 250px 与 750px)。因为 Foundation 5 是移动优先框架,.small-* 规则没有最大宽度限制,当未提供 .medium-* 或 .large-* 类覆盖时会自动向上继承到所有更大屏幕。