Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross browser method to fit a child div to its parent's width

I'm looking for a solution to fit a child div into it's parent's width.

Most solutions I've seen here are not cross-browser compatible (eg. display: table-cell; isn't supported in IE <=8).

image showing intended result with a child div being the full size of it's parent, with inner padding

like image 772
mate64 Avatar asked Jul 06 '11 08:07

mate64


People also ask

How do you make a child 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%.

How do you make a div relative to a parent?

The one key thing to remember when trying to position a child div relative to it's parent is that the child should be given the CSS property position:absolute; and the parent set to either position:absolute; or position:relative;.

Why child div is bigger than parent?

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.


6 Answers

The solution is to simply not declare width: 100%.

The default is width: auto, which for block-level elements (such as div), will take the "full space" available anyway (different to how width: 100% does it).

See: http://jsfiddle.net/U7PhY/2/

Just in case it's not already clear from my answer: just don't set a width on the child div.

You might instead be interested in box-sizing: border-box.

like image 184
thirtydot Avatar answered Nov 19 '22 12:11

thirtydot


You can use box-sizing css property, it's crossbrowser(ie8+, and all real browsers) and pretty good solution for such cases:

#childDiv{
   box-sizing: border-box;
   width: 100%; //or any percentage width you want
   padding: 50px;
}

Fiddle

like image 30
Y.Puzyrenko Avatar answered Nov 19 '22 11:11

Y.Puzyrenko


If you put position:relative; on the outer element, the inner element will place itself according to this one. Then a width:auto; on the inner element will be the same as the width of the outer.

like image 38
Steeven Avatar answered Nov 19 '22 11:11

Steeven


In your image you've putting the padding outside the child. This is not the case. Padding adds to the width of an element, so if you add padding and give it a width of 100% it will have a width of 100% + padding. In order to what you are wanting you just need to either add padding to the parent div, or add a margin to the inner div. Because divs are block-level elements they will automatically expand to the width of their parent.

like image 27
Alex Avatar answered Nov 19 '22 12:11

Alex


In case you want to use that padding space... then here's something:

http://jsfiddle.net/qD4zd/

All the colors are background colors.

like image 40
Joonas Avatar answered Nov 19 '22 11:11

Joonas


You don't even need width: 100% in your child div:

http://jsfiddle.net/DanielDZC/w2mev/1/

like image 43
Daniel Gruszczyk Avatar answered Nov 19 '22 10:11

Daniel Gruszczyk