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

Foundation 网格 - 水平堆叠

验证仅使用 .medium-* 类实现的“小屏纵向堆叠、中大屏水平并排”基础响应式网格模式,实测 640px→641px 断点切换及超出 12 列时的折行行为。

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

学完这一章,你能够

小屏堆叠、中大屏水平并排的经典模式

在日常多端页面开发中,最常见的基础布局是:手机等小型屏幕上各栏自动上下堆叠占满整宽,进入平板与桌面端后立即展开为左右并排的多栏结构。只需在 `.row` 中仅指定 `.medium-*` 类(例如两个 `.medium-6.columns`)而省略 `.small-*`,即可借助列默认的 `width: 100%` 与中屏媒体查询实现自动切换。本站离线 Chromium 跨 5 档视口实测:`375px` 与 `640px` 下两列均纵向堆叠(列宽 `375px` / `640px`,`stacked: true`);到达 `641px` 断点后立即并排为 `[320.5, 320.5]px`;在 `768px` 与 `1200px` 视口下则分别呈 `[384, 384]px` 与 `[500, 500]px` 水平平铺。

网格类名 `.small|medium|large-*` 中的末尾数字表示该列跨越的栅格份数:例如 `.small-1` 跨 `1` 列(`8.33%`)、`.small-4` 跨 `4` 列(`33.33%`)、`.small-6` 跨 `6` 列(`50%`)。本站 `1200px`(`.row` 宽 `1000px`)实测三者宽度依次为 `83.3px`、`333.3px` 与 `500px`,`1 + 4 + 7 = 12` 与 `6 + 6 = 12` 均能严丝合缝排在同一行;反之若同行之和超过 12(如 `768px` 视口下 `.medium-6` 配 `.medium-8`,合计 14 列,宽 `384px` 与 `512px`),第二列因空间不足被迫折至下一行。

列跨度数字与 12 列总和提示

提示主题核心说明
列数跨度含义与单行 12 列约束.small|medium|large-* 中的数字代表跨越列数(如 .small-1 跨 1 列、.small-4 跨 4 列、.small-6 跨 6 列即 50% 宽度),同一行各列数字之和应保持为 12,超过 12 将导致浮动折行。

离线实测 Foundation 5 .medium-6 小屏堆叠转中大屏水平布局及 12 列溢出折行

