在 TD 内添加控件时表项垂直对齐方式发生变化
我一直想知道这个问题。为什么在将控件放入 td 内部时,td 的对齐会受到影响。
例如。
<tr>
<td>Row 1</td>
<td>
<input type="text" />
<input type="button" value="Select" />
</td>
<td>Selected Value 1</td>
</tr>
<tr>
<td>Row 2</td>
<td colspan="2">
<input type="text" />
<input type="button" value="Select" />
Selected Value 2
</td>
</tr>
“第 1 行”文本与“选定值 1”对齐。但是,文本“选定值 2”稍微向下且未居中,且未与“行 2”对齐。
我已经尝试过垂直对齐中间甚至 valign 但它不起作用。 IE 和 Firefox 中都会发生这种情况。在IE中表现得更明显。我真的不明白这一点。
I have been wondering about this. Why does the alignment of the td gets affected when placing controls inside it.
For example.
<tr>
<td>Row 1</td>
<td>
<input type="text" />
<input type="button" value="Select" />
</td>
<td>Selected Value 1</td>
</tr>
<tr>
<td>Row 2</td>
<td colspan="2">
<input type="text" />
<input type="button" value="Select" />
Selected Value 2
</td>
</tr>
The "Row 1" text is aligned with "Selected Value 1". However, the text "Selected Value 2" is slightly down and not centered and not aligned with "Row 2".
I already tried vertical align middle or even valign and it does not work. It happens in both IE and Firefox. It is more obvious in IE. I really do not get this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为第二行中的组件与文本“内联”显示,并且组件的高度大于文本的行高。
如果将所有文本的行高更改为更大的值,它们都会对齐。
it's because the components in the 2nd row are being displayed "inline" with the text and the heights of the components are larger than the line height of the text.
if you changed the line heights for all the pieces of text to a larger value, they would all line up.
输入控件的行高与标准文本不同。因此,当您将这些原生内联控件放置在文本旁边时,它们会强制基线/行高的行为与没有它的文本的行为不同。如果您将图像放置在文本旁边而不使其浮动(文本与图像底部对齐,直到换行到下一行),也会发生这种情况。
您应该能够通过将输入控件向左浮动或更改文本的行高来避免此问题。
编辑:这似乎对我来说效果很好..
The input controls have different line-heights from the standard text. So when you place these, natively inline, controls next to text they force the baseline/lineheight to behave differently than the text would behave without it. This also happens if you place an image next to text without floating it (the text aligns itself with the bottom of the image, until it wraps to the next line).
You should be able to circumvent this by floating your input controls to the left, or possibly by changing the
line-height
of the text.Edit: This seemed to work fine for me..
当我尝试将垂直对齐的中间添加到 td 内的另一个控件时,它似乎将所有内容设置在中间。这在 IE 中是一种奇怪的行为,但它确实解决了我的问题。
When I tried adding vertical aligned middle to the other control inside the td, it seems to set all of the at the middle. It is a weird behavior in IE but it did fix my problem.