Sample HTML/CSS:
<div class="container">
<input type="text" />
<div class="filler"></div>
</div>
div.container {
padding: 5px;
border: 1px solid black;
background-color: gray;
}
div.filler {
background-color: red;
height: 5px;
}
input {
display: block;
}
Question
Why doesn't the input box expand to have the same outer width as, say div.filler
? That is to say, why doesn't the input box expand to fit its container like other block elements with width: auto;
do?
I tried checking the "User Agent CSS" in Firebug to see if I could come up with something there. No luck. I couldn't find any specific differences in CSS that I could specifically link to the input box behaving differently from the regular div.filler
.
Besides curiousity, I'd like to know why this is to get to the bottom of it to figure out a way to set width once and forget it. My current practice of explicitly setting the width of both input
and its containing block element seems redundant and less than modular. While I'm familiar with the technique of wrapping the input element in a div then assigning to the input element negative margins, this seems quite undesirable.
borkweb and Phrogz have both provided exceptional information that takes advantage of the border-box
box model from the old days. Thanks for this! I'd like to reiterate the focus of my question that I intend to keep distinct from the proposed solution to my practical problem:
What about the specification causes input boxes to be formatted unlike ordinary block elements like div
s? The border-box solution is wonderful, but it doesn't satisfy the desire to figure out why input boxes are this way in the first place and why they can't be made to behave exactly like ordinary div
s, which do not use the border-box model these days.
width:auto;
imply in this situation? It doesn't imply "full parent width" – Multifacetedwidth: auto;
is shotty at best. I've only ever used it when trying to force a non-floated block element to take up the rest of the container width next to a floated element. I'll remove it to avoid confusion in this case, although it seems not to make a difference. – Peepwidth: auto;
is generally the default setting. There's reasons to use it in a stylesheet, but in this case I'm almost certain that what you want iswidth: 100%;
– Multifacetedwidth: 100%
is insufficient because it sets theinput
width to the inner width of the container, and subsequent padding and borders will be in excess. – Peepwidth:auto
but what that means might vary. (Why or where this is codified, I don't know.) – Galatians