“clear” is pretty simple and it just means that an element will “clear” the floated content above it in the html. This means that it must start under the bottom margin of the floated element and not above it. (Clear:left applies to floated elements to the left of the cleared element and float:right refers to floated elements to the right of the cleared element). Without “clear” applied an element will always try and rise up alongside a float defined earlier in the html assuming that there is room for the content to fit.
In your example there is actually no room for content in the right column to slide up because the elements are too wide but some older browsers would occasionally try and squeeze them on the end. Therefore if you want to make sure that something that follows a float clears the float then add clear:both to it.
Don’t confuse “clearing floats” with “containing floats” as they are different things. Containing floats is a mechanism to ensure that floated content is contained within a parent div. Floats are removed from the flow so a parent div that holds only floated children will in fact hold nothing and have zero height. Therefore any background-colour on the parent will not show. In order for the background of a parent to stretch around a float you need to contain the float which can be done by creating a block formatting context which is commonly done by applying overflow:hidden (and zoom:1.0 for ie7 and under). Or alternatively by using the clearfix method already mentioned.
The CSS FAQ (see my sig) had a section on floats worth reading.
The problem with your original method is explained in this old demo.
As mentioned earlier your code is a nightmare to work with and your html could be reduced by 95% approx.
e.g. Just like this:
<div class="sidebar"><img style="margin:0px;" src="http://www.lincolnsrealdeals.com/lincoln-nebraska-street.JPG" alt="some_text"/></div>
<div class="wrap">
<div class="box exla"></div>
<div class="boxr exr"></div>
<div class="box exl"></div>
<div class="boxr exr2"></div>
<div class="box exl2"></div>
<div class="boxr exr3"></div>
<div class="box exl3"></div>
<div class="boxr exr4"></div>
<div class="box exl3"></div>
<div class="boxr exr5"></div>
</div>
All the inline styles should be removed to the css as follows:
Full code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="hello" />
<title>here</title>
<style type="text/css">
body { font-family:Arial, Helvetica, sans-serif }
.wrap {
width:741px;
margin:0;
background:white;
overflow:hidden;
}
.sidebar {
float:left;
width:290px;
background:pink;
overflow:hidden;
}
.box, .boxr {
width:570px;
height:160px;
clear:both;
margin:0 0 6px;
float:left;
border:3px solid gray;
}
.boxr { float:right }
.exla { margin-top:160px }
.exla {
background-color:LightCyan;
background: -moz-linear-gradient(top, LightCyan, Cyan);/* for firefox 3.6+ */
background: -webkit-gradient(linear, left top, left bottom, from(LightCyan ), to(Cyan)); /* for webkit browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr= LightCyan, endColorstr= Cyan); /* for IE */
}
.exr {
background-color:LightCoral;
background: -moz-linear-gradient(top, LightCoral, OrangeRed );/* for firefox 3.6+ */
background: -webkit-gradient(linear, left top, left bottom, from(LightCoral ), to(OrangeRed )); /* for webkit browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr= LightCoral, endColorstr= OrangeRed ); /* for IE */
}
.exl {
background-color:PaleVioletRed;
background: -moz-linear-gradient(top, PaleVioletRed, Red );/* for firefox 3.6+ */
background: -webkit-gradient(linear, left top, left bottom, from(PaleVioletRed ), to(Red )); /* for webkit browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr= PaleVioletRed, endColorstr= Red ); /* for IE */
}
.exr2 {
background-color:Cyan;
background: -moz-linear-gradient(top, Cyan, CornflowerBlue);/* for firefox 3.6+ */
background: -webkit-gradient(linear, left top, left bottom, from(Cyan ), to(CornflowerBlue)); /* for webkit browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr= Cyan, endColorstr= CornflowerBlue); /* for IE */
}
.exl2 {
background-color:LightBlue;
background: -moz-linear-gradient(top, LightBlue, Blue );/* for firefox 3.6+ */
background: -webkit-gradient(linear, left top, left bottom, from(LightBlue ), to(Blue )); /* for webkit browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr= LightBlue, endColorstr= Blue ); /* for IE */
}
.exr3 {
background-color:LightGreen;
background: -moz-linear-gradient(top, LightGreen, LawnGreen );/* for firefox 3.6+ */
background: -webkit-gradient(linear, left top, left bottom, from(LightGreen ), to(LawnGreen )); /* for webkit browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr= LightGreen, endColorstr= LawnGreen ); /* for IE */
}
.exl3 {
background-color:YellowGreen;
background: -moz-linear-gradient(top, YellowGreen, Yellow);/* for firefox 3.6+ */
background: -webkit-gradient(linear, left top, left bottom, from(YellowGreen ), to(Yellow)); /* for webkit browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr= YellowGreen, endColorstr= Yellow); /* for IE */;
}
.exr4 {
background-color:LightSteelBlue;
background: -moz-linear-gradient(top, LightSteelBlue, Blue);/* for firefox 3.6+ */
background: -webkit-gradient(linear, left top, left bottom, from(LightSteelBlue ), to(Blue)); /* for webkit browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr= LightSteelBlue, endColorstr= Blue); /* for IE */
}
.exl3 {
background-color:LightSalmon;
background: -moz-linear-gradient(top, LightSalmon, LightCoral );/* for firefox 3.6+ */
background: -webkit-gradient(linear, left top, left bottom, from(LightSalmon ), to(LightCoral )); /* for webkit browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr= LightSalmon, endColorstr= LightCoral ); /* for IE */
}
.exr5 {
background-color:LawnGreen;
background: -moz-linear-gradient(top, LawnGreen, Green);/* for firefox 3.6+ */
background: -webkit-gradient(linear, left top, left bottom, from(LawnGreen ), to(Green)); /* for webkit browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr= LawnGreen, endColorstr= Green); /* for IE */
}
</style>
</head>
<body>
<div class="sidebar"><img style="margin:0px;" src="http://www.lincolnsrealdeals.com/lincoln-nebraska-street.JPG" alt="some_text"/></div>
<div class="wrap">
<div class="box exla"></div>
<div class="boxr exr"></div>
<div class="box exl"></div>
<div class="boxr exr2"></div>
<div class="box exl2"></div>
<div class="boxr exr3"></div>
<div class="box exl3"></div>
<div class="boxr exr4"></div>
<div class="box exl3"></div>
<div class="boxr exr5"></div>
</div>
</body>
</html>