Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is it in the CSS/DOM that prevents an input box with display: block from expanding to the size of its container
    primarykey
    data
    text
    <h2>Sample HTML/CSS:</h2> <pre><code>&lt;div class="container"&gt; &lt;input type="text" /&gt; &lt;div class="filler"&gt;&lt;/div&gt; &lt;/div&gt; div.container { padding: 5px; border: 1px solid black; background-color: gray; } div.filler { background-color: red; height: 5px; } input { display: block; } </code></pre> <p><a href="http://jsfiddle.net/bPEkb/3/" rel="nofollow noreferrer">http://jsfiddle.net/bPEkb/3/</a></p> <h2>Question</h2> <p>Why doesn't the input box expand to have the same outer width as, say <code>div.filler</code>? That is to say, why doesn't the input box expand to fit its container like other block elements with <code>width: auto;</code> do?</p> <p>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 <code>div.filler</code>.</p> <p>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 <code>input</code> 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.</p> <p>borkweb and Phrogz have both provided exceptional information that takes advantage of the <code>border-box</code> 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 <a href="https://stackoverflow.com/questions/1030793/input-with-displayblock-is-not-a-block-why-not">proposed solution to my practical problem</a>:</p> <p><strong>What about the specification causes input boxes to be formatted unlike ordinary block elements like <code>div</code>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 <code>div</code>s, which do not use the border-box model these days.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload