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
A child div can also be wider than its parent by utilizing different positioning such as absolute or fixed positioning. Different results can occur depending on the specified position of the parent div but as long as the element is either absolute/fixed or contains a specified width, it will grow outside the parent.
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. The points he makes about padding also apply for the border. As noted in other answers, it may need width: 100%; height: 100% .
You can simply use the CSS display property with the value inline-block to make a <div> not larger than its contents (i.e. only expand to as wide as its contents).
Method 2: We can make the display attribute of the child container to table-row and display attribute of parent container to table, that will take all the height available from the parent div element. To cover all the width, we can make the width of parent div to 100%.
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/
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With