Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning text and select boxes to the same width in CSS?

Ok this is seemingly impossible to get right. I have a text box and a select box. I want them to be the same width exactly so they line up on the left margin and the right margin.

<!DOCTYPE html> <html>     <head>         <style type="text/css">             input, select {                 width: 200px;             }         </style>     </head>     <body>         <input type="text" value="ABC"><br>         <select>             <option>123</option>         </select>     </body> </html> 

That would do it you think, right? Nope. The select box is 6px shorter in Firefox. See screenshot. screenshot in firefox

Ok so lets edit the code and make two styles.

<style type="text/css">     input {         width: 200px;     }     select {         width: 206px;     } </style> 

Ok that works!

Fixed in Firefox

Oh wait, better test in Chrome...

chrome screenshot

FFFFFFFUUUUUUUUUUUU

Can someone tell me how to line these up in all browsers? Why can't I just do width: 200px on all, why do all the browsers display it differently? Also while we're at it why is the text box and select box different heights? How do we get them to the same height? Have tried height and line-height no no avail.


Solution:

Ok I've found the solution with some help from the answers below. The key is to use the box-sizing: border-box property so when you specify the width that includes the border and padding. See excellent explanation here. Then the browser can't stuff it up.

Code is below, have also set the height of the boxes to the same size and indented the text inside the box so it lines up. You also need to set the border as Chrome has a really weird looking border it uses for select boxes which will throw out the alignment. This will work for HTML5 sites (e.g. supporting IE9, Firefox, Chrome, Safari, Opera etc).

<!DOCTYPE html> <html>     <head>         <style type="text/css">             input, select {                 width: 200px;                 border: 1px solid #000;                 padding: 0;                 margin: 0;                 height: 22px;                 -moz-box-sizing: border-box;                 -webkit-box-sizing: border-box;                 box-sizing: border-box;             }             input {                 text-indent: 4px;             }         </style>     </head>     <body>         <input type="text" value="ABC"><br>         <select>             <option>123</option>             <option>123456789 123123123123</option>             <option>123456789 123123123123 134213721381212</option>         </select>     </body> </html> 

Just one final warning you may not want input buttons, checkboxes etc to be included in this styling so use the input:not([type='button']) to not apply it to certain types or use input[type='text'], input[type='password'] to specify ones you do want it to apply to.

like image 463
zuallauz Avatar asked Apr 03 '12 20:04

zuallauz


People also ask

How do you make boxes the same size in CSS?

The box-sizing property allows us to include the padding and border in an element's total width and height. If you set box-sizing: border-box; on an element, padding and border are included in the width and height: Both divs are the same size now!

How do you make all text boxes the same size in HTML?

The key is to use the box-sizing: border-box property so when you specify the width that includes the border and padding. See excellent explanation here. Then the browser can't stuff it up. Code is below, have also set the height of the boxes to the same size and indented the text inside the box so it lines up.

How do I align a box in CSS?

Center Align Elements To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.


1 Answers

This is because the <input type="text" /> element has a slightly different default style to the <select> element e.g. the border, padding and margin values may be different.

You can observe these default styles through an element inspector such as Firebug for Firefox or the built-in one for Chrome. Futhermore, these default stylesheets differ from browser to browser.

You have two options:

  1. Explicitly set the border, padding and margin values to be the same for both elements
  2. A CSS reset stylesheet to be included before your own CSS stylesheets

I would go with option 1. because option 2. requires a lot of work and you'll end up with tons of unnecessary CSS. But some web developers prefer to start from scratch and have complete control.

like image 178
Chris Cannon Avatar answered Oct 13 '22 05:10

Chris Cannon