简体   繁体   English

如何在HTML中计算文本框的高度

[英]How height of a text box is calculated in HTML

Though I have already referred box model but could not track How height of a text box or any HTML element is being calculated in following fiddle 虽然我已经提到了框模型,但是无法跟踪下面的小提琴如何计算文本框或任何HTML元素的高度

 <input type="text" id="test" onclick= "alert('computed height ' + window.getComputedStyle(this).height + ' and font-size ' + window.getComputedStyle(this)['font-size'])" name="test" value="Click here to get height" style=" padding: 0px; border: 0px; line-height: 1; "/> 

I am not able to find how height of the text box is 15px even if with 0 border,0 margin,0 padding and 13.33px font size. 即使使用0边框,0边距,0填充和13.33px字体大小,我也无法找到文本框的高度是15px。

EDIT- Though firefox works perfect by calculations, but after zooming it cuts some of the content 编辑-虽然通过计算,firefox可以完美地工作,但是缩放后它会切掉一些内容 Firefox上的文字

Chrome上的文字

It Seems chrome applies some formatting to render better than firefox. 似乎chrome应用了某些格式以使其比firefox更好。

Ok Here is the explanation, I learnt by tring. 好的,这是我通过练习中学到的解释。

Refer the specification http://www.w3.org/TR/CSS2/visudet.html#inline-non-replaced 请参阅规范http://www.w3.org/TR/CSS2/visudet.html#inline-non-replaced

which says 这说

The height of the content area [of inline elements] should be based on the font, but this specification does not specify how. [内联元素]的内容区域的高度应基于字体,但是此规范未指定如何。 A UA may, eg, use the em-box or the maximum ascender and descender of the font... UA可以例如使用em-box或字体的最大升序和降序...

Chrome and fire-fox have different implementation for line-height Chrome和fire-fox的行高实现方式不同

Chrome- Chrome will ignore line-height if it is not able to render it perfectly and will increase the space to fit text perfectly. 镀铬 Chrome会忽略行高度,如果它是不能够完美呈现,并会增加空间完全适合文本。

FireFox- FireFox will also ignore the line-height if it is less than font-size but it will provide exact space provided as font-size even if is not able to render it nicely. FireFox-如果 FireFox小于font-size,它也将忽略行高,但即使无法很好地呈现,它也将提供作为font-size提供的准确空间。

did you check the line-height? 你检查行高了吗? try setting it to some px value 尝试将其设置为一些px值

if the height is not specified, and border, padding are 0 then it will be the line-height value that is the cause. 如果未指定高度,并且border,padding为0,则将是导致行高的值。 Each browser have it's own pre defined css. 每个浏览器都有自己的预定义CSS。 so you have to reset most of them in your css (mostly at the top) so all browsers render the same 因此您必须在CSS中重置大多数(大多数位于顶部),以便所有浏览器呈现相同的

edit: I meant, line-height:1 will be different in each browser based on the browsers predefined styles, did you reset the line-height on the body initially. 编辑:我的意思是,您最初是否在主体上重置了行高,但每个浏览器的line-height:1会有所不同,具体取决于浏览器的预定义样式。 Easy way to find out is giving the line-height a px value and check all browsers 找出问题的简单方法是给line-height一个px值并检查所有浏览器

Why you not use offset. 为什么不使用偏移量。 this.offsetHeight

see this code 看到这个代码

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

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