How to get equal width of input and select fields

HtmlCssHtml SelectBorder Box

Html Problem Overview


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?

Html Solutions


Solution 1 - Html

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/

Solution 2 - Html

I tried Gaby's answer (+1) above but it only partially solved my problem. Instead I used the following CSS, where content-box was changed to border-box:

input, select {
    -webkit-box-sizing: border-box;
       -moz-box-sizing: border-box;
            box-sizing: border-box;
}

Solution 3 - Html

Add this code in css:

 select, input[type="text"]{
      width:100%;
      box-sizing:border-box;
    }

Solution 4 - Html

First set the width of each element equal, in your case width either 193px or 198px. Then you can follow one of them below.

a. add this line box-sizing: border-box; in the style of you select and input. just like style="width:198px; box-sizing: border-box;"

or

b. add these lines in your CSS (external on internal CSS whichever you are using).

select, input{
      box-sizing: border-box;
    }

Solution 5 - Html

create another class and increase the with size with 2px example

.enquiry_fld_normal{
width:278px !important; 
}

.enquiry_fld_normal_select{
width:280px !important; 
 }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionluk4443View Question on Stackoverflow
Solution 1 - HtmlGabriele PetrioliView Answer on Stackoverflow
Solution 2 - HtmltomsvView Answer on Stackoverflow
Solution 3 - Html151291View Answer on Stackoverflow
Solution 4 - HtmlRubelView Answer on Stackoverflow
Solution 5 - HtmlionMobDevView Answer on Stackoverflow