简体   繁体   English

为什么textarea高于文本输入?

[英]Why textarea higher than text input?

Found it, sorry, changed padding and forgot to change it for textarea. 发现它,抱歉,更改填充并忘记更改textarea。 My mistake. 我的错。 Will delete ) 会删除)

I'm trying to make table with text input and textarea input on the same row, but cannot make textarea the same height as the text input. 我正在尝试在同一行上创建带有文本输入和textarea输入的表,但不能使textarea与文本输入的高度相同。 Could anyone please tell me, why text input and textarea have different height and how to fix that? 任何人都可以告诉我,为什么文本输入和textarea有不同的高度以及如何解决这个问题? As you can see, textarea is 2px higher and chrome inspector tells me the same. 正如您所看到的,textarea高出2px,而chrome检查员告诉我同样的情况。 What's wrong and how to fix that? 怎么了,怎么解决? (sorry for a large text, site told me that I've got "mostly code" and have to add some details. Have no idea, what else may I add to that code, the problem is obvious))) Thank you! (对不起一个大文本,网站告诉我,我有“大部分代码”,并且必须添加一些细节。不知道,我还有什么可以添加到该代码,问题很明显)))谢谢!

 div { background: #ddd; } input#te { position: relative; outline:0; border: 1px solid #a78; padding-left: 0.5em; padding-right: 0.5em; margin: 0; background: transparent; height: 5em; line-height: 5em; vertical-align: middle; } textarea#ta { position: relative; outline:0; border: 1px solid #a78; padding-left: 0.5em; padding-right: 0.5em; margin: 0; background: transparent; height: 5em; vertical-align: middle; resize: none; } 
 <table> <tr> <td> <div> <input type=text id="te" value="input is 2px smaller in chrome"> </div> </td> <td> <div> <textarea id="ta">textarea is 2px higher in chrome</textarea> </div> </td> </tr> </table 

the textarea has padding top-bottom 2px, textarea填充顶部2px,

the input only 1px. 输入只有1px。

change the padding on textarea: 更改textarea上的填充:

 div { background: #ddd; } input#te { position: relative; outline:0; border: 1px solid #a78; padding-left: 0.5em; padding-right: 0.5em; margin: 0; background: transparent; height: 5em; line-height: 5em; vertical-align: middle; } textarea#ta { position: relative; outline:0; border: 1px solid #a78; padding: 1px 0.5em; margin: 0; background: transparent; height: 5em; vertical-align: middle; resize: none; } 
 <table> <tr> <td> <div> <input type=text id="te" value="input is 2px smaller in chrome"> </div> </td> <td> <div> <textarea id="ta">textarea is 2px higher in chrome</textarea> </div> </td> </tr> </table 

To fix the problem, add box-sizing: border-box to the input element AND to the textarea element (for the fix to work in Chrome). 要解决此问题,请将box-sizing: border-box到input元素和textarea元素(以便在Chrome中使用修复)。

 div { background: #ddd; } input#te { position: relative; outline: 0; border: 1px solid #a78; padding-left: 0.5em; padding-right: 0.5em; margin: 0; background: transparent; height: 5em; line-height: 5em; vertical-align: middle; box-sizing: border-box; } textarea#ta { position: relative; outline: 0; border: 1px solid #a78; padding-left: 0.5em; padding-right: 0.5em; margin: 0; background: transparent; height: 5em; vertical-align: middle; resize: none; box-sizing: border-box; } 
 <table> <tr> <td> <div> <input type=text id="te" value="input is 2px smaller in chrome"> </div> </td> <td> <div> <textarea id="ta">textarea is 2px higher in chrome</textarea> </div> </td> </tr> </table> 

box-sizing: border-box; is what you need to place onto the input and text area. 是您需要放置在输入和文本区域。 Also works well for select. 也适用于选择。

By using box-sizing: border-box; 通过使用box-sizing: border-box; it means you dont have to adjust all of your widths, padding and line-height and you can have everything the same 这意味着你不必调整所有的宽度,填充和线高,你可以拥有一切相同的东西

 div { background: #ddd; } input#te { position: relative; outline:0; border: 1px solid #a78; padding-left: 0.5em; padding-right: 0.5em; margin: 0; background: transparent; height: 5em; line-height: 5em; vertical-align: middle; box-sizing: border-box; } textarea#ta { position: relative; outline:0; border: 1px solid #a78; padding-left: 0.5em; padding-right: 0.5em; margin: 0; background: transparent; height: 5em; vertical-align: middle; resize: none; box-sizing: border-box; } 
 <table> <tr> <td> <div> <input type=text id="te" value="input is 2px smaller in chrome"> </div> </td> <td> <div> <textarea id="ta">textarea is 2px higher in chrome</textarea> </div> </td> </tr> </table 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM