简体   繁体   English

输入类型的宽度=文本元素

[英]Width of input type=text element

How come when I do this: 我怎么做的时候:

<input type="text" style="width: 10px; padding: 2px"/>
<div style="width: 10px; border: solid 1px black; padding: 2px">&nbsp;</div>

the input ends up 2 px wider than the div in both IE6 and FF3? 输入最终比IE6和FF3中的div宽2 px? What am I missing? 我错过了什么?

EDIT: As many people have said, the border is the issue. 编辑:正如许多人所说,边界是问题。 If I set border: 0px on the input, it will have the same width as the div with a 0 px border (verified by wrapping it inside a bordered SPAN). 如果我在输入上设置border:0px,它将具有与具有0 px边框的div相同的宽度(通过将其包装在有边界的SPAN中进行验证)。

However, when I measure the elements in paint, the div has a 14 px interior, just as expected (10+2+2). 但是,当我测量绘画中的元素时,div的内部空间为14像素,正如预期的那样(10 + 2 + 2)。 The input, however, has a 16 px interior, and then a border outside of that. 但是,输入内部有16像素,然后是外部边框。 Why is this? 为什么是这样? Probably not a bug since it happens in both IE6 and FF3, but I dont understand it. 可能不是一个错误,因为它发生在IE6和FF3,但我不明白。

I believe that is just how the browser renders their standard input. 我相信这就是浏览器如何呈现标准输入。 If you set a border on the input: 如果在输入上设置边框:

<input type="text" style="width: 10px; padding: 2px; border: 1px solid black"/>
<div style="width: 10px; border: solid 1px black; padding: 2px"> </div>

Then both are the same width, at least in FF. 然后两者都是相同的宽度,至少在FF中。

The visible width of an element is width + padding + border + outline , so it seems that you are forgetting about the border on the input element. 元素的可见宽度是width + padding + border + outline ,因此您似乎忘记了输入元素上的边框。 That is, to say, that the default border width for an input element on most (some?) browsers is actually calculated as 2px, not one. 也就是说,大多数(某些?)浏览器上的输入元素的默认边框宽度实际上计算为2px,而不是1。 Hence your input is appearing as 2px wider. 因此,您的输入显示为2px更宽。 Try explicitly setting the border-width on the input, or making your div wider. 尝试在输入上明确设置border-width ,或使div更宽。

边框输入宽度为10 + 2倍1 px

I think you are forgetting about the border. 我想你忘记了边界。 Having a one-pixel-wide border on the Div will take away two pixels of total length. Div上有一个像素宽的边框会占用总长度的两个像素。 Therefore it will appear as though the div is two pixels shorter than it actually is. 因此,它看起来好像div比它实际上短两个像素。

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

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