Newbie CSS question. I thought width:auto for a display:block element meant ‘fill available space’. However for an <input> element this doesn’t seem to be the case. For example:
<body>
<form style='background:red'>
<input type='text' style='background:green; display:block; width:auto'>
</form>
</body>
Two questions then:
-
Is there a definition of exactly what
width:autodoes mean? The CSS spec seems vague to me, but maybe I missed the relevant section. -
Is there a way to achieve my expected behavior for a input field – ie. fill available space like other block level elements do?
Thanks!
An
<input>‘s width is generated from itssizeattribute. The defaultsizeis what’s driving the auto width.You could try
width:100%as illustrated in my example below.Doesn’t fill width:
Fills width:
Smaller size, smaller width:
UPDATE
Here’s the best I could do after a few minutes. It’s 1px off in FF, Chrome, and Safari, and perfect in IE. (The problem is #^&* IE applies borders differently than everyone else so it’s not consistent.)