Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input type=text to fill parent container

Tags:

html

css

I'm trying to let an <input type="text"> (henceforth referred to as “textbox”) fill a parent container by settings its width to 100%. 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.

Here's a minimal code to reproduce the behaviour in all modern browsers.

#x {
  background: salmon;
  padding: 1em;
}

#y, input {
  background: red;
  padding: 0 20px;
  width: 100%;
}
<div id="x">
  <div id="y">x</div>
  <input type="text"/>
</div>

My question: How do I get the textbox to fit the container?

Notice: for the <div id="y">, this is straightforward: simply set width: auto. 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 display: block for the textbox).

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 divitis that I want to avoid at all cost. This can only be a last-resort hack.

like image 535
Konrad Rudolph Avatar asked Sep 09 '08 18:09

Konrad Rudolph


4 Answers

With CSS3 you can use the box-sizing property on your inputs to standardise their box models. Something like this would enable you to add padding and have 100% width:

input[type="text"] {
    -webkit-box-sizing: border-box; // Safari/Chrome, other WebKit
    -moz-box-sizing: border-box;    // Firefox, other Gecko
    box-sizing: border-box;         // Opera/IE 8+
}

Unfortunately this won't work for IE6/7 but the rest are fine (Compatibility List), so if you need to support these browsers your best bet would be Davids solution.

If you'd like to read more check out this brilliant article by Chris Coyier.

Hope this helps!

like image 72
studioromeo Avatar answered Oct 16 '22 18:10

studioromeo


You can surround the textbox with a <div> and give that <div> padding: 0 20px. 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.

like image 45
David Kolar Avatar answered Oct 16 '22 17:10

David Kolar


Because of the way the Box-Modell is defined and implemented I don't think there is a css-only solution to this problem. (Apart from what Matthew described: using percentage for the padding as well, e.g. width: 94%; padding: 0 3%;)

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.

Interesting by-product of some testing I've done: Firefox does set the width of an input field to 100% if additionally to width: 100%; you also set max-width to 100%. This doesn't work in Opera 9.5 or IE 7 though (haven't tested older versions).

like image 3
Ben Avatar answered Oct 16 '22 17:10

Ben


How do I get the textbox to fit the container in 2019?

Just use display: flex;

#x {
  background: salmon;
  padding: 1em;
  display: flex;
  flex-wrap: wrap;
}

#y, input {
  background: red;
  padding: 0 20px;
  width: 100%;
}
<div id="x">
  <div id="y">x</div>
  <input type="text"/>
</div>
like image 3
Andyba Avatar answered Oct 16 '22 18:10

Andyba