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

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

具体效果图如下:

父DIV高度没有自动如下:

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

 

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这个属性就搞定。哈哈 是不是很简单呀。

 

 

你可能感兴趣的:(CSS)