5

HTML

<div class="wrap">
    <input class="field" type="text">
    <textarea class="field" row="10"></textarea>
</div>

CSS

.wrap{width:300px;overflow:hidden;padding:10px;}
.field{display:block;width:100%;margin:10px 0;padding:10px;}

I expect width of text input and textarea should be exactly equal to parent div. but they are not . can anybody explain why?

Live code

Arif
  • 978
  • 12
  • 29

4 Answers4

17

The total width is calculated as a sum of padding + width + borderWidth. This is the default behaviour of the box model. You can change it by using box-sizing property. In your case:

.field {
    ...
    box-sizing: border-box;
}

http://jsfiddle.net/aLz6b/3/

Further reading: http://css-tricks.com/box-sizing/

dfsq
  • 191,768
  • 25
  • 236
  • 258
3

Its because you have padding applied to the .field remove padding and see it will work.

.field{display:block;width:100%;margin:10px 0;}

OR

you have 100% width set so just set width as below.

.field {
    display: block;
    margin: 10px 0;
    padding: 10px;
    width: 280px;
}
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
1

The width: 100% property applies to the content box, not the border box. If you are using CSS3 you can set box-sizing: border-box to obtain the expected size.

http://www.w3.org/TR/css3-ui/#box-sizing0

dsh
  • 12,037
  • 3
  • 33
  • 51
0

Try this:

.wrap{width:300px;overflow:hidden;padding:10px 0px;}
Jesse
  • 418
  • 5
  • 8