简体   繁体   English

CSS高度如何应用于输入字段?

[英]How does CSS height get applied to input fields?

I'm having some difficulty understanding how the css height property is applied to input fields when i use em as a unit. 我使用em作为单位时,在理解css height属性如何应用于input字段时遇到了一些困难。

Take a look at this jsfiddle: jsfiddle 看看这个jsfiddle: jsfiddle

If the em value is equal to the base font size examples one and three should be the same height. 如果em值等于基本字体大小,则示例1和3的高度应相同。 If em is based on what the font size is set to in the element (which I believe is correct), then examples one and two should be the same size. 如果em基于元素中设置的字体大小(我认为是正确的),则示例一和示例二的大小应相同。 As it is, none of these items are the same size. 实际上,这些项目都没有相同的大小。 If I set the height to px then I can force them to all be the same size. 如果将高度设置为px则可以将它们的大小都设置为相同。

What is coming into play that makes all of my input boxes a different size? 是什么使我的所有输入框都具有不同的大小?

CSS: CSS:

.one {
  max-height:3em;
  height: 3em;
  font-size:1em;
  vertical-align: top;
}

.two {
  max-height:6em;
  height: 6em;
  font-size: 0.5em;
  vertical-align:top;
}

.three {
  max-height:3em;
  height: 3em;
  font-size: 0.5em;
  vertical-align:top;
}

HTML: HTML:

<input type="text" class="one" placeholder="one">
<input type="text" class="two" placeholder="two">
<input type="text" class="three" placeholder="three">

If em is based on what the font size is set to in the element (which I believe is correct) 如果em基于元素中设置的字体大小(我认为是正确的)

No, that is incorrect. 不,那是不正确的。

The em given for the height is based on the elements parent's font-size, not the font size given to the element itself. 给定高度的em是基于父元素的字体大小,而不是元素本身的字体大小。

The font size of the input, when em is used, is also based/inherited from its parents font size. 当使用em时,输入的字体大小也基于/继承自其父字体大小。

So this is kind of the whole idea, so they scale equal if parent's font size changes. 因此,这是一个完整的主意,因此如果父级的字体大小更改,它们的缩放比例将相等。

Here is a sample showing that 这是一个示例,显示

 div { font-size: 30px; margin: 20px 0; } .em1 { font-size: 1em; } .em2 { font-size: 2em; } .one { max-height:3em; height: 3em; font-size:1em; vertical-align: top; } .two { max-height:6em; height: 6em; font-size: 0.5em; vertical-align:top; } .three { max-height:3em; height: 3em; font-size: 0.5em; vertical-align:top; } 
 <div> <input type="text" class="one" placeholder="one"> <input type="text" class="two" placeholder="two"> <input type="text" class="three" placeholder="three"> </div> <div class="em1"> <input type="text" class="one" placeholder="one"> <input type="text" class="two" placeholder="two"> <input type="text" class="three" placeholder="three"> </div> <div class="em2"> <input type="text" class="one" placeholder="one"> <input type="text" class="two" placeholder="two"> <input type="text" class="three" placeholder="three"> </div> 

This actually seems like a bug in Chrome / Safari. 实际上,这似乎是Chrome / Safari中的错误。 Firefox and Opera renders them correctly. Firefox和Opera可以正确呈现它们。

One way to fix this so it renders the same in all browsers, are to use rem instead of em : https://www.sitepoint.com/understanding-and-using-rem-units-in-css/ 解决此问题以使其在所有浏览器中呈现相同效果的一种方法是使用rem而不是emhttps : //www.sitepoint.com/understanding-and-using-rem-units-in-css/

However, this has other potential pitfalls and implications 但是,这还有其他潜在的陷阱和影响

This is because you are using em that is equal to the computed font-size for the element to which the em is applied. 这是因为您使用的em等于所应用em的元素的计算字体大小。 For a better explaination read this . 为了获得更好的解释,请阅读此内容

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

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