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

Foundation 表格

核对 Foundation 5 原生 <table> 的四周边框、浅灰表头与偶数行斑马条纹样式,并离线对比外层容器 overflow-x:hidden 裁切与 overflow-x:auto 横向滚动的实测差异。

学完这一章,你能够

  • 核对 Foundation 5 原生 <table> 的四周边框、浅灰表头与偶数行斑马条纹样式,并离线对比外层容器 overflow-x:hidden 裁切与 overflow-x:auto 横向滚动的实测差异。

Foundation 基础表格边框与斑马条纹

Foundation 5 直接为原生 `<table>` 元素赋予整洁的网格外观,无需额外添加类名即可同时获得四边外框、浅灰表头与交替斑马条纹。本站在离线 Playwright Chromium 中对三列基础表格实测:表格四周边框均为 `1px solid rgb(221, 221, 221)`、主体底色 `rgb(255, 255, 255)`、`<thead>` 背景为 `rgb(245, 245, 245)` 且表头 `<th>` 呈 `700` 加粗 `14px` 字号;`<tbody>` 中奇数行保持透明(透出白底),偶数行(`tr:nth-of-type(even)`)自动填充浅灰 `rgb(249, 249, 249)`,单元格内边距为 `9px 10px`。

窄屏响应式表格容器:overflow-x:hidden 与 overflow-x:auto 实测对照

当多列表格在小屏设备上超出父容器宽度时,常见做法是在 `<table>` 外包裹一层 `<div>` 并控制其水平溢出行为。参考写法使用 `<div style="overflow-x:hidden">` 来阻止页面整体出现横向撑破,但需注意工程上的关键差别:本站离线 Chromium 将 `600px` 宽表格分别置于 `300px` 宽的 `overflow-x:hidden` 与 `overflow-x:auto` 容器内实测,前者虽避免了父级撑宽,却直接裁切隐藏了右侧 `300px`(`scrollWidth 600 - clientWidth 300`)内容且无法通过用户滚动查看;而改用 `overflow-x:auto` 时容器可在原地横向滚动(实测 `scrollLeft` 可平滑滚动至 `120px`)以保留全部列数据。因此实际生产场景通常优先选用 `overflow-x:auto`。

离线验证 Foundation 5 表格边框、偶数行斑马条纹与窄屏容器溢出行为

含完整源码 · 150 行
展开查看收起
python
"""Offline Playwright Chromium lab for Foundation Ch 04 (tables)."""
import json
from playwright.sync_api import sync_playwright

