Foundation 输入框尺寸
核对 Foundation 5 利用网格列宽控制输入框尺寸、等宽三列响应式堆叠、.inline.right 内联标签垂直居中以及 .row.collapse 前置/后置标签与圆角按钮组合。
学完这一章,你能够
- 核对 Foundation 5 利用网格列宽控制输入框尺寸、等宽三列响应式堆叠、.inline.right 内联标签垂直居中以及 .row.collapse 前置/后置标签与圆角按钮组合。
使用网格列控制输入框宽度
由于 Foundation 5 表单控件默认占满父容器 `100%` 宽度,调整输入框尺寸的标准做法是将其放入响应式网格列中(如 `.large-10.medium-7.columns`、`.small-5.columns`、`.medium-3.columns`,完整断点规则参见网格系统章节)。本站离线 Chromium 在最大行宽 `960px` 下跨三档视口实测:`480px` 小屏下仅带中大屏类的列回退为 `480px` 满宽,而 `.small-5` 保持 `200px`(5/12);`768px` 中屏下三列分别占 `448px`、`320px`、`192px`;`1200px` 大屏下三列分别占 `800px`(10/12)、`400px`(5/12)、`240px`(3/12)。
相等大小多列输入框布局
在一行 `.row` 内并置三个 `.medium-4.columns` 即可实现等宽三列输入区。离线实测:`480px` 窄屏下三列均为 `480px` 且纵坐标严格递增自动堆叠;`960px` 桌面端下三列位于同一水平行且宽度精确均分为 `[320, 320, 320]px`。
左侧内联标签 .inline 与 .right
如需把标签放在输入框左侧而非上方,可将 `<label>` 与 `<input>` 分置在同一嵌套 `.row` 的左右两列(如 `.small-3.columns` 配 `.small-9.columns`),并给 `<label>` 加上 `.inline`(实现垂直居中)及可选的 `.right`(文字右对齐)。实测 `label.inline.right` 计算内边距顶部 `9px`、总高 `37px` 与右侧 `37px` 高的 `<input>` 顶部完全平齐,`text-align: right`,点击标签后焦点准确落入 `#inl-name`。
前置与后置标签、按钮及圆角组合
在嵌套的 `<div class="row collapse">`(`.collapse` 会把直接子列的左右内边距清零为 `0px`,消除列间隙)中配合 `<span class="prefix">` 或 `<span class="postfix">` 可拼接紧贴输入框的前置/后置标签,并通过网格列比例分配标签与输入框宽度。
若附加项需要可点击操作,只需把 `<span>` 换成 `<a href="#" class="button prefix">` 或 `<a href="#" class="button postfix">`;在外层 `.row.collapse` 上附加 `.prefix-radius`、`.postfix-radius`、`.prefix-round` 或 `.postfix-round` 即可仅对外侧边角应用圆角。本站离线实测确认:`.collapse` 列右内边距为 `0px`,前置/后置节点与输入框边缘无缝相接(`right === left`);`.prefix-radius` 左上/右上圆角为 `3px/0px`,`.postfix-radius` 为 `0px/3px`,`.prefix-round` 按钮呈主题蓝 `rgb(0, 140, 186)` 且外侧圆角 `1000px/0px`,`.postfix-round` 为 `0px/1000px`。
离线验证 Foundation 5 输入框网格断点宽度、内联标签对齐与前后置圆角按钮
"""Offline Playwright Chromium lab for Foundation Ch 24 (input-sizing)."""
import json
from playwright.sync_api import sync_playwright
FOUNDATION_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 16px; color: #222; background: #fff; }
.row { width: 100%; max-width: 960px; margin: 0 auto; }
.row::before, .row::after { content: " "; display: table; }
.row::after { clear: both; }
.row .row { width: auto; max-width: none; margin: 0 -15px; }
.row.collapse > .columns { padding-left: 0; padding-right: 0; }
.row .row.collapse { margin: 0; }
.columns { position: relative; padding-left: 15px; padding-right: 15px; float: left; width: 100%; }
.small-3 { width: 25%; }
.small-5 { width: 41.66667%; }
.small-8 { width: 66.66667%; float: left; }
.small-9 { width: 75%; }
@media only screen and (min-width: 40.063em) {
.medium-3 { width: 25%; }
.medium-4 { width: 33.33333%; }
.medium-7 { width: 58.33333%; }
}
@media only screen and (min-width: 64.063em) {
.large-6 { width: 50%; }
.large-10 { width: 83.33333%; }
}
label { display: block; font-size: 14px; color: #4d4d4d; margin-bottom: 4px; }
label.inline { margin: 0 0 16px 0; padding: 9px 0; line-height: 19px; }
label.right { float: right; text-align: right; }
input[type="text"] {
display: block; width: 100%; height: 37px; padding: 8px; margin: 0 0 16px 0;
border: 1px solid #cccccc; background: #fff; font-size: 14px;
}
span.prefix, span.postfix, a.button.prefix, a.button.postfix {
display: block; position: relative; z-index: 2; text-align: center;
width: 100%; height: 37px; line-height: 35px; font-size: 14px;
border: 1px solid #cccccc; text-decoration: none;
}
span.prefix, span.postfix { background: #f2f2f2; color: #333333; }
span.prefix { border-right: none; }
span.postfix { border-left: none; }
a.button.prefix, a.button.postfix { background: #008cba; border-color: #007095; color: #ffffff; }
.prefix-radius .prefix { border-radius: 3px 0 0 3px; }
.prefix-radius input { border-radius: 0 3px 3px 0; }
.postfix-radius input { border-radius: 3px 0 0 3px; }
.postfix-radius .postfix { border-radius: 0 3px 3px 0; }
.prefix-round .prefix { border-radius: 1000px 0 0 1000px; }
.prefix-round input { border-radius: 0 1000px 1000px 0; }
.postfix-round input { border-radius: 1000px 0 0 1000px; }
.postfix-round .postfix { border-radius: 0 1000px 1000px 0; }
"""
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_CSS}</style></head>
<body>
<form id="f-grid">
<div class="row"><div id="col-l10-m7" class="large-10 medium-7 columns"><label>A<input type="text"></label></div></div>
<div class="row"><div id="col-s5" class="small-5 columns"><label>B<input type="text"></label></div></div>
<div class="row"><div id="col-m3" class="medium-3 columns"><label>C<input type="text"></label></div></div>
</form>
<form id="f-equal">
<div class="row">
<div id="eq1" class="medium-4 columns"><label>M4-1<input type="text"></label></div>
<div id="eq2" class="medium-4 columns"><label>M4-2<input type="text"></label></div>
<div id="eq3" class="medium-4 columns"><label>M4-3<input type="text"></label></div>
</div>
</form>
<form id="f-inline">
<div class="row">
<div class="small-8">
<div class="row">
<div class="small-3 columns"><label id="inl-lbl" for="inl-name" class="inline right">Name</label></div>
<div class="small-9 columns"><input type="text" id="inl-name" placeholder="First Name.."></div>
</div>
</div>
</div>
</form>
<form id="f-addons">
<div class="row">
<div class="large-6 columns">
<div id="row-pre-rad" class="row collapse prefix-radius">
<div id="pre-rad-col" class="small-3 columns"><span id="pre-rad-span" class="prefix">Prefix</span></div>
<div class="small-9 columns"><input id="pre-rad-inp" type="text" placeholder="Value"></div>
</div>
</div>
<div class="large-6 columns">
<div class="row collapse postfix-radius">
<div class="small-9 columns"><input id="post-rad-inp" type="text" placeholder="Value"></div>
<div class="small-3 columns"><span id="post-rad-span" class="postfix">Postfix</span></div>
</div>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<div class="row collapse prefix-round">
<div class="small-3 columns"><a id="pre-rnd-btn" href="#" class="button prefix">Go</a></div>
<div class="small-9 columns"><input id="pre-rnd-inp" type="text" placeholder="Value"></div>
</div>
</div>
<div class="large-6 columns">
<div class="row collapse postfix-round">
<div class="small-9 columns"><input id="post-rnd-inp" type="text" placeholder="Value"></div>
<div class="small-3 columns"><a id="post-rnd-btn" href="#" class="button postfix">Go</a></div>
</div>
</div>
</div>
</form>
</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": 480, "height": 900})
page.set_content(HTML_PAGE)
# Case 1: grid column input sizing across small (480), medium (768), large (1200 -> max 960 row)
def measure_grid():
return page.evaluate("""() => ({
l10_m7: Math.round(document.getElementById('col-l10-m7').getBoundingClientRect().width),
s5: Math.round(document.getElementById('col-s5').getBoundingClientRect().width),
m3: Math.round(document.getElementById('col-m3').getBoundingClientRect().width),
})""")
small_w = measure_grid()
page.set_viewport_size({"width": 768, "height": 900})
med_w = measure_grid()
page.set_viewport_size({"width": 1200, "height": 900})
large_w = measure_grid()
c1 = {"at_480": small_w, "at_768": med_w, "at_1200_row960": large_w}
exp1 = {
"at_480": {"l10_m7": 480, "s5": 200, "m3": 480},
"at_768": {"l10_m7": 448, "s5": 320, "m3": 192},
"at_1200_row960": {"l10_m7": 800, "s5": 400, "m3": 240},
}
cases.append(dict(name="grid_column_input_sizing_breakpoints", passed=(c1 == exp1), expected=exp1, actual=c1))
# Case 2: equal 3-up columns stack on small, align on medium+
page.set_viewport_size({"width": 480, "height": 900})
eq_small = page.evaluate("""() => {
const rects = ['#eq1', '#eq2', '#eq3'].map(s => document.querySelector(s).getBoundingClientRect());
return {
widths: rects.map(r => Math.round(r.width)),
stacked_vertically: rects[0].top < rects[1].top && rects[1].top < rects[2].top,
};
}""")
page.set_viewport_size({"width": 960, "height": 900})
eq_desk = page.evaluate("""() => {
const rects = ['#eq1', '#eq2', '#eq3'].map(s => document.querySelector(s).getBoundingClientRect());
return {
widths: rects.map(r => Math.round(r.width)),
same_row: Math.round(rects[0].top) === Math.round(rects[1].top) && Math.round(rects[1].top) === Math.round(rects[2].top),
};
}""")
c2 = {"mobile_480": eq_small, "desktop_960": eq_desk}
exp2 = {
"mobile_480": {"widths": [480, 480, 480], "stacked_vertically": True},
"desktop_960": {"widths": [320, 320, 320], "same_row": True},
}
cases.append(dict(name="equal_columns_three_up_and_stacking", passed=(c2 == exp2), expected=exp2, actual=c2))
# Case 3: inline right label alignment and focus
page.click("#inl-lbl")
c3 = page.evaluate("""() => {
const lbl = document.getElementById('inl-lbl');
const inp = document.getElementById('inl-name');
const ls = getComputedStyle(lbl);
const lr = lbl.getBoundingClientRect();
const ir = inp.getBoundingClientRect();
return {
active_id: document.activeElement.id,
label_align: ls.textAlign,
label_padding_top: ls.paddingTop,
same_top: Math.round(lr.top) === Math.round(ir.top),
label_height: Math.round(lr.height),
input_height: Math.round(ir.height),
};
}""")
exp3 = {
"active_id": "inl-name",
"label_align": "right",
"label_padding_top": "9px",
"same_top": True,
"label_height": 37,
"input_height": 37,
}
cases.append(dict(name="inline_right_label_alignment", passed=(c3 == exp3), expected=exp3, actual=c3))
# Case 4: prefix/postfix collapse, button, radius & round corners
page.set_viewport_size({"width": 1200, "height": 900})
c4 = page.evaluate("""() => {
const colPad = getComputedStyle(document.getElementById('pre-rad-col')).paddingRight;
const preSpan = document.getElementById('pre-rad-span');
const preInp = document.getElementById('pre-rad-inp');
const postInp = document.getElementById('post-rad-inp');
const postSpan = document.getElementById('post-rad-span');
const preBtn = document.getElementById('pre-rnd-btn');
const postBtn = document.getElementById('post-rnd-btn');
return {
collapse_col_padding_right: colPad,
prefix_flush_with_input: Math.round(preSpan.getBoundingClientRect().right) === Math.round(preInp.getBoundingClientRect().left),
postfix_flush_with_input: Math.round(postInp.getBoundingClientRect().right) === Math.round(postSpan.getBoundingClientRect().left),
prefix_radius: getComputedStyle(preSpan).borderTopLeftRadius + '/' + getComputedStyle(preSpan).borderTopRightRadius,
postfix_radius: getComputedStyle(postSpan).borderTopLeftRadius + '/' + getComputedStyle(postSpan).borderTopRightRadius,
prefix_round_btn_bg: getComputedStyle(preBtn).backgroundColor,
prefix_round_radius: getComputedStyle(preBtn).borderTopLeftRadius + '/' + getComputedStyle(preBtn).borderTopRightRadius,
postfix_round_radius: getComputedStyle(postBtn).borderTopLeftRadius + '/' + getComputedStyle(postBtn).borderTopRightRadius,
};
}""")
exp4 = {
"collapse_col_padding_right": "0px",
"prefix_flush_with_input": True,
"postfix_flush_with_input": True,
"prefix_radius": "3px/0px",
"postfix_radius": "0px/3px",
"prefix_round_btn_bg": "rgb(0, 140, 186)",
"prefix_round_radius": "1000px/0px",
"postfix_round_radius": "0px/1000px",
}
cases.append(dict(name="prefix_postfix_collapse_buttons_and_radius", passed=(c4 == exp4), expected=exp4, actual=c4))
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
- 在离线 Chromium 中核对 480/768/1200px 视口列宽变化、三列等宽堆叠与并排、.inline.right 37px 垂直对齐以及 .row.collapse 前后置无缝贴合与 3px/1000px 圆角。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:f3eb5f13903e0412887770ecadd9933c79e629308db4506600e9168472b08022
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:d36b0a38d982ddcc82d8a25d69fa9df8bf226cee7310b5ab99793c763f7fcb70
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
在构建前置/后置标签(.prefix / .postfix)输入框时,为什么外层行必须添加 .collapse 类?
展开参考解答
Foundation 网格列默认带有左右内边距形成列间距;添加 .row.collapse 后子列水平 padding 归零,前置/后置标签或按钮才能与输入框边缘无缝衔接成一个整体控件。