8

As you can see from this example, the input seems to "overflow" its parent div. I'd like to add padding to the input without making it overflow its parent.

I would like to know the best solution/workaround for every browser (including IE, Firefox, Chrome, etc).

nbro
  • 15,395
  • 32
  • 113
  • 196
Ricardo Rodrigues
  • 2,633
  • 5
  • 29
  • 31
  • See the answer to this previous question: [width: 100%-padding?](http://stackoverflow.com/questions/5219175/width-100-padding) – John Lawrence Jun 09 '12 at 23:47

4 Answers4

26

You can see this answer, but if you don't like it, you can use box-sizing CSS3 property like this:

input {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

Live jsFiddle example

Community
  • 1
  • 1
Fong-Wan Chau
  • 2,259
  • 4
  • 26
  • 42
2

Padding adds to the width of your object. One option would be to remove the left/right padding from the input and just use text-indent, although this removes the right padding.

.inside{
    background: blue;
    border: none;
    padding-bottom: 10px;
    padding-top: 10px;
    text-indent: 10px;
    width: 100%;
}

Alternatively, instead of using hardcoded pixel-widths for your padding, you could use percentages, and subtract that value from the width:

.inside{
    padding: 3%;
    width: 94%;
}
jeffjenx
  • 17,041
  • 6
  • 57
  • 99
  • The problem not want the text on input near the border. But thanks for the tip – Ricardo Rodrigues Jun 09 '12 at 23:51
  • Well , that one of the solutions I thought. Will see other responses , because I'm looking the best way and crossbrowser solution. Other idea was using position: absolute; right:0; left:0 and define a width , then center using margin: auto; – Ricardo Rodrigues Jun 09 '12 at 23:56
0

Don't specify the width of the inside div as 100%. A div will automatically fit the width of its parent container. Demo

sachleen
  • 30,730
  • 8
  • 78
  • 73
0

Looks like the input is inside the div but is located in the top left corner. Because the input takes-up 100% of the div's width it obscures the red background of the div. The div is longer so it sticks out the bottom making it seem like the input is on-top. Do the following:

  • Apply the padding to the CSS of the outside div not the input box.
    You could apply a margin to the input if you want but I think padding the containing div is better.
    • Make the input box less wide than the div (<100%)
GrantVS
  • 2,073
  • 1
  • 24
  • 27