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

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值