<p>I'm trying to let an <code>&lt;input type="text"&gt;</code> (henceforth referred to as “textbox”) fill a parent container by settings its <code>width</code> to <code>100%</code>. This works until I give the textbox a padding. This is then added to the content width and the input field overflows. Notice that in Firefox this only happens when rendering the content as standards compliant. In quirks mode, another box model seems to apply.</p>

<p>Here's a minimal code to reproduce the behaviour in all modern browsers.</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-css lang-css prettyprint-override"><code>#x {
  background: salmon;
  padding: 1em;
}

#y, input {
  background: red;
  padding: 0 20px;
  width: 100%;
}</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div id="x"&gt;
  &lt;div id="y"&gt;x&lt;/div&gt;
  &lt;input type="text"/&gt;
&lt;/div&gt;</code></pre>
</div>
</div>
</p>

<p>My question: <strong>How do I get the textbox to fit the container?</strong></p>

<p><em>Notice</em>: for the <code>&lt;div id="y"&gt;</code>, this is straightforward: simply set <code>width: auto</code>. However, if I try to do this for the textbox, the effect is different and the textbox takes its default row count as width (even if I set <code>display: block</code> for the textbox).</p>

<p>EDIT: David's solution would of course work. However, I do not want to modify the HTML – I do especially not want to add dummy elements with no semantic functionality. This is a typical case of <a href="http://en.wiktionary.org/wiki/Citations:divitis" rel="nofollow noreferrer">divitis</a> that I want to avoid at all cost. This can only be a last-resort hack.</p>

