74

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...

7
  • 26
    Can I tell you a dirty little secret? I use tables to format my forms. It's just way too easy. But Sssssshh, keep that on the down low. Commented May 22, 2009 at 0:17
  • In all seriousness, though, have you tried setting margin and padding to 0? Commented May 22, 2009 at 0:18
  • Paolo, I did originally (and it was easy to make the inputs the same width) but I have strived to do it table less and have succeeded over the last 8 months or so ... this one is just giving me grief. Commented May 22, 2009 at 0:21
  • I had padding (to give between the borders and text some room to breathe) and when I removed the padding, the difference is less (but then my text is hard up against the borders) Commented May 22, 2009 at 0:22
  • Oh, I know. I love CSS and I obviously agree with its ideas of semantic relevance and such, but when it comes to forms I just like the flexibility and simplicity of tables. Anyways, I'm not sure why this is happening. Form fields are infamous for just how painful it is to get them to style how you want. Sorry. :) Commented May 22, 2009 at 0:25

3 Answers 3

168

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Might give this a try... do you have any recommended reading on the subject?
this worked for me, brilliant. This has been bugging me for ages.
You also may want to give the elements a standard vertical-alignment if they are contained within the same element.
For IE7 you could use box-sizing-polyfill. Just add *behavior: url(boxsizing.htc); after box-sizing: border-box;
If anyone else is scratching their heads trying this with a "search" type input, and not text, be sure to target input[type="search"] as this defaults to border-sizing: content-box in some browsers.
|
0

padding will make the text be closer to the edge of the box.

try setting the margin to 0px, and if that seems right, play around with it (like just setting margin-left only)

1 Comment

I tried using padding: 0 & margin: 0 with !important on the inputs, selects and input-row div, but it just bunches them up real tight. The select box is about 2px narrower (which may have something to do with the border)
0

Had same problems with 100% width table and cells, with a textbox (also with width at 100%) wider than the table cell.

This solved my problem in the css:

table td
{
    padding-right: 8px;
}

Not the best solution ever, cause you probably get some additional space to the right side. But at least it's not hiding anymore!

1 Comment

I'm doubtful about such absolute sizes. If you use another browser, another window manager / windows style, this is likely to not work any more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.