FOUNDATION_TABLE_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; padding: 16px; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; color: #222222; }
table { background: #ffffff; border: 1px solid #dddddd; margin-bottom: 1.25rem; table-layout: auto; border-collapse: separate; border-spacing: 0; }
table thead { background: #f5f5f5; }
table thead tr th { padding: 0.5rem 0.625rem 0.625rem; font-size: 0.875rem; font-weight: bold; color: #222222; text-align: left; }
table tr td { padding: 0.5625rem 0.625rem; font-size: 0.875rem; color: #222222; text-align: left; }
table tr.even, table tr.alt, table tr:nth-of-type(even) { background: #f9f9f9; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_TABLE_CSS}</style></head>
<body>
  <table id="basic-table">
    <thead>
      <tr id="head-row">
        <th>Firstname</th><th>Lastname</th><th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr id="row-1"><td>John</td><td>Doe</td><td>john@example.com</td></tr>
      <tr id="row-2"><td>Mary</td><td>Moe</td><td>mary@example.com</td></tr>
      <tr id="row-3"><td>July</td><td>Dooley</td><td>july@example.com</td></tr>
    </tbody>
  </table>

  <div id="wrap-hidden" style="overflow-x:hidden; width:300px;">
    <table id="wide-hidden" style="min-width:600px;">
      <thead><tr><th>ID</th><th>Firstname</th><th>Lastname</th><th>Department</th><th>Email</th></tr></thead>
      <tbody><tr><td>1</td><td>John</td><td>Doe</td><td>Engineering</td><td>john.doe@example.com</td></tr></tbody>
    </table>
  </div>

  <div id="wrap-auto" style="overflow-x:auto; width:300px;">
    <table id="wide-auto" style="min-width:600px;">
      <thead><tr><th>ID</th><th>Firstname</th><th>Lastname</th><th>Department</th><th>Email</th></tr></thead>
      <tbody><tr><td>1</td><td>John</td><td>Doe</td><td>Engineering</td><td>john.doe@example.com</td></tr></tbody>
    </table>
  </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": 800, "height": 600})
        page.set_content(HTML_PAGE)

        # Case 1: four borders and thead background
        actual_1 = page.evaluate("""() => {
            const tbl = getComputedStyle(document.getElementById('basic-table'));
            const thead = getComputedStyle(document.querySelector('#basic-table thead'));
            const th = getComputedStyle(document.querySelector('#basic-table th'));
            return {
                borders: [tbl.borderTopWidth, tbl.borderRightWidth, tbl.borderBottomWidth, tbl.borderLeftWidth],
                border_color: tbl.borderTopColor,
                table_bg: tbl.backgroundColor,
                thead_bg: thead.backgroundColor,
                th_font_weight: th.fontWeight,
                th_font_size: th.fontSize
            };
        }""")
        expected_1 = {
            "borders": ["1px", "1px", "1px", "1px"],
            "border_color": "rgb(221, 221, 221)",
            "table_bg": "rgb(255, 255, 255)",
            "thead_bg": "rgb(245, 245, 245)",
            "th_font_weight": "700",
            "th_font_size": "14px",
        }
        cases.append({
            "name": "basic_table_borders_and_header",
            "expected": expected_1,
            "actual": actual_1,
            "passed": actual_1 == expected_1,
        })

        # Case 2: gray zebra striping across tbody rows
        actual_2 = page.evaluate("""() => ({
            row1_bg: getComputedStyle(document.getElementById('row-1')).backgroundColor,
            row2_bg: getComputedStyle(document.getElementById('row-2')).backgroundColor,
            row3_bg: getComputedStyle(document.getElementById('row-3')).backgroundColor,
            td_padding: getComputedStyle(document.querySelector('#row-1 td')).paddingTop + ' ' + getComputedStyle(document.querySelector('#row-1 td')).paddingLeft
        })""")
        expected_2 = {
            "row1_bg": "rgba(0, 0, 0, 0)",
            "row2_bg": "rgb(249, 249, 249)",
            "row3_bg": "rgba(0, 0, 0, 0)",
            "td_padding": "9px 10px",
        }
        cases.append({
            "name": "zebra_striping_even_rows",
            "expected": expected_2,
            "actual": actual_2,
            "passed": actual_2 == expected_2,
        })

        # Case 3: overflow-x:hidden clipping vs overflow-x:auto horizontal scroll
        actual_3 = page.evaluate("""() => {
            const wh = document.getElementById('wrap-hidden');
            const wa = document.getElementById('wrap-auto');
            wa.scrollLeft = 120;
            wh.scrollLeft = 120;
            return {
                hidden_overflow_x: getComputedStyle(wh).overflowX,
                hidden_client_w: wh.clientWidth,
                hidden_scroll_w: wh.scrollWidth,
                hidden_clipped_px: wh.scrollWidth - wh.clientWidth,
                auto_overflow_x: getComputedStyle(wa).overflowX,
                auto_scroll_left_after_scroll: wa.scrollLeft
            };
        }""")
        expected_3 = {
            "hidden_overflow_x": "hidden",
            "hidden_client_w": 300,
            "hidden_scroll_w": 600,
            "hidden_clipped_px": 300,
            "auto_overflow_x": "auto",
            "auto_scroll_left_after_scroll": 120,
        }
        cases.append({
            "name": "responsive_table_overflow_hidden_vs_auto",
            "expected": expected_3,
            "actual": actual_3,
            "passed": actual_3 == expected_3,
        })

        browser.close()
    print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
在离线 Chromium 中核对 table 四边 1px #dddddd 边框、thead #f5f5f5 背景、tbody 偶数行 #f9f9f9 条纹,以及 300px 容器包裹 600px 宽表时 overflow-x:hidden 裁切 300px 与 overflow-x:auto 可横向滚动至 120px。

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

本地运行

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

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

本章附件与实例包

下载课程关联工程包:Foundation

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

下载课程关联工程包:Foundation

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

常见错误与边界检查

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

技术规范与官方资料

练习与参考解答

查看参考解答

两者都能防止宽表格撑破页面主容器,但 overflow-x:hidden 会直接裁切隐藏超出容器宽度的列内容,用户无法查看被裁切的数据;overflow-x:auto 则在容器内部提供水平滚动条,允许用户左右滑动浏览完整表格。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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