What's the danger in not specifying widths for a floated element?
My un开发者_StackOverflowderstanding is floated elements must have their widths specified, or else unpredictable rendering may ensure.
Is this true/false?
Is it acceptable to float elements like DIVs and not specify widths?
Not including a width on a floated element is unpredictable because the browser is left to determine how wide your float is.
Theoretically, a floated element with an undefined width should shrink to the widest element within it. This could be a word, a sentence or even a single character - and results can vary from browser to browser. Source: http://css.maxdesign.com.au/floatutorial/introduction.htm
In general, you don't have to.
If you specify the width of a float element, its width will be as you specified.
If you don't - the width of the element will depend on its content. If there is no content - the width will be zero (unless you specified padding or border), if there is a long text in it - the element will probably expand up to the whole page (or the parent element). If, for example, you have an image in the float element, its width will be as of the image width and so on.
The only time I had a problem with floats with no widths was in IE6. I'd never tested the site on IE6 before and was showing it to a friend on their work computer which ran IE6. It completely trashed the page, and was kind of embarrassing.
When I got back to work I found and installed the wonderful IETester program, added widths on a couple elements, and now all works well. Get IETester here: http://www.my-debugbar.com/wiki/IETester/HomePage
Good luck, and remember: test on IE6 if you're relying on floats with no widths for important parts of your layout!
精彩评论