Why is the child input field larger than the parent div
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 that the width of the text input and textarea should be exactly equal to the parent div. but this is not so. can anyone explain why?
+4
4 answers
The total width is calculated as the sum of padding + width + borderWidth . This is the default behavior for the box model. You can change it using the box-sizing property. In your case:
.field { ... box-sizing: border-box; } Further reading: http://css-tricks.com/box-sizing/
+13