74

I have an html element that is contained within a div. Height are dictated by the outer div and the height and width of the input control are 100%. At the most basic level, I am having an issue where the textbox extends past the right of the containing div.

Basic example code:

<div style="height:25px; width: 150px;">
     <input type="text" style="height:100%; width:100%"  />
</div>

The rendering of this control is far more complex than this, but still when the control is stripped down to this level, I have an issue where the textbox sticks out past the containing div.

BrandonS
  • 2,513
  • 7
  • 28
  • 29
  • Ok, so the question WAS why is my textbox element extending out past it's parent div? I've found that it seems to be related to my DOCTYPE. We are required to use With the code from my post, if I plug that into an html page without giving it a DOCTYPE, it works fine. So in order reproduce my problem, you must use the strict 4.01 DOCTYPE and then inspect the size of the DIV and TextBox with the developer tool or set a border on the DIV. – BrandonS Oct 27 '09 at 21:22
  • @BrandonS would you mind marking my answer as correct? – heedfull Nov 25 '16 at 14:00
  • tried most of the answers bellow but they don't work – UMR Jun 13 '21 at 08:22

8 Answers8

151

You can use box-sizing:border-box to take care of this. Just put the following in your css file:

input{box-sizing:border-box} 

It means that border on the input box is actually inside the width of the input rather than being added onto the outside. This is what is making the input larger than the container.

Paul Irish has really good post explaining this technique http://paulirish.com/2012/box-sizing-border-box-ftw

The points he makes about padding also apply for the border.

There's even a compass mixin to make it easier to support older browsers. (http://compass-style.org/reference/compass/css3/box_sizing/)

Lal
  • 14,726
  • 4
  • 45
  • 70
heedfull
  • 3,071
  • 3
  • 17
  • 11
20

This did the job for me :

input {
  padding: 0.2em; 
  box-sizing: border-box;
  width: 100% 
} 
davidsbro
  • 2,761
  • 4
  • 23
  • 33
Raphayol
  • 1,266
  • 11
  • 14
11

Try to give input width : 100%; box-sizing: border-box;

Usman Iqbal
  • 2,379
  • 5
  • 26
  • 50
4

unfortunately this will depend on the browser you are working with but setting the width of the object (the textbox) does not take into account the width of the border on the object. most browsers only take into consideration any padding from the outer object and margins from the contained object but a few (i'm looking at you IE) do not add in the border when calculating percentages;

your best bet is to change the border on the textbox or to throw in another div between teh textbox and the container with a padding of say 2px with a margin-top: -2px and a margin-left:-2px (i'm guessing at the border width)

Mike Valstar
  • 3,499
  • 5
  • 24
  • 32
  • 3
    You can use box-sizing:border box to take care of this. It means that border on the input box is actually inside the width of the input rather than being added on the outside. Paul Irish does a better job of explaining it than me: http://paulirish.com/2012/box-sizing-border-box-ftw/ – heedfull Feb 05 '13 at 06:52
3

I'm assuming that you want the contained element (<input>) to be smaller than, or contained entirely within, the <div>?

You can either:

input {width: 50%; /* or whatever */ }

An html-element's width is calculated (I think) as the defined width + borders + margin + padding

If you've already defined the input as having 100% width of the parent, and then the other attributes are added it will definitely overflow the parent div.

You can set the margin/padding/borders to 0, but that would likely not look good. So it's easier, though not necessarily perfect, just to use a suitably-smaller width.

You could, of course, use

#parent_div {overflow: hidden; /* or 'auto' or whatever */}

to hide the portion of the input element that would normally overflow the container div.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • This is interesting. I will have to play around with this idea more. We create other web controls in this manner and I do not have this issue with the "select" element(maybe it doesn't have and built in padding or margin in addition to it's border so it's not as much of an issue. – BrandonS Oct 27 '09 at 21:41
3

Please apply the following css to your input elements.

{
    box-sizing: border-box;
    width: 100%;
}

if you use bootstrap or other css library, it will be not problem.

xorozo
  • 540
  • 4
  • 18
1

I know this post is fairly old, but it's a common problem and no one posted any good answers...

The following HTML code looks fine. But when I add the doctype, the problem appear

div.field_container
{
    height: 25px;
    width: 150px;
    border: 1px solid red;
}
div.field_container input
{
    height: 100%;
    width: 100%;
}
<div class="field_container">
    <input type="text" name="" value="" />
</div>

To fix the width / height problem, you can add padding to your field_container, but that will make the container bigger.

div.field_container
{
    height: 25px;
    width: 150px;
    padding-bottom: 6px;
    padding-right: 4px;
    border: 1px solid red;
}
div.field_container input
{
    height: 100%;
    width: 100%;
}
<div class="field_container">
    <input type="text" name="" value="" />
</div>

If you can't change the container width, you can also use the following trick, but that will still increase the height

div.field_container
{
    height: 25px;
    width: 150px;
    padding-bottom: 6px;
    border: 1px solid red;
}
div.field_container input
{
    height: 100%;
    width: 100%;
}
<div class="field_container">
    <div style="height: 100%; margin-right:4px"><input type="text" name="" value="" /></div>
</div>
Gael
  • 444
  • 3
  • 10
0

Instead of applying the style directly on the input element, maybe abstract the CSS into a file and use classes:

div.field_container
{
  height: 25px;
  width: 150px;
}

div.field_container input
{
  height: 100%;
  width: 100%;
} 

<div class="field_container">
  <input type="text" name="" value="" />
</div>

I am running out the door before I could test if this helps at all, but at the very least, you could play with max-height/width settings on the DIV css to make it not break if my solution doesn't work. And having the CSS abstracted like this makes problems easier to solve using a plugin like Firebug in Firefox.

Question though, is there a need to enclose the input tag in it's own DIV? I wouldn't be surprised if there is just a better layout you could build that would avoid the need to do this...

Mike Arsenault
  • 43
  • 1
  • 2
  • 7