gpt4 book ai didi

html - 为什么不垂直对齐 : middle work with input elements in a table-cell?

转载 作者:太空宇宙 更新时间:2023-11-04 14:00:21 24 4
gpt4 key购买 nike

这是我的代码:

*               { vertical-align: top; margin: 0; }
td { vertical-align: middle; border: 1px solid red; }
td:nth-child(1) { line-height: 3em; }
td:nth-child(2) { line-height: 4em; }
td:nth-child(3) { line-height: 0.1em; }
p, input { outline: 1px solid green; }
<table>
<tr>
<td>
<p>
Some vertically centered text.
</p>
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
</table>

可以看到,pinputline-height: .1em 完全垂直居中,而line-height: 4eminput 被移动到顶部。

经过长时间的研究,我没有找到对这种行为的解释。

我设置了一个 jsfiddle:https://jsfiddle.net/dlenne/8xapwz11/14/ .

最佳答案

您已设置 vertical-align: middletd 上, 每个都是 input 的父级元素。

但是 vertical-align属性(property)不被继承(source)。

因此,一种解决方案是将规则直接应用于输入:

*               { vertical-align: top; margin: 0; }
td { vertical-align: middle; border: 1px solid red; }
td:nth-child(1) { line-height: 3em; }
td:nth-child(2) { line-height: 4em; }
td:nth-child(3) { line-height: .1em; }
p, input { outline: 1px solid green; }
input { vertical-align: middle; } /* new */
<table>
<tr>
<td>
<p>Some vertically centered text.</p>
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
</table>

另一种解决方案只是绕过 line-height通过切换 display 规则来自 inline 的输入值至 block .

*               { vertical-align: top; margin: 0; }
td { vertical-align: middle; border: 1px solid red; }
td:nth-child(1) { line-height: 3em; }
td:nth-child(2) { line-height: 4em; }
td:nth-child(3) { line-height: .1em; }
p, input { outline: 1px solid green; }
input { display: block; } /* new */
<table>
<tr>
<td>
<p>Some vertically centered text.</p>
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
</table>

引用资料:

关于html - 为什么不垂直对齐 : middle work with input elements in a table-cell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36848273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com