Tailwind CSS 表格
核对 Tailwind CSS 表格布局(table-auto/table-fixed)、边框合并与间距、单元格内外边距、对齐排版、合并单元格、响应式显隐列与表题位置。
学完这一章,你能够
- 核对 Tailwind CSS 表格布局(table-auto/table-fixed)、边框合并与间距、单元格内外边距、对齐排版、合并单元格、响应式显隐列与表题位置。
表格布局算法:table-auto 与 table-fixed
Tailwind CSS 通过 `table-auto` 与 `table-fixed` 控制 CSS `table-layout`。`table-auto` 为默认算法,浏览器综合各行单元格内容宽度自动分配列宽;`table-fixed` 则忽略正文内容长度,仅依据表格总宽与首行(或 `<col>`)宽度设定列宽,未指定列宽时平均切分各列,适合规整的数据表。本站离线 Chromium 在 `600px` 宽 3 列表格中实测:相同长歌曲名数据下,`table-auto` 首列宽度超过短年份第三列的 2 倍以上;切换 `table-fixed` 后三列严格均分为 `[200, 200, 200]`。
边框合并、单元格间距与内外边距
表格视觉边界主要通过 `border`(添加边框)、`border-collapse`(合并相邻单元格边框避免双线)或 `border-separate` 搭配 `border-spacing-*`(分离边框并设置单元格间距)控制;单元格留白使用 `px-*`、`py-*`,整表外部间距使用 `m-*`/`mt-*`。本站实测:`border-collapse` 与 `border-separate border-spacing-2` 计算属性分别为 `'collapse'` 与 `'separate'`(`borderSpacing: '8px'`);单元格 `px-6 py-4` 计算内边距为 `16px 24px`,表格 `mt-8` 上外边距为 `32px`。
对齐排版、合并单元格、响应式列与表格标题
单元格文字可直接复用排版工具类,如 `text-left`/`text-center`/`text-right`、`font-semibold`/`font-bold` 以及 `uppercase`/`lowercase`/`capitalize`;跨列跨行合并仍沿用原生 HTML `colspan` 与 `rowspan` 属性。本站实测 `600px` 宽 3 列 `table-fixed border-collapse` 表格中,`<th colspan="2" class="text-center font-bold uppercase">` 计算对齐为 `center`、字重 `700`、大小写转换 `uppercase`,与单列实测宽度比为 `[399, 200]`(含 `1px` 折叠共享边框)。
结合响应式前缀(如 `hidden md:table-cell`)可在小屏隐藏次要列、在中屏及以上恢复表格单元格显示;表格标题 `<caption>` 位置由 `caption-top` 与 `caption-bottom` 控制。本站实测:挂 `hidden md:table-cell` 的第二列表头在 `500px` 视口计算 `display` 为 `'none'`,在 `900px` 视口恢复 `'table-cell'`;`caption-top` 计算 `captionSide` 为 `'top'` 且底部位于 `tbody` 之上,`caption-bottom` 为 `'bottom'` 且顶部位于 `tbody` 之下。
离线 Chromium 验证 table-auto/fixed 列宽、边框折叠、colspan 及响应式列/表题
"""Offline Playwright Chromium lab for Tailwind CSS Ch 19: Table."""
import json
from playwright.sync_api import sync_playwright
CSS = """
*, ::before, ::after { box-sizing: border-box; }
body { margin: 0; font-size: 16px; }
.table-auto { table-layout: auto; }
.table-fixed { table-layout: fixed; }
.w-full { width: 100%; }
.border { border: 1px solid #cbd5e1; }
.border-collapse { border-collapse: collapse; }
.border-separate { border-collapse: separate; }
.border-spacing-2 { border-spacing: 0.5rem; }
.px-4 { padding-left: 1rem; padding-right: 1rem; }
.py-2 { padding-top: 0.5rem; padding-bottom: 0.5rem; }
.px-6 { padding-left: 1.5rem; padding-right: 1.5rem; }
.py-4 { padding-top: 1rem; padding-bottom: 1rem; }
.mt-8 { margin-top: 2rem; }
.text-left { text-align: left; }
.text-center { text-align: center; }
.text-right { text-align: right; }
.font-semibold { font-weight: 600; }
.font-bold { font-weight: 700; }
.uppercase { text-transform: uppercase; }
.lowercase { text-transform: lowercase; }
.capitalize { text-transform: capitalize; }
.hidden { display: none; }
.caption-top { caption-side: top; }
.caption-bottom { caption-side: bottom; }
@media (min-width: 768px) {
.md\\:table-cell { display: table-cell; }
}
"""
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": 900, "height": 900})
page.set_content(f"""
<!doctype html><html><head><style>{CSS}</style></head>
<body>
<div style="width:600px">
<table id="t-auto" class="table-auto w-full border-collapse">
<thead><tr><th id="ta1" class="border px-4 py-2">Song</th><th id="ta2" class="border px-4 py-2">Artist</th><th id="ta3" class="border px-4 py-2">Year</th></tr></thead>
<tbody><tr><td class="border px-4 py-2">The Sliding Mr. Bones (Next Stop, Pottersville)</td><td class="border px-4 py-2">Malcolm Lockyer</td><td class="border px-4 py-2">1961</td></tr></tbody>
</table>
<table id="t-fixed" class="table-fixed w-full border-collapse">
<thead><tr><th id="tf1" class="border px-4 py-2">Song</th><th id="tf2" class="border px-4 py-2">Artist</th><th id="tf3" class="border px-4 py-2">Year</th></tr></thead>
<tbody><tr><td class="border px-4 py-2">The Sliding Mr. Bones (Next Stop, Pottersville)</td><td class="border px-4 py-2">Malcolm Lockyer</td><td class="border px-4 py-2">1961</td></tr></tbody>
</table>
</div>
<table id="t-sep" class="table-auto border-separate border-spacing-2 mt-8">
<tbody><tr><td id="cell-lg" class="border px-6 py-4">Cell</td></tr></tbody>
</table>
<div style="width:600px">
<table id="t-span" class="table-fixed w-full border-collapse">
<thead>
<tr>
<th id="th-span" class="border px-4 py-2 text-center font-bold uppercase" colspan="2">Header 1 & 2</th>
<th id="th-single" class="border px-4 py-2 text-right font-semibold capitalize">header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td class="border px-4 py-2">C1</td>
<td class="border px-4 py-2">C2</td>
<td class="border px-4 py-2">C3</td>
</tr>
</tbody>
</table>
</div>
<table class="table-auto w-full border-collapse">
<thead><tr><th class="border">H1</th><th id="resp-col" class="border hidden md:table-cell">H2</th><th class="border">H3</th></tr></thead>
</table>
<table id="t-ctop">
<caption id="cap-top" class="caption-top">Top Caption</caption>
<tbody id="tb-top"><tr><td>Row</td></tr></tbody>
</table>
<table id="t-cbot">
<caption id="cap-bot" class="caption-bottom">Bottom Caption</caption>
<tbody id="tb-bot"><tr><td>Row</td></tr></tbody>
</table>
</body></html>
""")
# Case 1: table-auto vs table-fixed
act1 = page.evaluate("""() => {
const w = id => Math.round(document.getElementById(id).getBoundingClientRect().width);
const auto1 = w('ta1'), auto3 = w('ta3');
const fix = [w('tf1'), w('tf2'), w('tf3')];
return {
auto_layout: getComputedStyle(document.getElementById('t-auto')).tableLayout,
fixed_layout: getComputedStyle(document.getElementById('t-fixed')).tableLayout,
auto_col1_wider_than_col3: auto1 > auto3 * 2,
fixed_equal_cols: fix,
};
}""")
exp1 = {
"auto_layout": "auto",
"fixed_layout": "fixed",
"auto_col1_wider_than_col3": True,
"fixed_equal_cols": [200, 200, 200],
}
cases.append({
"name": "table_auto_vs_table_fixed_widths",
"passed": act1 == exp1,
"expected": exp1,
"actual": act1,
})
# Case 2: border-collapse, border-separate/spacing, cell padding & table margin
act2 = page.evaluate("""() => {
const sAuto = getComputedStyle(document.getElementById('t-auto'));
const sSep = getComputedStyle(document.getElementById('t-sep'));
const sCell = getComputedStyle(document.getElementById('cell-lg'));
return {
collapse_mode: sAuto.borderCollapse,
separate_mode: sSep.borderCollapse,
border_spacing: sSep.borderSpacing,
table_mt_8: sSep.marginTop,
cell_px6_py4: [sCell.paddingTop, sCell.paddingRight],
};
}""")
exp2 = {
"collapse_mode": "collapse",
"separate_mode": "separate",
"border_spacing": "8px",
"table_mt_8": "32px",
"cell_px6_py4": ["16px", "24px"],
}
cases.append({
"name": "border_collapse_spacing_and_padding",
"passed": act2 == exp2,
"expected": exp2,
"actual": act2,
})
# Case 3: alignment, font weight, text transform & colspan
act3 = page.evaluate("""() => {
const s1 = getComputedStyle(document.getElementById('th-span'));
const s2 = getComputedStyle(document.getElementById('th-single'));
return {
span_align: s1.textAlign,
span_weight: s1.fontWeight,
span_transform: s1.textTransform,
single_align: s2.textAlign,
single_weight: s2.fontWeight,
single_transform: s2.textTransform,
colspan2_vs_single_width: [
Math.round(document.getElementById('th-span').getBoundingClientRect().width),
Math.round(document.getElementById('th-single').getBoundingClientRect().width),
],
};
}""")
exp3 = {
"span_align": "center",
"span_weight": "700",
"span_transform": "uppercase",
"single_align": "right",
"single_weight": "600",
"single_transform": "capitalize",
"colspan2_vs_single_width": [399, 200],
}
cases.append({
"name": "alignment_typography_and_colspan",
"passed": act3 == exp3,
"expected": exp3,
"actual": act3,
})
# Case 4: Responsive hidden md:table-cell & caption-top vs caption-bottom
md_disp = page.evaluate("() => getComputedStyle(document.getElementById('resp-col')).display")
page.set_viewport_size({"width": 500, "height": 900})
sm_disp = page.evaluate("() => getComputedStyle(document.getElementById('resp-col')).display")
cap_info = page.evaluate("""() => {
const ct = document.getElementById('cap-top').getBoundingClientRect();
const tt = document.getElementById('tb-top').getBoundingClientRect();
const cb = document.getElementById('cap-bot').getBoundingClientRect();
const tb = document.getElementById('tb-bot').getBoundingClientRect();
return {
caption_top_side: getComputedStyle(document.getElementById('cap-top')).captionSide,
caption_bottom_side: getComputedStyle(document.getElementById('cap-bot')).captionSide,
top_above_body: ct.bottom <= tt.top + 1,
bottom_below_body: cb.top >= tb.bottom - 1,
};
}""")
act4 = {"sm_display": sm_disp, "md_display": md_disp, **cap_info}
exp4 = {
"sm_display": "none",
"md_display": "table-cell",
"caption_top_side": "top",
"caption_bottom_side": "bottom",
"top_above_body": True,
"bottom_below_body": True,
}
cases.append({
"name": "responsive_column_and_caption_position",
"passed": act4 == exp4,
"expected": exp4,
"actual": act4,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
- 核对 600px 表格 table-fixed 均分 [200,200,200]、border-collapse vs border-spacing(8px)、colspan=2 宽度比 [399,200]、500px/900px 响应式列显隐及 caption-top/bottom 上下定位。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:811bf07075f48d463ea4f8c29f0c8df9f499c3c5ec511b78e1b7080463d9ebc5
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:a3b651e6cc0bb6b597079b84ad31b767ac047a5d390801681463f4884f8abce7
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
什么时候应选择 table-fixed 而不是默认的 table-auto?border-spacing-* 在哪种边框模式下才能生效?
展开参考解答
当需要列宽整齐可控、不因某一格超长文本挤压其他列时应使用 table-fixed(并在首行指定列宽);border-spacing-* 仅在 border-separate(border-collapse: separate)模式下生效,在 border-collapse 下会被浏览器忽略。