Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does an Input width of 100% always overrun the containing div?

Tags:

html

css

please note the fiddle: http://jsfiddle.net/cBWaG/4/

I have a container with a padding. What I want is the input(text) to be 100% width from within the padding. Currently though it overshoots the padding. Suggestions on how to prevent the input from overshooting the container/padding without having to use a fixed width?

Thanks

like image 347
AnApprentice Avatar asked Jul 31 '11 18:07

AnApprentice


People also ask

What does width 100% do in HTML?

If you set the width to 100% on the body element you will have a full page width. This is essentially equivalent to not setting a width value and allowing the default. If you want to use the body element as a smaller container and let the HTML element fill the page, you could set a max-width value on the body.

Why is input bigger than div?

It means that border on the input box is actually inside the width of the input rather than being added onto the outside. This is what is making the input larger than the container. The points he makes about padding also apply for the border. As noted in other answers, it may need width: 100%; height: 100% .

How do you make a div 100 width?

What you could do is set your div to be position: absolute so your div is independent of the rest of the layout. Then say width: 100% to have it fill the screen width. Now just use margin-left: 30px (or whatever px you need) and you should be done.


3 Answers

try this

input[type="text"] {
     width: 100%; 
     box-sizing: border-box;
     -webkit-box-sizing:border-box;
     -moz-box-sizing: border-box;
}

Demo

like image 58
h0mayun Avatar answered Oct 11 '22 14:10

h0mayun


Form inputs are really hard to style. And it's especially hard to make the style look consistent across all browsers. One of the best ways I've found to style text inputs is to get rid of the border, outline, padding, and margin. Then wrap the input in a div with the border, outline, padding, and margin that you want.

I think I achieved what you want. Check out this forked jsFiddle: http://jsfiddle.net/aP2Ju/

like image 35
Matt Bradley Avatar answered Oct 11 '22 14:10

Matt Bradley


The width property of an element in the standard box model does not include padding, margin or borders; it is the width given to the element's contents, not to the box itself. The box is bigger than the width by the size of the padding. Borders are then added, and finally the margin.

Older versions of IE (v4 and 5) used a non-standard box model which is now known as "Quirks mode". This box model placed the padding and borders inside the measured area, so that a box's width property included them. This model does make it easier to have a box that is 100% width.

Both box models have their strengths, but IE was going against the standards when they implemented their box model, and it was one of the reasons that made it difficult for developers to update older sites designed for IE only to work for other browsers.

More recently, a CSS property has been added which allows designers to choose which of these box models they wish to use for any given elements on their page. The property is called box-sizing, and is supported by most browsers in current use; the only major ones that don't support it are IE6 and IE7 (others require a vendor prefix but do support it). See here for a table of browser support for the feature: http://caniuse.com/#search=box-sizing

The other option is simply not to set the width at all. A <div> element by default is set to fill the available width, which basically means 100%, but in a way that works, no matter what the padding and margin are set to, which is basically what you're after. If you've overridden the width and want to set it back to this setting, use width:auto;.

Hope that helps.

like image 24
Spudley Avatar answered Oct 11 '22 12:10

Spudley