Stablize floating containers with overflow: hidden
A large majority of web designers still uses the CSS property clear: both to stabilize floating boxes in CSS layouts:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<style type="text/css">
div { border: 1px solid #444; padding: 15px; margin: 5px; }
.div1, .div2 { height: 100px; width: 100px; float: left}
.clear { clear: both }
</style>
<title>Demo</title>
</head>
<body>
<div class='container'>
<div class='div1'>div1</div>
<div class='div2'>div2</div>
</div>
<div class='clear'></div>
</body>
</html>However, there is a much more elegant way to solve the problem – with overflow: hidden. This solution is known since many years, but nobody ever uses it. Instead, everybody is moaning about clear: both.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<style type="text/css">
div { border: 1px solid #444; padding: 15px; margin: 5px; }
.container { overflow: hidden; }
.div1, .div2 { height: 100px; width: 100px; float: left}
</style>
<title>Demo</title>
</head>
<body>
<div class='container'>
<div class='div1'>div1</div>
<div class='div2'>div2</div>
</div>
</body>
</html>Why is it more elegant? Because it doesn’t create an extra element, and all CSS properties only influence the behaviour of the elements they’re applied to.
Where’s the catch? There’s none. This works in all major browsers, including IE 6. You can float left or right. You can give inner and outer elements margin, padding, height, width. You can nest floating elements. So why is everybody using clear:both? I don’t know.
My guess is that it is evolutionary. Somebody influential started with bad code, and everybody else just copied it.

