DIV+CSS关于浮动跑到上面的原因

今天,遇到了一个问题就是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>

这样就可了


问题搞定!

很多时候都是细节问题。希望这个可以帮到和一样遇到过这种问题的朋友。


  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值