DIV浮动后父DIV高度自动失效解决方案

今天在用dreamweaver写CSS代码的时候发现在居然给子DIV设置浮动后父DIV的高度自动居然失效,郁闷了半天,只好从google中去寻找答案  ,在google在钻了一下,最终得以解决。

具体效果图如下:

父DIV高度没有自动如下:

父DIV高度自动后效果如下:

 

html代码如下:

<!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" />
<link href="index.css" type="text/css" rel="stylesheet"/>
<title>无标题文档</title>
</head>

<body>
<div class="header"></div>
<div class="main">
<div class="main1"></div>
<div class="main2"></div>
</div>
<div class="buttom"></div>
</body>
</html>

 

css代码如下:

没有浮动之前的css代码:

.header{
height:100px;
width:960px;
margin:0px auto;
background:#339933;
}
.main{
min-height:50px;
height:auto;
width:960px;
background:#000000;
margin:0px auto;
}
.main1{
height:300px;
float:left;
width:200px;
background:#CC3366;

}
.main2{
height:300px;
float:left;
width:500px;
background:#CC3366;
margin-left:20px;

}

.buttom{
height:100px;
width:960px;
background:#333300;
margin:0px auto;
}

 

浮动后的完整代码如下:

.header{
height:100px;
width:960px;
margin:0px auto;
background:#339933;
}
.main{
min-height:50px;
height:auto;
width:960px;
background:#000000;
margin:0px auto;
overflow:auto;
}
.main1{
height:300px;
float:left;
width:200px;
background:#CC3366;

}
.main2{
height:300px;
float:left;
width:500px;
background:#CC3366;
margin-left:20px;

}

.buttom{
height:100px;
width:960px;
background:#333300;
margin:0px auto;
}

 

总结:其实很简单 只需要在父DIV中加入overflow:auto或overflow: hidden这个属性就搞定。哈哈 是不是很简单呀。

 

 


版权声明:本文为xmq_666原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。