蓝色天空.知识 · 实践 · 创造
我的收藏
Foundation 教程目录 37章
Foundation / LEARN BY DOING

Foundation 网格 - 中型设备

解析 Foundation 5 中型设备(40.0625em–64.0624em)的 .medium-* 覆盖规则,对比小中屏组合与仅设 .medium-* 两种写法,并纠偏移动优先向上继承逻辑。

学完这一章,你能够

  • 解析 Foundation 5 中型设备(40.0625em–64.0624em)的 .medium-* 覆盖规则,对比小中屏组合与仅设 .medium-* 两种写法,并纠偏移动优先向上继承逻辑。

中型设备断点范围与 .small-* + .medium-* 组合覆盖

承接上一节小型设备的 `25% / 75%` 两栏(`.small-3` + `.small-9`),在平板等中型设备上通常更适合 `50% / 50%` 对称布局。Foundation 5 将中型设备定义为 `40.0625em` 至 `64.0624em`(16px 根字号下对应 `641px` 至 `1024px`),通过在同一列上叠加 `.medium-*` 类即可在中屏起接管列宽:`class="small-3 medium-6 columns"` 与 `class="small-9 medium-6 columns"`。

需准确理解移动优先的逐级向上继承规则:当元素同时声明 `.small-*` 与 `.medium-*` 但未声明 `.large-*` 时,小型设备生效 `.small-*`(`25% / 75%`),中型设备起被 `.medium-*` 覆盖为 `50% / 50%`,而大型设备会向上继承最近一档已定义的 `.medium-6`(即保持 `50% / 50%`,而非退回 `.small-*`)。本站离线 Chromium 实测验证:`375px` 下两列宽 `[93.8, 281.3]px`(`640px` 为 `[160, 480]px`);跨过 `641px` 阈值后变为 `[320.5, 320.5]px`,`768px` 为 `[384, 384]px`,`1024px`、`1025px` 与 `1200px` 均稳定为 `[500, 500]px`。

仅在中型及以上设备分栏(省略 .small-*)

如果希望手机端保持整宽纵向堆叠、仅从平板及以上才分成左右两栏,则去掉 `.small-*` 仅写 `.medium-6`。本站离线实测仅写 `.medium-6` 的双列:`375px` 与 `640px` 下两列各占 `100%` 上下堆叠(`375px` 下 `[375, 375]px`、`stacked: true`);到达 `641px`、`768px` 与 `1200px` 后自动切换并保持 `50% / 50%` 水平并排(`768px` 为 `[384, 384]px`,`1200px` 为 `[500, 500]px`)。

中型设备网格 12 列总和提示

提示主题核心说明
各断点档位内列数之和须为 12在同一行叠加 .small-* 与 .medium-* 时,每个断点档位内部的列数之和都应等于 12(如 small 档 3+9=12、medium 档 6+6=12)。

离线实测 Foundation 5 中型设备 .medium-6 组合覆盖、向上继承与 640/641px 边界

含完整源码 · 134 行
展开查看收起
python
"""Offline Playwright Chromium lab for Foundation Ch 34 (grid-medium)."""
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-9 { width: 75%; }
@media only screen and (min-width: 40.0625em) {
  .medium-6 { 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-combo" class="row">
    <div id="c-left" class="small-3 medium-6 columns demo-col" style="background-color:yellow;"><p>左栏</p></div>
    <div id="c-right" class="small-9 medium-6 columns demo-col" style="background-color:pink;"><p>右栏</p></div>
  </div>
  <div id="row-med-only" class="row">
    <div id="mo-left" class="medium-6 columns demo-col" style="background-color:yellow;"><p>仅中屏左</p></div>
    <div id="mo-right" class="medium-6 columns demo-col" style="background-color:pink;"><p>仅中屏右</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 combined with .medium-6/.medium-6 across 375, 768, 1200px
        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('c-left').getBoundingClientRect();
                const b = document.getElementById('c-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,
                };
            }""")
        expected_1 = {
            "375": {"widths": [93.8, 281.3], "same_row": True},
            "768": {"widths": [384.0, 384.0], "same_row": True},
            "1200": {"widths": [500.0, 500.0], "same_row": True},
        }
        cases.append({
            "name": "small_3_9_combined_with_medium_6_6",
            "expected": expected_1,
            "actual": actual_1,
            "passed": actual_1 == expected_1,
        })

        # Case 2: Medium-only (.medium-6 without .small-*) stacks at 375px, 50/50 at 768px and 1200px
        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('mo-left').getBoundingClientRect();
                const b = document.getElementById('mo-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_2 = {
            "375": {"widths": [375.0, 375.0], "same_row": False, "stacked": True},
            "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_only_stacks_on_small_splits_on_medium_and_large",
            "expected": expected_2,
            "actual": actual_2,
            "passed": actual_2 == expected_2,
        })

        # Case 3: Exact medium range boundaries (640 vs 641, 1024 vs 1025)
        actual_3 = {}
        for vw in (640, 641, 1024, 1025):
            page.set_viewport_size({"width": vw, "height": 800})
            actual_3[str(vw)] = page.evaluate("""() => {
                const a = document.getElementById('c-left').getBoundingClientRect();
                const b = document.getElementById('c-right').getBoundingClientRect();
                const m1 = document.getElementById('mo-left').getBoundingClientRect();
                const m2 = document.getElementById('mo-right').getBoundingClientRect();
                return {
                    combo_widths: [Math.round(a.width * 10) / 10, Math.round(b.width * 10) / 10],
                    med_only_same_row: Math.abs(m1.top - m2.top) < 1,
                };
            }""")
        expected_3 = {
            "640": {"combo_widths": [160.0, 480.0], "med_only_same_row": False},
            "641": {"combo_widths": [320.5, 320.5], "med_only_same_row": True},
            "1024": {"combo_widths": [500.0, 500.0], "med_only_same_row": True},
            "1025": {"combo_widths": [500.0, 500.0], "med_only_same_row": True},
        }
        cases.append({
            "name": "medium_breakpoint_range_boundaries",
            "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 叠加 .medium-6 在 375px/768px/1200px 的比例变化(25/75 → 50/50 → 50/50),仅写 .medium-6 的小屏堆叠,以及 640/641px 与 1024/1025px 边界值。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

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

下载本章实验包 (mbskycom202612a739.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:Foundation

课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a739.zip

下载课程关联工程包:Foundation

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a750.zip

常见错误与边界检查

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

技术规范与官方资料

练习与参考解答

查看参考解答

呈现 50%/50%(各 500px)。因为 .medium-6 定义在 min-width: 40.0625em 媒体查询中,1200px 同样满足该条件且没有 .large-* 类进一步覆盖,因此大屏继承 .medium-6 的 50% 宽度。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习