如何获得等宽度的输入和选择字段 [英] How to get equal width of input and select fields
问题描述
在表单上,我有一个选择和两个输入字段。这些元件是垂直对准的。不幸的是,我不能得到这些元素的宽度相等。
这是我的代码:
< select name =name1style =width:198px>
< option> value1< / option>
< option> value2< / option>
< / select>< br />
< input type =textname =id1style =width:193px>< br />
< input type =textname =id2style =width:193px>对于上面的例子,select元素的最佳宽度是198或199像素(当然我尝试了193像素的像素) ,但差异是主要的)。我认为,这取决于分辨率,在各种计算机和浏览器,因为这些元素没有相等的宽度(有时我认为差异是大约1或2像素)。我试图在div或表行中设置这些元素,但它没有帮助。
问:如何获得这些元素的精确等宽?
解决方案 更新的答案
是如何改变输入/ textarea / select元素使用的框模型,使它们都以相同的方式工作。您需要使用为每个浏览器使用前缀实现的框大小
属性
-ms-box-sizing:content-box;
-moz-box-sizing:content-box;
-webkit-box-sizing:content-box;
box-sizing:content-box;
这意味着我们前面提到的2px差异不存在..
示例,位于 http://www.jsfiddle.net/gaby/WaxTS/5/
注意: em>
如果您重置其边框,则选择
元素将总是小于输入元素。
示例: http:// www .jsfiddle.net / gaby / WaxTS / 2 /
On the form, I have one select and two input fields. These elements are vertically aligned. Unfortunately, I can't get equal width of these elements.
Here's my code:
<select name="name1" style="width:198px">
<option>value1</option>
<option>value2</option>
</select><br/>
<input type="text" name="id1" style="width:193px"><br/>
<input type="text" name="id2" style="width:193px">
For above example, the best width for select element is 198 or 199 px (of course I tried 193px, but the difference is major). I think, it depends on resolution, on various computers and browsers since these elements don't have equal widths (sometimes I thinks difference is about 1 or 2 px). I've tried to set these elements in div or table rows, but it doesn't help.
Q: How could I get precisely equal width of these elements?
解决方案 Updated answer
Here is how to change the box model used by the input/textarea/select elements so that they all behave the same way. You need to use the box-sizing
property which is implemented with a prefix for each browser
-ms-box-sizing:content-box;
-moz-box-sizing:content-box;
-webkit-box-sizing:content-box;
box-sizing:content-box;
This means that the 2px difference we mentioned earlier does not exist..
example at http://www.jsfiddle.net/gaby/WaxTS/5/
note: On IE it works from version 8 and upwards..
Original
if you reset their borders then the select
element will always be 2 pixels less than the input
elements..
example: http://www.jsfiddle.net/gaby/WaxTS/2/
这篇关于如何获得等宽度的输入和选择字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!