DIV+CSS 设计的页面,DIV高度自适应问题

解决方法: 方法1: 在内层元素的最后面加入一个元素,并且设置这个元素不浮动,强制使外层元素包裹内层元素如<div style="clear:both;float:none"></div> 方法2:设置外层元素css属性overfloat为hidden或者auto,zoom设置为1,设置zoom是为了兼容ie6

方法4:.clearFix:after

方法3:设置外层元素高度为一个固定值,这个一般不采取使用。

 
 
例子1:
<!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" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	<title></title>
	<style type="text/css">
		#main{background:#ccc;zoom:1;}/*专业对付IE6/7*/
		#left{width:100px;height:100px;background:red;float:left;}
		#right{width:200px;height:200px;background:green;float:right;}
		#footer{width:50px;height:50px;background:blue;clear:both;}
		.clear{clear:both;}/*给空标签,设置清除两者浮动,使父元素的高度自适应子元素高度*/
	</style>
</head>
<body>
	<div id="main">
		<div id="left">左浮动元素</div>

		<div id="right">右浮动元素</div>
		<div class="clear"></div>
	</div>
	<div id="footer">底部元素</div>
</body>
</html>
 
 
例子2:
<!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" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	<title></title>
	<style type="text/css">
		#main{background:#ccc;border:1px solid #000;overflow:hidden;zoom:1;}
		#left{width:100px;height:100px;background:red;float:left;}
		#right{width:200px;height:200px;background:green;float:right;}
		#footer{height:50px;background:blue;}
	</style>
</head>
<body>
	<div id="main">
		<div id="left">左浮动元素</div>

		<div id="right">右浮动元素</div>
	</div>
	<div id="footer">底部元素</div>
</body>
</html>

 

 

例子4:

<!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" xml:lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 <title></title>
 <style type="text/css">
  .clearFix:after{
   clear:both;
   display:block;
   visibility:hidden;
   height:0;
   line-height:0;
   content:"";
  }
  .clearFix{zoom:1;}/*针对IE6/7*/
  #main,#footer{width:800px;margin:0 auto;}
  #main{background:#ccc;border:1px solid #000;position:relative;}
  #left{width:100px;height:100px;background:red;position:absolute;left:-50px;top:50px;}
  #right{width:200px;height:200px;background:green;float:right;}
  #footer{height:50px;background:blue;}
 </style>
</head>
<body>
 <div id="main" class="clearFix">
  <div id="left">左浮动元素</div>
  <div id="right">右浮动元素</div>
 </div>
 <div id="footer">底部元素</div>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值