将字段标签的基线与文本输入中文本的基线对齐
我在字段集中的某些标签文本旁边显示文本输入。我希望文本输入中文本的基线与标签文本的基线对齐。我可以为 .label
元素设置一个模糊因子 padding-top
,但 .value
元素的内容可能包含只读文本,并且这会破坏这些字段的标签/值基线对齐。我还怀疑,由于不同浏览器的输入字段之间的高度差异,不同的浏览器需要不同的模糊因素。
有人知道如何使标签和输入文本基线对齐吗?我的标签文本可能跨越多行,因此我希望任何解决方案都能考虑到这一点。
您可以在 http://jsbin.com/enobe3 中查看以下代码的实例。
CSS
* {
font-family: sans-serif;
font-size: 13px;
}
.field {
clear: left;
padding-top: 5px;
}
.label {
float: left;
width: 90px;
}
.value {
margin-left: 90px;
padding-left: 10px;
}
HTML
<fieldset>
<div class="field">
<div class="label">A short label</div>
<div class="value">Some text</div>
</div>
<div class="field">
<div class="label">A long long long long long long long wrapping label</div>
<div class="value">Some text</div>
</div>
<div class="field">
<div class="label">A short label</div>
<div class="value"><input value="Some text"/></div>
</div>
<div class="field">
<div class="label">A long long long long long long long wrapping label</div>
<div class="value"><input value="Some text"/></div>
</div>
</fieldset>
I'm displaying text inputs next to some label text in a fieldset. I'd like the baseline of the text in the text inputs to line up with the baseline of the label text. I could set a fudge factor padding-top
for my .label
elements but the content of my .value
elements may contain read-only text and this would screw up the label/value baseline alignment for those fields. I also suspect the different fudge factors would be required for different browsers because of height differences between input fields across browsers.
Does anyone have any ideas how I can get my label and input text baselines to align? My label text may span multiple lines and as such I'd like any solution to take this into account.
You can see a live example the following code at http://jsbin.com/enobe3.
CSS
* {
font-family: sans-serif;
font-size: 13px;
}
.field {
clear: left;
padding-top: 5px;
}
.label {
float: left;
width: 90px;
}
.value {
margin-left: 90px;
padding-left: 10px;
}
HTML
<fieldset>
<div class="field">
<div class="label">A short label</div>
<div class="value">Some text</div>
</div>
<div class="field">
<div class="label">A long long long long long long long wrapping label</div>
<div class="value">Some text</div>
</div>
<div class="field">
<div class="label">A short label</div>
<div class="value"><input value="Some text"/></div>
</div>
<div class="field">
<div class="label">A long long long long long long long wrapping label</div>
<div class="value"><input value="Some text"/></div>
</div>
</fieldset>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基线对齐问题与文本框和输入字段的默认行高有关。
输入
字段具有较大的默认高度,以容纳填充、边框和文本。我尝试了您的 HTML/CSS 代码片段,并将我的版本发布在 http://jsfiddle.net/audetwebdesign/EbuJH/
我添加了一些背景颜色、一些填充和边距来勾勒出块的轮廓。
关键是设置
line-height
属性,以便标签和输入字段具有相同的值,在我的示例中为 2.0。您可以调整行高以了解其工作原理。
我在 Firefox 和 IE8 中测试了该示例,结果与严格的文档类型一致。
结果对于单行标签效果很好。一个缺点是多行标签的间距可能比您想要的要大。您可以通过将多行标签的第一行放入跨度中并仅将行高应用到该跨度来解决此问题,但这是额外的工作。
至少我们知道控制基线定位的因素是什么,这样我们才能取得进展。
The baseline alignment issue is related to the default line height for the text boxes and input field.
input
fields have a larger default height to accommodate padding, border and text.I experimented with your HTML/CSS code snippets and posted my version at http://jsfiddle.net/audetwebdesign/EbuJH/
I added some background colors, some padding and margins to outline the blocks.
The key is to set the
line-height
property so that your labels and input fields have the same value, 2.0 in my example.You can adjust the line height to get a sense of how it works.
I tested the example in Firefox and IE8 and the results are consistent with a strict doctype.
The result works well for single line labels. The one drawback is that the multi-line labels may have more spacing than you like. You could work around this by putting the first line of the multi-line label in a span and applying the line height only to the span, but that is extra work.
At least we know understand what controls the baseline positioning, so we can make progress.