Why does display:block;width:auto;
on my text input not behave like a div and fill the container width? I was under the impression that a div is simply a block element with auto width. In the following code shouldn't the div and the input have identical dimensions?
How do I get the input to fill the width. 100% width won't work because the input has padding and a border (causing a final width of 1px + 5px + 100% + 5px + 1px). Fixed widths aren't an option, I'm looking for something more flexible.
I'd prefer a direct answer to a workaround, this seems like a CSS quirk and understanding it may be useful later.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>width:auto</title>
<style>
div, input {
border: 1px solid red;
height: 5px;
padding: 5px;
}
input, form {
display: block;
width: auto;
}
</style>
</head>
<body>
<div></div>
<form>
<input type="text" name="foo" />
</form>
</body>
</html>
EDIT: I should point out I can already do this with wrapper workarounds. Apart from this screwing with the page semantics and CSS selector relationships I'm trying to understand the nature of the problem and whether it can be overcome by changing the nature of the INPUT itself.
EDIT 2: Ok, this is TRULY strange! I've found that the solution is to simply add max-width:100%
to an input with width:100%;padding:5px;
. However this raises even more questions (which i'll ask in a seperate question) but it seems that width uses the normal CSS box model and max-width uses the IE border-box model. How very odd.
EDIT 3: Ok, that last one appears to be a bug in FF3. IE8 and Safari4 limit the max-width to 100% + padding + border which is what the spec says to do. Finally, IE got something right.
EDIT 4: Oh my god, this is awesome! In the process of playing with this, and with much help from the venerable gurus Dean Edwards and Erik Arvidsson, I managed to piece together 3 seperate solutions to make a true cross-browser 100% width on elements with arbitrary padding and borders. See answer below. This solution does not require any extra HTML markup, just a class (or selector) and an optional behaviour for legacy IE.