python · 阅读源码
"""Offline Playwright Chromium lab for Foundation Ch 32 (grid-stacked-to-horizontal)."""
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-1 { width: 8.33333%; }
.small-4 { width: 33.33333%; }
.small-6 { width: 50%; }
.small-7 { width: 58.33333%; }
@media only screen and (min-width: 40.0625em) {
  .medium-6 { width: 50%; }
  .medium-8 { width: 66.66667%; }
}
.demo-col { min-height: 32px; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_GRID_CSS}</style></head>
<body>
  <div id="row-main" class="row">
    <div id="m-left" class="medium-6 columns demo-col" style="background-color:yellow;"><p>左栏内容</p></div>
    <div id="m-right" class="medium-6 columns demo-col" style="background-color:pink;"><p>右栏内容</p></div>
  </div>
  <div id="row-spans" class="row">
    <div id="s-1" class="small-1 columns demo-col">1</div>
    <div id="s-4" class="small-4 columns demo-col">4</div>
    <div id="s-7" class="small-7 columns demo-col">7</div>
  </div>
  <div id="row-half" class="row">
    <div id="s-6a" class="small-6 columns demo-col">6A</div>
    <div id="s-6b" class="small-6 columns demo-col">6B</div>
  </div>
  <div id="row-overflow" class="row">
    <div id="ov-1" class="medium-6 columns demo-col">6</div>
    <div id="ov-2" class="medium-8 columns demo-col">8</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: .medium-6 stacks vertically on small (<641px), aligns horizontally on medium/large (>=641px)
        actual_1 = {}
        for vw in (375, 640, 641, 768, 1200):
            page.set_viewport_size({"width": vw, "height": 800})
            actual_1[str(vw)] = page.evaluate("""() => {
                const a = document.getElementById('m-left').getBoundingClientRect();
                const b = document.getElementById('m-right').getBoundingClientRect();
                return {
                    widths: [Math.round(a.width * 10) / 10, Math.round(b.width * 10) / 10],
                    same_row: Math.abs(a.top - b.top) < 1,
                    stacked: b.top >= a.bottom - 1,
                };
            }""")
        expected_1 = {
            "375": {"widths": [375.0, 375.0], "same_row": False, "stacked": True},
            "640": {"widths": [640.0, 640.0], "same_row": False, "stacked": True},
            "641": {"widths": [320.5, 320.5], "same_row": True, "stacked": False},
            "768": {"widths": [384.0, 384.0], "same_row": True, "stacked": False},
            "1200": {"widths": [500.0, 500.0], "same_row": True, "stacked": False},
        }
        cases.append({
            "name": "medium_6_stacked_on_small_horizontal_on_medium_large",
            "expected": expected_1,
            "actual": actual_1,
            "passed": actual_1 == expected_1,
        })

        # Case 2: .small-1, .small-4, .small-6 widths and 12-column sum
        page.set_viewport_size({"width": 1200, "height": 800})
        actual_2 = page.evaluate("""() => {
            const r = id => document.getElementById(id).getBoundingClientRect();
            const r1 = r('s-1'), r4 = r('s-4'), r7 = r('s-7'), r6a = r('s-6a'), r6b = r('s-6b');
            return {
                small_1_w: Math.round(r1.width * 10) / 10,
                small_4_w: Math.round(r4.width * 10) / 10,
                small_7_w: Math.round(r7.width * 10) / 10,
                row_1_4_7_same_row: Math.abs(r1.top - r4.top) < 1 && Math.abs(r4.top - r7.top) < 1,
                small_6_pair_w: [Math.round(r6a.width * 10) / 10, Math.round(r6b.width * 10) / 10],
                small_6_same_row: Math.abs(r6a.top - r6b.top) < 1,
            };
        }""")
        expected_2 = {
            "small_1_w": 83.3,
            "small_4_w": 333.3,
            "small_7_w": 583.3,
            "row_1_4_7_same_row": True,
            "small_6_pair_w": [500.0, 500.0],
            "small_6_same_row": True,
        }
        cases.append({
            "name": "column_span_numbers_and_12_sum",
            "expected": expected_2,
            "actual": actual_2,
            "passed": actual_2 == expected_2,
        })

        # Case 3: Exceeding 12 columns (6 + 8 = 14) wraps to next line
        page.set_viewport_size({"width": 768, "height": 800})
        actual_3 = page.evaluate("""() => {
            const a = document.getElementById('ov-1').getBoundingClientRect();
            const b = document.getElementById('ov-2').getBoundingClientRect();
            return {
                widths: [Math.round(a.width * 10) / 10, Math.round(b.width * 10) / 10],
                same_row: Math.abs(a.top - b.top) < 1,
                wrapped_below: b.top >= a.bottom - 1,
            };
        }""")
        expected_3 = {
            "widths": [384.0, 512.0],
            "same_row": False,
            "wrapped_below": True,
        }
        cases.append({
            "name": "overflow_over_12_columns_wraps_to_next_line",
            "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 中核对 .medium-6 双列在 375px/640px 堆叠、641px/768px/1200px 并排,.small-1/.small-4/.small-6 宽度计算,以及 6+8=14 列溢出折行。
离线实测 Foundation 5 .medium-6 小屏堆叠转中大屏水平布局及 12 列溢出折行

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

当前源码 SHA256:6bf59943d3656733cc047f7901534adf3b225852df052edbef37f887acdbbfed

下载本站示例源码

本地运行

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

下载Foundation 网格 - 水平堆叠实验

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

ZIP SHA256:cdfdc9d499d463cd519669fc2f1b37d8c023c346291243728f83d92be3c39cbd

常见错误与边界

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

练习与解答

为什么只写 .medium-6 而不写 .small-12 就能在手机端实现 100% 宽度堆叠?如果同一行误写了 .medium-6 与 .medium-8 会发生什么?

展开参考解答

因为 Foundation 5 为所有 .columns 默认设置了 width: 100%,在低于 40.0625em(641px)时 .medium-6 媒体查询不生效,列自然各占 100% 上下堆叠;若同行合计超过 12 列(如 6+8=14),第二列会因浮点宽度之和大于 100% 而被挤到下一行。

规范与官方依据