今天,遇到了一个问题就是div地底部莫名其妙的跑到上面去了。
<!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>无标题文档</title>
<style type="text/css">
body{ text-align:center;}
.top{ width:960px; height:100px; background-color:#003300; margin:0 auto;}
.main{ margin:0 auto; width:960px;}
.a{ float:left; margin-left:10px;}
#left{ height:100px;}
#center{ height:200px;}
#right{ height:50px;}
.bottom{ width:960px; height:140px; background-color:#003399; margin:0 auto; }
</style>
</head>
<body>
<div class="top"><h1 style="color:#CCCCCC;">TOP</h1></div>
<div class="main">
<div class="a" id="left">
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">LEFT</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">LEFT</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">LEFT</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">LEFT</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">LEFT</h1></div>
</div>
<div class="a" id="center">
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">CENTER</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">CENTER</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">CENTER</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">CENTER</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">CENTER</h1></div>
</div>
<div class="a" id="right">
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">RIGHT</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">RIGHT</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">RIGHT</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">RIGHT</h1></div>
</div>
<div style="clear:both;"><h1>Clear Float DIV</h1><br /></div>
</div>
<div class="bottom" style="background-color:#FF0000; text-align:center;"><h1 style="color:#FFFFFF">BOTTOM</h1></div>
</body>
</html>
上面的代码就是无论你怎么清除浮动他bottom都是会跑到center下面去
如图所示:
因为left、center、right这几个div现在都是浮动的状态,浮动表示你现在的中间这个大的div不占位置的所以,最底部的bottom就会往上挤压。
这时候,很多人会想到清除浮动的方法。
当然,这些都是对那些有经验的来说可能最开始就写的很仔细。
但是往往我们在编写代码的时候会时不时在left或者是center这几个div里面给他添加了高度。
这时候,我告诉你完了。
导致你无论怎么去清除浮动都是没用的。
问题原因:
关键:检查你浮动的那几个div是否你给添加了height这个属性高度。
只要修改下就OK了!
<!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>无标题文档</title>
<style type="text/css">
body{ text-align:center;}
.top{ width:960px; height:100px; background-color:#003300; margin:0 auto;}
.main{ margin:0 auto; width:960px;}
.a{ float:left; margin-left:10px;}
.bottom{ width:960px; height:140px; background-color:#003399; margin:0 auto; }
</style>
</head>
<body>
<div class="top"><h1 style="color:#CCCCCC;">TOP</h1></div>
<div class="main">
<div class="a" id="left">
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">LEFT</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">LEFT</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">LEFT</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">LEFT</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">LEFT</h1></div>
</div>
<div class="a" id="center">
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">CENTER</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">CENTER</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">CENTER</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">CENTER</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">CENTER</h1></div>
</div>
<div class="a" id="right">
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">RIGHT</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">RIGHT</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">RIGHT</h1></div>
<div style="width:100px; height:100px; background-color:#660000; margin-bottom:10px;"><h1 style="color:#FFFFFF">RIGHT</h1></div>
</div>
<div style="clear:both;"><h1>Clear Float DIV</h1><br /></div>
</div>
<div class="bottom" style="background-color:#FF0000; text-align:center;"><h1 style="color:#FFFFFF">BOTTOM</h1></div>
</body>
</html>
这样就可了
问题搞定!
很多时候都是细节问题。希望这个可以帮到和一样遇到过这种问题的朋友。