开发者

What's the danger in not specifying widths for a floated element?

开发者 https://www.devze.com 2022-12-22 18:47 出处:网络
My un开发者_StackOverflowderstanding is floated elements must have their widths specified, or else unpredictable rendering may ensure.

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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号