Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the input and the select do not get the same width?

Tags:

html

css

I have a table with two rows. The first row contains an input and the second row contains a select. Even though I have set their widths to be 100%, the select box is a few pixels smaller than the input. Any ideas why is this so and how can I set their widths to be equal to each other and as big as possible (e.g. %100) in a way that works across all (A-grade) browsers?

<table width="100%" style="margin-top: 5px;">
<tr>
    <td width="35"><label for="desc">Description</label></td>
    <td>
        <input type="text" style="width: 100%;" name="desc" id="desc" />
    </td>
</tr>
<tr>
    <td width="35"><label for="group">Group</label></td>
    <td>
        <select id="group" name="group" style="width: 100%; line-height: 17px;">            
            <option value="val">name</option>            
        </select>
    </td>
</tr>
</table>
like image 387
Behrang Avatar asked Jun 11 '10 05:06

Behrang


People also ask

How do you make input and select elements the same width?

To create the inputs of the same width, we have to use some CSS attributes in our program. box-sizing: It defines how the height and width of the element are calculated. moz-box-sizing: It is supported in the Mozilla Firefox browser only. -webkit-box-sizing: It is supported in the Google Chrome browser only.

How do you change the input field width?

The width attribute specifies the width of the <input> element. Note: The width attribute is used only with <input type="image"> . Tip: Always specify both the height and width attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded.

What is the default width of input?

The width will be the value of the size attribute in characters that the input will display. Value should be an integer greater than zero. When not defined, the width of most input elements defaults to 20.


1 Answers

This is because with an <input>, the border and padding is added on to the width (like with most other elements). With a <select>, the border and padding is included in the width, just like in the old IE quirks mode.

You can get round this by increasing the width to take account of this, if you know the width in pixels:

input  { width: 200px; padding: 10px; border-width:5px; }
select { width: 230px; padding: 10px; border-width:5px; }

Or (if you can rely on browser support) you can use the new CSS3 box-sizing property to make them behave consistently, and draw padding and border outside of the element width:

input, select {
  width: 200px;
  padding: 10px;
  border-width:5px;
  -webkit-box-sizing: content-box; /* Safari/Chrome, other WebKit */
  -moz-box-sizing: content-box;    /* Firefox, other Gecko */
  box-sizing: content-box;         /* Opera/IE 8+ */
}

Alternatively, you can set box-sizing to border-box to make the inputs behave like the selects, with padding drawn inside the width of the box.

Tested in Chrome, IE8, FF

like image 130
Jim Avatar answered Sep 19 '22 02:09

Jim