Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select inputs and text inputs in HTML - Best way to make equal width?

Tags:

css

xhtml

I've got a simple form like so (illustrative purposes only)...

<form>     <div class="input-row">       <label>Name</label>       <input type="text" name="name" />    </div>     <div class="input-row">       <label>Country</label>       <select name="country">          <option>Australia</option>          <option>USA</option>       </select>    </div>  </form> 

My layout method using CSS is as follows...

form  {     width: 500px; }  form .input-row {     display: block;     width: 100%;     height: auto;     clear: both;      overflow: hidden; /* stretch to contain floated children */     margin-bottom: 10px; }  form .input-row label {     display: block;     float: left; }  form .input-row input, form .input-row select {     display: block;     width: 50%;     float: right;     padding: 2px; } 

This all aligns quite nicely, except my select element (in Firefox anyway) isn't always the same width as my other input elements. It generally is narrower by a few pixels.

I've tried changing the width to a pixel size (e.g. 200px) but it has not made a difference.

What is the best way to get these to all have the same width? I hope it doesn't resort to me setting the select's width individually, or putting them into tables...

like image 686
alex Avatar asked May 22 '09 00:05

alex


People also ask

How do you make an input field the same size in 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. b. add these lines in your CSS (external on internal CSS whichever you are using).

How do you control input width in HTML?

1. Set width in HTML. In HTML, you can use the width attribute to set the width of an element. Alternatively, you can also use the size attribute to define the width of the <input> .

How do you increase the width of a selection box in HTML?

Answer: Use the CSS :focus pseudo-class By default the size of the <select> element is depend on the size of the largest <option> text. However, sometimes it is useful to set a fixed width to the select box and increase its size back to the original size when the user tries to select some option (i.e. on focus).


1 Answers

The solution is to specify box model for form elements, and browsers tend to agree most when you use border-box:

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

There's normalize.css project that aggregates such tricks.

like image 71
Kornel Avatar answered Sep 17 '22 15:09

Kornel