Of course, width: 100% on a block element fills the container, but only if border, padding, and margin are zero. Putting such an input in a containing div with padding does the trick, but I’m curious why simply input {display: block; width: auto;} doesn’t work. Suggestions?
Of course, width: 100% on a block element fills the container, but only if
Share
I agree with centr0 as to why
width: autodoesn’t work.If you actually want to get an
inputto be full width of its container, then the following works. Given:Do this with the css:
Explanation: Obviously, the -14px margin on the
inputcounteracts the padding added to theformelement. What is not so obvious is where the14pxcame from. It is equal to the left/right 5px padding of theinputplus the default forinputelement’s border width (2px on all the browsers I tested). So 2 + 5 + 5 + 2 = 14.To really be sure you are consistent cross-browser, you would want to explicitly set the border width to what you desired for the
input. If you want a different padding (or thicker/thinner borders), then just redo the math. A 3px padding would be 10px instead of 14px, a 7px padding would be 18px instead of 14px.For the example, you can set the width of
#Container(could also be thebodyitself that is just defaulting to 100% of the page width) to what you desire, and the above css should match its width.