Foundation 网格系统
系统解析 Foundation 5 响应式 12 列浮动网格结构、small/medium/large 三档断点规则、嵌套/偏移/排序能力及 62.5rem 宽屏容器控制,并附离线 Chromium 实测。
学完这一章,你能够
- 系统解析 Foundation 5 响应式 12 列浮动网格结构、small/medium/large 三档断点规则、嵌套/偏移/排序能力及 62.5rem 宽屏容器控制,并附离线 Chromium 实测。
12 列响应式网格与三档列前缀
Foundation 5 采用 12 列响应式浮动网格体系:无需拆满 12 列时可将相邻列合并(例如 3×4、4+8、6+6 或通栏 12)以组建不同宽度的版块;同一组列会随视口尺寸自动调整排布,在大屏幕上横向并排显示多列,而在小屏幕下可按顺序纵向堆叠为单列。
网格按设备宽度提供三档断点类前缀:`.small-*`(手机等小型设备)、`.medium-*`(平板等中等设备)与 `.large-*`(笔记本及台式机等大型设备)。开发时可将多档类组合写在同一列元素上,按移动优先规则自下而上覆盖,构建精细的多端自适应布局。
基本网格结构与多断点组合实例
构建网格时先用 `<div class="row">` 建立水平行容器,再在其中添加携带 `.columns`(或 `.column`)与 `small-num`、`medium-num`、`large-num` 类的子列,单行各列的 `num` 之和应保持等于 `12`(每列默认左右内边距各 `0.9375rem` 即 `15px`,形成 `30px` 列间距)。
以三行综合结构为例:第一行 `.small-12` 占满 `100%` 宽度;第二行 `.small-8` 与 `.small-4` 按 `66.67% / 33.33%` 划分;第三行叠加 `.small-8.large-9` 与 `.small-4.large-3`,在小屏与中屏保持 `66.67% / 33.33%`,进入大屏后切换为 `75% / 25%`。本站离线 Chromium 实测(每列实测左右内边距 `['15px', '15px']`):`375px` 视口下三行宽度依次为 `375px`、`[250, 125]px`、`[250, 125]px`;`768px` 下为 `768px`、`[512, 256]px`、`[512, 256]px`;`1200px` 下(`.row` 上限 `1000px`)变为 `1000px`、`[666.7, 333.3]px` 与 `[750, 250]px`。
网格断点选项与高级布局能力
按 16px 根字号换算,Foundation 5 三档断点的精确临界值分别为:`small`(`<40.0625em`,即 `≤640px`,始终水平排列)、`medium`(`≥40.0625em`,即 `641px` 起水平并排,以下堆叠)与 `large`(`≥64.0625em`,即 `1025px` 起水平并排,以下堆叠)。本站实测 `640px→641px` 与 `1024px→1025px` 边界切换准确无误,同时在 `1200px` 视口实测三档均支持的嵌套列(半宽列内再分两列各 `250px`)、偏移量(`.small-offset-2` 左外边距 `166.7px`)与列排序(`.small-push-4` 与 `.small-pull-8` 视觉左偏移变为 `[333.3px, 0px]`,完成左右互换)。
宽屏 62.5rem 上限与全宽背景技巧
默认 `.row` 的 `max-width` 为 `62.5rem`(16px 根字号下即 `1000px`),因此在宽屏显示器上即使列宽写满 12 列也不会铺满整个视口。若希望网格贴边铺满,可用自定义 CSS 覆盖 `.row { max-width: 100%; }`。本站 `1200px` 视口实测:默认 `.row` 渲染宽 `1000px`(左右居中各留 `100px`),覆盖 `max-width: 100%` 后 `.row` 扩展至 `1200px`,其内两列 `.small-6` 由 `500px` 增至 `600px`。
若既想保留内容区 `62.5rem` 的默认可读宽度,又需要通栏背景色横跨整页,可将默认 `.row` 放进一个带背景色和内边距的全宽外层容器中(例如 `background-color: coral; padding: 25px;` 包裹 `.row`)。本站 `1200px` 视口实测:外层通栏容器实测宽 `1200px`(背景色 `rgb(255, 127, 80)`),内部 `.row` 依然居中保持 `1000px`,两列 `.small-6` 各宽 `500px`。
Foundation 5 网格选项多设备对照表
| 选项维度 | 小型设备 (<40.0625em / ≤640px) | 中等设备 (≥40.0625em / ≥641px) | 大型设备 (≥64.0625em / ≥1025px) |
|---|---|---|---|
| 网格行为 | 始终保持水平排列 | 低于断点纵向堆叠,≥641px 水平排列 | 低于断点纵向堆叠,≥1025px 水平排列 |
| 类前缀 | .small-* | .medium-* | .large-* |
| 列数上限 | 12 | 12 | 12 |
| 支持嵌套 | Yes(列内可继续嵌套 .row) | Yes(列内可继续嵌套 .row) | Yes(列内可继续嵌套 .row) |
| 支持偏移 | Yes(.small-offset-*) | Yes(.medium-offset-*) | Yes(.large-offset-*) |
| 支持排序 | Yes(.small-push-* / .small-pull-*) | Yes(.medium-push-* / .medium-pull-*) | Yes(.large-push-* / .large-pull-*) |
离线实测 Foundation 5 12 列网格结构、三档断点切换与 62.5rem 宽屏控制
"""Offline Playwright Chromium lab for Foundation Ch 31 (grid-system)."""
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; }
.row .row { width: auto; margin-left: -0.9375rem; margin-right: -0.9375rem; max-width: none; }
.column, .columns { padding-left: 0.9375rem; padding-right: 0.9375rem; width: 100%; float: left; position: relative; }
[class*="column"] + [class*="column"]:last-child { float: right; }
[class*="column"] + [class*="column"].end { float: left; }
.small-1 { width: 8.33333%; }
.small-2 { width: 16.66667%; }
.small-3 { width: 25%; }
.small-4 { width: 33.33333%; }
.small-5 { width: 41.66667%; }
.small-6 { width: 50%; }
.small-7 { width: 58.33333%; }
.small-8 { width: 66.66667%; }
.small-9 { width: 75%; }
.small-10 { width: 83.33333%; }
.small-11 { width: 91.66667%; }
.small-12 { width: 100%; }
.small-offset-2 { margin-left: 16.66667% !important; }
.small-push-4 { position: relative; left: 33.33333%; right: auto; }
.small-pull-8 { position: relative; right: 66.66667%; left: auto; }
@media only screen and (min-width: 40.0625em) {
.medium-1 { width: 8.33333%; }
.medium-2 { width: 16.66667%; }
.medium-3 { width: 25%; }
.medium-4 { width: 33.33333%; }
.medium-5 { width: 41.66667%; }
.medium-6 { width: 50%; }
.medium-7 { width: 58.33333%; }
.medium-8 { width: 66.66667%; }
.medium-9 { width: 75%; }
.medium-10 { width: 83.33333%; }
.medium-11 { width: 91.66667%; }
.medium-12 { width: 100%; }
}
@media only screen and (min-width: 64.0625em) {
.large-1 { width: 8.33333%; }
.large-2 { width: 16.66667%; }
.large-3 { width: 25%; }
.large-4 { width: 33.33333%; }
.large-5 { width: 41.66667%; }
.large-6 { width: 50%; }
.large-7 { width: 58.33333%; }
.large-8 { width: 66.66667%; }
.large-9 { width: 75%; }
.large-10 { width: 83.33333%; }
.large-11 { width: 91.66667%; }
.large-12 { width: 100%; }
}
.row.widescreen-full { max-width: 100%; }
.demo-box { min-height: 24px; }
"""
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_GRID_CSS}</style></head>
<body>
<div id="row-1" class="row">
<div id="r1-c1" class="small-12 columns demo-box" style="background-color:yellow;">.small-12</div>
</div>
<div id="row-2" class="row">
<div id="r2-c1" class="small-8 columns demo-box" style="background-color:beige;">.small-8</div>
<div id="r2-c2" class="small-4 columns demo-box" style="background-color:gray;">.small-4</div>
</div>
<div id="row-3" class="row">
<div id="r3-c1" class="large-9 small-8 columns demo-box" style="background-color:pink;">.small-8 .large-9</div>
<div id="r3-c2" class="large-3 small-4 columns demo-box" style="background-color:orange;">.small-4 .large-3</div>
</div>
<div id="row-med" class="row">
<div id="rm-c1" class="medium-6 columns demo-box">M1</div>
<div id="rm-c2" class="medium-6 columns demo-box">M2</div>
</div>
<div id="row-lrg" class="row">
<div id="rl-c1" class="large-6 columns demo-box">L1</div>
<div id="rl-c2" class="large-6 columns demo-box">L2</div>
</div>
<div id="row-cap" class="row">
<div id="nest-parent" class="small-6 columns demo-box">
<div class="row">
<div id="nest-a" class="small-6 columns demo-box">N1</div>
<div id="nest-b" class="small-6 columns demo-box">N2</div>
</div>
</div>
<div id="offset-col" class="small-4 small-offset-2 columns demo-box">Off</div>
</div>
<div id="row-order" class="row">
<div id="ord-1" class="small-8 small-push-4 columns demo-box">Push</div>
<div id="ord-2" class="small-4 small-pull-8 columns demo-box">Pull</div>
</div>
<div id="row-ws" class="row widescreen-full">
<div id="ws-c1" class="small-6 columns demo-box">WS1</div>
<div id="ws-c2" class="small-6 columns demo-box">WS2</div>
</div>
<div id="bleed-wrap" style="background-color:coral;padding:25px;">
<div id="bleed-row" class="row">
<div id="bl-c1" class="small-6 columns demo-box" style="background-color:yellow;">.small-6</div>
<div id="bl-c2" class="small-6 columns demo-box" style="background-color:pink;">.small-6</div>
</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": 900})
page.set_content(HTML_PAGE)
# Case 1: Basic 12-column structure & responsive ratios across 375 / 768 / 1200px
actual_1 = {}
for vw in (375, 768, 1200):
page.set_viewport_size({"width": vw, "height": 900})
actual_1[str(vw)] = page.evaluate("""() => {
const r = id => Math.round(document.getElementById(id).getBoundingClientRect().width * 10) / 10;
const sameRow = (a, b) => Math.abs(
document.getElementById(a).getBoundingClientRect().top -
document.getElementById(b).getBoundingClientRect().top
) < 1;
const cs = getComputedStyle(document.getElementById('r1-c1'));
return {
row1_w: r('r1-c1'),
row2_w: [r('r2-c1'), r('r2-c2')],
row2_same_row: sameRow('r2-c1', 'r2-c2'),
row3_w: [r('r3-c1'), r('r3-c2')],
row3_same_row: sameRow('r3-c1', 'r3-c2'),
gutter_lr: [cs.paddingLeft, cs.paddingRight],
};
}""")
expected_1 = {
"375": {
"row1_w": 375.0,
"row2_w": [250.0, 125.0],
"row2_same_row": True,
"row3_w": [250.0, 125.0],
"row3_same_row": True,
"gutter_lr": ["15px", "15px"],
},
"768": {
"row1_w": 768.0,
"row2_w": [512.0, 256.0],
"row2_same_row": True,
"row3_w": [512.0, 256.0],
"row3_same_row": True,
"gutter_lr": ["15px", "15px"],
},
"1200": {
"row1_w": 1000.0,
"row2_w": [666.7, 333.3],
"row2_same_row": True,
"row3_w": [750.0, 250.0],
"row3_same_row": True,
"gutter_lr": ["15px", "15px"],
},
}
cases.append({
"name": "basic_12_col_and_responsive_ratios",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Breakpoint behavior (640/641, 1024/1025) & nesting/offset/ordering
bp_checks = {}
for vw in (640, 641, 1024, 1025):
page.set_viewport_size({"width": vw, "height": 900})
bp_checks[str(vw)] = page.evaluate("""() => {
const sameRow = (a, b) => Math.abs(
document.getElementById(a).getBoundingClientRect().top -
document.getElementById(b).getBoundingClientRect().top
) < 1;
return {
medium_horizontal: sameRow('rm-c1', 'rm-c2'),
large_horizontal: sameRow('rl-c1', 'rl-c2'),
};
}""")
page.set_viewport_size({"width": 1200, "height": 900})
caps = page.evaluate("""() => {
const w = id => Math.round(document.getElementById(id).getBoundingClientRect().width * 10) / 10;
const l = id => Math.round(document.getElementById(id).getBoundingClientRect().left * 10) / 10;
const rowLeft = document.getElementById('row-order').getBoundingClientRect().left;
return {
nested_pair_w: [w('nest-a'), w('nest-b')],
offset_margin_left: Math.round(parseFloat(getComputedStyle(document.getElementById('offset-col')).marginLeft) * 10) / 10,
ordered_visual_lefts: [
Math.round((l('ord-1') - rowLeft) * 10) / 10,
Math.round((l('ord-2') - rowLeft) * 10) / 10,
],
visual_swapped: l('ord-1') > l('ord-2'),
};
}""")
actual_2 = {"breakpoints": bp_checks, "capabilities": caps}
expected_2 = {
"breakpoints": {
"640": {"medium_horizontal": False, "large_horizontal": False},
"641": {"medium_horizontal": True, "large_horizontal": False},
"1024": {"medium_horizontal": True, "large_horizontal": False},
"1025": {"medium_horizontal": True, "large_horizontal": True},
},
"capabilities": {
"nested_pair_w": [250.0, 250.0],
"offset_margin_left": 166.7,
"ordered_visual_lefts": [333.3, 0.0],
"visual_swapped": True,
},
}
cases.append({
"name": "grid_breakpoint_behavior_and_capabilities",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Widescreen default max-width (62.5rem = 1000px) vs max-width: 100%
page.set_viewport_size({"width": 1200, "height": 900})
actual_3 = page.evaluate("""() => {
const defRow = document.getElementById('row-1');
const wsRow = document.getElementById('row-ws');
const w = el => Math.round(el.getBoundingClientRect().width * 10) / 10;
return {
default_max_width: getComputedStyle(defRow).maxWidth,
default_rendered_w: w(defRow),
default_left_margin: Math.round(defRow.getBoundingClientRect().left * 10) / 10,
override_max_width: getComputedStyle(wsRow).maxWidth,
override_rendered_w: w(wsRow),
override_cols_w: [w(document.getElementById('ws-c1')), w(document.getElementById('ws-c2'))],
};
}""")
expected_3 = {
"default_max_width": "1000px",
"default_rendered_w": 1000.0,
"default_left_margin": 100.0,
"override_max_width": "100%",
"override_rendered_w": 1200.0,
"override_cols_w": [600.0, 600.0],
}
cases.append({
"name": "widescreen_max_width_override",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
# Case 4: Full-bleed coral outer container with default 1000px inner .row
actual_4 = page.evaluate("""() => {
const wrap = document.getElementById('bleed-wrap');
const inner = document.getElementById('bleed-row');
const w = el => Math.round(el.getBoundingClientRect().width * 10) / 10;
return {
wrapper_w: w(wrap),
wrapper_bg: getComputedStyle(wrap).backgroundColor,
inner_row_w: w(inner),
inner_cols_w: [w(document.getElementById('bl-c1')), w(document.getElementById('bl-c2'))],
};
}""")
expected_4 = {
"wrapper_w": 1200.0,
"wrapper_bg": "rgb(255, 127, 80)",
"inner_row_w": 1000.0,
"inner_cols_w": [500.0, 500.0],
}
cases.append({
"name": "full_bleed_wrapper_with_default_row",
"expected": expected_4,
"actual": actual_4,
"passed": actual_4 == expected_4,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
- 在离线 Chromium 中核对 375px/768px/1200px 视口下 .small-12、.small-8/.small-4、.large-9/.large-3 列宽,640/641px 与 1024/1025px 断点切换,嵌套/偏移/排序及 max-width: 100% 与全宽背景容器表现。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:659fb05abdb9c63258b555c80c7ef1303d8f9d39b3683b8eddda617cbdec73cb
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:37d090b67e87f0b54d72c429ce32a2f89fc547db7e6d13c9aaf1506820bd48a0
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
在 1200px 宽屏下,为什么给默认 .row 中的两列设置 .small-6 后总宽只有 1000px 而不是 1200px?如何既保留 1000px 内容宽度又让背景色通栏?
展开参考解答
因为 Foundation 5 默认为 .row 设定了 max-width: 62.5rem(16px 根字号下为 1000px)并自动水平居中;若要背景色通栏同时保留内容最大宽度,可在 .row 外部套一层设置背景色的全宽容器 div,内部仍使用默认 .row。