100% Width Form Fields with CSS box sizing
by Charlie on June 1, 2012
The Problem
Setting width: 100% on an INPUT or TEXTAREA tag with a border or padding will cause the element to expand outside of its parent container. Unlike most elements, setting display: block; and width: auto; won’t automatically expand a form field to it’s maximum width within its container.
To compensate for this, it has been suggested to wrap each form field inside a DIV, and set padding and border on the DIV. This way, setting width: 100% on the INPUT or TEXTAREA would match the inner width of the containing parent, within the parent’s padding, border, and margins. While this solution works, it adds a ton of unnecessary markup.
The Solution
Luckily, CSS has a cleaner solution, straight out of quirks mode. The well-known IE6 box-model bug is actually a supported rendering mode in CSS (however, ironically not supported in IE6 and 7). By setting the box-sizing attribute in CSS, we can force the width of an element to include the border and padding.
textarea {
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
}
Support
box-sizing is supported in all modern browsers after IE7. To support IE6 and 7, if you must, I’d recommend using separate conditional stylesheets to set the width of the form fields to a lower percentage.








