Strange layout behaviour
I am a little bit confused.开发者_开发百科 Here is a small web page. There are two main div-s: top and mainBlock.
First contains an image. The problem is that firebug shows that div#top's height is equal to 0 and because of that the next div mainBlock moves up. If I would delete this piece of code:div#logo{
float: left;
}
everything will start working fine and div#mainBlock will be below the div#top. Could you, please, explain me how it works and how to avoid this in proper way?
Here is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Paralab - Website & interface design, web apps development, usability</title>
<style text="text/css">
html, body{
}
div#logo{
float: left;
}
#top{
width: 100%;
}
#mainBlock{
width:100%;
}
</style>
</head>
<body>
<div id="top">
<div id="logo">
<img alt="logo" src="img/logo.png" />
</div>
</div>
<div id="mainBlock">
Contact Us
</div>
</body>
</html>
You need to clear your float. There are a few ways to do this, I'd check out this article on QuirksMode about clearing floats for more info.
because it's floating - add clear:left;
to your mainBlock
class.
http://jsfiddle.net/XEaBT/
Floated images leave the flow of the document. Set #top{overflow:hidden;}
I would highly recommend that you use a strict doctype instead of transitional. You will get more consistent behavior across browsers.
精彩评论