why the margin-top: 20px; in footer doesn’t work? what’s the reason may be cause to this?
#main {
margin: 0 auto;
width: 960px;
}
#left {
float: left;
border: 1px solid red;
width: 300px;
margin-right: 10px;
height: 500px;
}
#right {
float: right;
border: 1px solid green;
width: 500px;
height: 500px;
}
#footer {
clear: both;
margin-top: 20px;
border: 1px solid lime;
height: 200px;
}
<div id="main">
<div id="left"></div>
<div id="right"></div>
<div id="footer"></div>
</div>
Try to add some clearer:
When an element’s css
clearset toboth, it won’t let ANY FLOATING element to overlap in its border and text area, meaning margin can be overlapped by float elements. That is why you cannot see your footer’s margin. So we basically need an extra div, which is not floated, so the margin of your footer has something to push. Try my codes below (with BG and Borders), you’ll see what I’m saying.Without extra div:
With extra div:
Resource:
http://www.w3.org/TR/CSS2/visuren.html#flow-control