tags:

views:

52

answers:

3

If a div is 100% width, should I still put in width: 100%;? I look at a lot of code and it's always in there even though by default a div is 100%.

A: 

I'm not sure if the children elements will adapt themselfs with procentual values if their parent doesn't have a width attribute. Otherwise it's just semantic and good practice to put width: 100%; if the div is supposed to span 100% of it's parent container.

Stefan Konno
I thought it would be "semantic" to do it.
omnix
A: 

nope, pretty useless I think to give it a 100% width unless you have a background-color or image or something in this div.

Yeah_Nikolai
+4  A: 

No, doing so can actually cause problems. 100% is not the same as auto. width refers to the width of the content, excluding borders, padding and margins. auto automatically computes the width such that the total width of the div fits the parent, but setting 100% will force the content alone to 100%, meaning the padding etc. will stick out of the div, making it larger than the parent.

See this for an example

casablanca
so setting the 'width' to 'auto' would be better?
omnix
Yes, but that's the default anyway.
casablanca