Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why child input field is wider than parent div [duplicate]

Tags:

css

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

like image 599
Arif Avatar asked Mar 23 '13 05:03

Arif


People also ask

Why is child div larger than parent div?

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.

Why is input bigger than div?

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% .

How do you make a div not bigger than a parent?

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).

How do you make a div full width of a parent?

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%.


2 Answers

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/

like image 57
dfsq Avatar answered Nov 15 '22 07:11

dfsq


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;
}
like image 31
Dipesh Parmar Avatar answered Nov 15 '22 05:11

Dipesh Parmar