[{'answer_id': 52575, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 0, 'selected': False, 'text': '<p>i believe you can counter the overflow with a negative margin. ie</p>\n\n<pre><code>margin: -1em;\n</code></pre>\n'}, {'answer_id': 52633, 'author': 'David Kolar', 'author_id': 3283, 'author_profile': 'https://Stackoverflow.com/users/3283', 'pm_score': 5, 'selected': False, 'text': '<p>You can surround the textbox with a <code>&lt;div&gt;</code> and give that <code>&lt;div&gt;</code> <code>padding: 0 20px</code>. Your problem is that the 100% width does not include any padding or margin values; these values are added on top of the 100% width, thus the overflow.</p>\n'}, {'answer_id': 52896, 'author': 'Matthew M. Osborn', 'author_id': 5235, 'author_profile': 'https://Stackoverflow.com/users/5235', 'pm_score': 1, 'selected': False, 'text': '<p>This behavior is caused by the different interpretations of the box model.  The correct box model states that the width applies only to the content and padding and margin add on to it.  So therefore your are getting 100% plus a 20px right and left padding equaling 100%+40px as the total width.  The original IE box model, also known as quirks mode, includes padding and margin in the width.  So the width of your content would be 100% - 40px in this case.  This is why you see two different behaviors.  As far as I know there is no solution for this there is however a work around by setting the width to say 98% and the padding to 1% on each side.</p>\n'}, {'answer_id': 52985, 'author': 'Matthew M. Osborn', 'author_id': 5235, 'author_profile': 'https://Stackoverflow.com/users/5235', 'pm_score': 1, 'selected': False, 'text': '<p>@Domenic this does not work. width auto does nothing more then the default behavior of that element because the initial value of width is auto ( see page 164, Cascading Style Sheets Level 2 Revision 1 Specification).  Assigning a display of type block does not work either, this simply tell the browser to use a block box when displaying the element and does not assign a default behavior of taking as much space as possible like a div does ( see page 121, Cascading Style Sheets Level 2 Revision 1 Specification).  That behavior is handled by the visual user agent not CSS or HTML definition.</p>\n'}, {'answer_id': 53074, 'author': 'Ben', 'author_id': 5005, 'author_profile': 'https://Stackoverflow.com/users/5005', 'pm_score': 2, 'selected': False, 'text': '<p>Because of the way the <a href="http://reference.sitepoint.com/css/boxmodel" rel="nofollow noreferrer">Box-Modell</a> is defined and implemented I don\'t think there is a css-only solution to this problem. (Apart from what <a href="https://stackoverflow.com/questions/52563/css-textbox-to-fill-parent-container#52896">Matthew</a> described: using percentage for the padding as well, e.g. width: 94%; padding: 0 3%;)</p>\n\n<p>You could however build some Javascript-Code to calculate the width dynmically on page-load... hm, and that value would of course also have to be updated every time the browserwindow is resized. </p>\n\n<p>Interesting by-product of some testing I\'ve done: Firefox <strong>does</strong> set the width of an input field to 100% if additionally to <code>width: 100%;</code> you also set max-width to 100%. This doesn\'t work in Opera 9.5 or IE 7 though (haven\'t tested older versions).</p>\n'}, {'answer_id': 1082765, 'author': 'Aseem Kishore', 'author_id': 132978, 'author_profile': 'https://Stackoverflow.com/users/132978', 'pm_score': 2, 'selected': False, 'text': '<p>This is unfortunately not possible with pure CSS; HTML or Javascript modifications are necessary for any non-trivial flexible-but-constrained UI behavior. CSS3 columns will help in this regard somewhat, but not in scenarios like yours.</p>\n\n<p>David\'s solution is the cleanest. It\'s not really a case of divitis -- you\'re not adding a bunch of divs unnecessarily, or giving them classnames like "p" and "h1". It\'s serving a specific purpose, and the nice thing in this case is that it\'s also an extensible solution -- e.g. you can then add rounded corners at any time without adding anything further. Accessibility also isn\'t affected, as they\'re empty divs.</p>\n\n<p>Fwiw, here\'s how I implement all of my textboxes:</p>\n\n<pre><code>&lt;div class="textbox" id="username"&gt;\n    &lt;div class="before"&gt;&lt;/div&gt;\n    &lt;div class="during"&gt;\n        &lt;input type="text" value="" /&gt;\n    &lt;/div&gt;\n    &lt;div class="after"&gt;&lt;/div&gt;\n&lt;/div&gt;\n</code></pre>\n\n<p>You\'re then free to use CSS to add rounded corners, add padding like in your case, etc., but you also don\'t have to -- you\'re free to hide those side divs altogether and have just a regular input textbox.</p>\n\n<p>Other solutions are to use tables, e.g. Amazon uses tables in order to get flexible-but-constrained layout, or to use Javascript to tweak the sizes and update them on window resizes, e.g. Google Docs, Maps, etc. all do this.</p>\n\n<p>Anyway, my two cents: don\'t let idealism get in the way of practicality in cases like this. :) David\'s solution works and hardly clutters up HTML at all (and in fact, using semantic classnames like "before" and "after" is still very clean imo).</p>\n'}, {'answer_id': 4472431, 'author': 'Keith Walton', 'author_id': 22448, 'author_profile': 'https://Stackoverflow.com/users/22448', 'pm_score': 0, 'selected': False, 'text': '<p>The default padding and border will prevent your textbox from truly being 100%, so first you have to set them to 0:</p>\n\n<pre class="lang-css prettyprint-override"><code>input {\n    background: red;\n    padding: 0;\n    width: 100%;\n    border: 0; //use 0 instead of "none" for ie7 \n}\n</code></pre>\n\n<p>Then, put your border and any padding or margin you want in a div around the textbox:</p>\n\n<pre class="lang-css prettyprint-override"><code>.text-box {\n    padding: 0 20px;\n    border: solid 1px #000000;\n}\n\n&lt;body&gt;\n    &lt;div id="x"&gt;\n        &lt;div id="y"&gt;x&lt;/div&gt;\n        &lt;div class="text-box"&gt;&lt;input type="text"/&gt;&lt;/div&gt;\n    &lt;/div&gt;\n&lt;/body&gt;\n</code></pre>\n\n<p>This should allow your textbox to be expandable and the exact size you want without javascript.</p>\n'}, {'answer_id': 5405832, 'author': 'studioromeo', 'author_id': 308886, 'author_profile': 'https://Stackoverflow.com/users/308886', 'pm_score': 6, 'selected': True, 'text': '<p>With CSS3 you can use the box-sizing property on your inputs to standardise their box models.\nSomething like this would enable you to add padding and have 100% width:</p>\n\n<pre class="lang-css prettyprint-override"><code>input[type="text"] {\n    -webkit-box-sizing: border-box; // Safari/Chrome, other WebKit\n    -moz-box-sizing: border-box;    // Firefox, other Gecko\n    box-sizing: border-box;         // Opera/IE 8+\n}\n</code></pre>\n\n<p>Unfortunately this won\'t work for IE6/7 but the rest are fine (<a href="http://www.quirksmode.org/css/user-interface/" rel="noreferrer">Compatibility List</a>), so if you need to support these browsers your best bet would be Davids solution.</p>\n\n<p>If you\'d like to read more check out <a href="http://css-tricks.com/box-sizing/" rel="noreferrer">this brilliant article by Chris Coyier</a>.</p>\n\n<p>Hope this helps!</p>\n'}, {'answer_id': 59520751, 'author': 'Andyba', 'author_id': 1851028, 'author_profile': 'https://Stackoverflow.com/users/1851028', 'pm_score': 2, 'selected': False, 'text': '<p><strong>How do I get the textbox to fit the container in 2019?</strong></p>\n\n<p>Just use display: flex;</p>\n\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-css lang-css prettyprint-override"><code>#x {\r\n  background: salmon;\r\n  padding: 1em;\r\n  display: flex;\r\n  flex-wrap: wrap;\r\n}\r\n\r\n#y, input {\r\n  background: red;\r\n  padding: 0 20px;\r\n  width: 100%;\r\n}</code></pre>\r\n<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div id="x"&gt;\r\n  &lt;div id="y"&gt;x&lt;/div&gt;\r\n  &lt;input type="text"/&gt;\r\n&lt;/div&gt;</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}, {'answer_id': 63966907, 'author': 'hoang21', 'author_id': 3579956, 'author_profile': 'https://Stackoverflow.com/users/3579956', 'pm_score': 0, 'selected': False, 'text': "<p>To make the input fill up width of parent, there're 3 attributes to set: width: 100%, margin-left: 0, margin-right: 0.</p>\n<p>I just guess zero margin setting can help, and I had tried it, however I don't know why margin (left and right; of course top and bottom margins don't affect here) should to be zero to make it works. :-)</p>\n<pre><code>input {\n  width: 100%;\n  margin-left: 0;\n  margin-right: 0;\n}\n</code></pre>\n<p>Note: You may need to set box-sizing to border-box to make sure the padding don't affect the result.</p>\n"}, {'answer_id': 64799327, 'author': 'G Hasse', 'author_id': 9923799, 'author_profile': 'https://Stackoverflow.com/users/9923799', 'pm_score': 0, 'selected': False, 'text': '<p>I use to solve this with CSS-only tables. A little bit long example but\nimportant for all who wants to make entry screens for large amount of fields\nfor databases...</p>\n<p>// GH</p>\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>// NO JAVA !!! ;-)</code></pre>\r\n<pre class="snippet-code-css lang-css prettyprint-override"><code>html {\n  height: 100%;\n}\n\nbody {\n  position: fixed;\n  margin: 0px;\n  padding: 0px;\n  border: 2px solid #FF0000;\n  width: calc(100% - 4px);\n  /* Demonstrate how form can fill body */\n  min-height: calc(100% - 120px);\n  margin-top: 60px;\n  margin-bottom: 60px;\n}\n\n\n/* Example how to make a data entry form */\n\n.rx-form {\n  display: table;\n  table-layout: fixed;\n  border: 1px solid #0000FF;\n  width: 100%;\n  border-collapse: separate;\n  border-spacing: 5px;\n}\n\n.rx-caption {\n  display: table-caption;\n  border: 1px solid #000000;\n  text-align: center;\n  padding: 10px;\n  margin: 10px;\n  width: calc(100% - 40px);\n  font-size: 2.5em;\n}\n\n.rx-row {\n  display: table-row;\n  /* To make frame on rows. Rows have no border... ? */\n  box-shadow: 0px 0px 0px 1px rgb(0, 0, 0);\n}\n\n.rx-cell {\n  display: table-cell;\n  margin: 0px;\n  padding: 4px;\n  border: 1px solid #FF0000;\n}\n\n.rx-cell label {\n  float: left;\n  border: 1px solid #00FF00;\n  width: 110px;\n  padding: 4px;\n  font-size: 1em;\n  text-align: right;\n  font-family: Arial, Helvetica, sans-serif;\n  overflow: hidden;\n  white-space: nowrap;\n  overflow: hidden;\n  text-overflow: ellipsis;\n}\n\n.rx-cell label:after {\n  content: " :";\n}\n\n.rx-cell input[type=\'text\'] {\n  float: right;\n  border: 1px solid #FF00FF;\n  padding: 4px;\n  background-color: #eee;\n  border-radius: 0px;\n  font-family: Arial, Helvetica, sans-serif;\n  font-size: 1em;\n  /* Fill the cell - but subtract the label width - and litte more... */\n  width: calc(100% - 130px);\n  overflow: hidden;\n  white-space: nowrap;\n  overflow: hidden;\n  text-overflow: ellipsis;\n}\n\ninput[type=\'submit\'] {\n  font-size: 1.3em;\n}</code></pre>\r\n<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;html&gt;\n&lt;meta charset="UTF-8"&gt;\n\n&lt;body&gt;\n\n  &lt;!--                                                                                                                                                                                                                                                                             \n        G Hasse, gorhas at raditex dot nu                                                                                                                                                                                                                                            \n        This example have a lot of frames so we                                                                                                                                                                                                                                      \n        can experiment with padding and margins.                                                                                                                                                                                                                                     \n    --&gt;\n\n  &lt;form&gt;\n\n    &lt;div class=\'rx-form\'&gt;\n\n\n      &lt;div class=\'rx-caption\'&gt;\n        Caption\n      &lt;/div&gt;\n\n\n      &lt;!-- First row of entry --&gt;\n\n      &lt;div class=\'rx-row\'&gt;\n\n        &lt;div class=\'rx-cell\'&gt;\n          &lt;label for="input11"&gt;Label 1-1&lt;/label&gt;\n          &lt;input type="text" name="input11" id="input11" value="Some latin text here. And if it is very long it will get ellipsis" /&gt;\n        &lt;/div&gt;\n\n        &lt;div class=\'rx-cell\'&gt;\n          &lt;label for="input12"&gt;Label 1-2&lt;/label&gt;\n          &lt;input type="text" name="input12" id="input12" value="The content of input 2" /&gt;\n        &lt;/div&gt;\n\n        &lt;div class=\'rx-cell\'&gt;\n          &lt;label for="input13"&gt;Label 1-3&lt;/label&gt;\n          &lt;input type="text" name="input13" id="input13" value="Content 3" /&gt;\n        &lt;/div&gt;\n\n        &lt;div class=\'rx-cell\'&gt;\n          &lt;label for="input14"&gt;Label 1-4&lt;/label&gt;\n          &lt;input type="text" name="input14" id="input14" value="Content 4" /&gt;\n        &lt;/div&gt;\n\n      &lt;/div&gt;\n\n      &lt;!-- Next row of entry --&gt;\n\n      &lt;div class=\'rx-row\'&gt;\n\n        &lt;div class=\'rx-cell\'&gt;\n          &lt;label for="input21"&gt;Label 2-1&lt;/label&gt;\n          &lt;input type="text" name="input21" id="input21" value="Content 2-1"&gt;\n        &lt;/div&gt;\n\n        &lt;div class=\'rx-cell\'&gt;\n          &lt;label for="input22"&gt;Label 2-2&lt;/label&gt;\n          &lt;input type="text" name="input22" id="input22" value="Content 2-2"&gt;\n        &lt;/div&gt;\n\n        &lt;div class=\'rx-cell\'&gt;\n          &lt;label for="input23"&gt;Label 2-3&lt;/label&gt;\n          &lt;input type="text" name="input23" id="input23" value="Content 2-3"&gt;\n        &lt;/div&gt;\n\n      &lt;/div&gt;\n\n\n      &lt;!-- Next row of entry --&gt;\n\n      &lt;div class=\'rx-row\'&gt;\n\n        &lt;div class=\'rx-cell\'&gt;\n          &lt;label for="input21"&gt;Label 2-1&lt;/label&gt;\n          &lt;input type="text" name="input21" id="input21" value="Content 2-1"&gt;\n        &lt;/div&gt;\n\n        &lt;div class=\'rx-cell\'&gt;\n          &lt;label for="input22"&gt;Label 2-2&lt;/label&gt;\n          &lt;input type="text" name="input22" id="input22" value="Content 2-2"&gt;\n        &lt;/div&gt;\n\n        &lt;div class=\'rx-cell\'&gt;\n          &lt;label for="input23"&gt;Label 2-3&lt;/label&gt;\n          &lt;input type="text" name="input23" id="input23" value="Content 2-3"&gt;\n        &lt;/div&gt;\n\n      &lt;/div&gt;\n\n      &lt;!-- And some text in cells --&gt;\n\n      &lt;div class=\'rx-row\'&gt;\n\n        &lt;div class=\'rx-cell\'&gt;\n          &lt;div&gt;Cell content&lt;/div&gt;\n        &lt;/div&gt;\n\n        &lt;div class=\'rx-cell\'&gt;\n          &lt;span&gt;Cell content&lt;/span&gt;\n        &lt;/div&gt;\n\n      &lt;/div&gt;\n\n\n\n      &lt;!-- And we place the submit buttons in a cell --&gt;\n\n      &lt;div class=\'rx-row\'&gt;\n\n        &lt;div class=\'rx-cell\'&gt;\n          &lt;input type="submit" name="submit1" value="submit1" /&gt;\n          &lt;input type="submit" name="submit2" value="submit2" /&gt;\n        &lt;/div&gt;\n\n      &lt;/div&gt;\n\n      &lt;!-- End of form --&gt;\n    &lt;/div&gt;\n  &lt;/form&gt;\n\n&lt;/body&gt;\n\n&lt;/html&gt;</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}]
['https://Stackoverflow.com/questions/52563', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1968/']