CSS中zoom:1的作用

zoom属性用来设定对象的缩放功能,并且还有浏览器兼容性问题,Firefox和Opera浏览器不支持此属性。但是这些都不是本章节的介绍的内容,本章节主要介绍此属性的一些特殊用法,先看一段代码实例:

复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>CSS学习-蚂蚁部落</title> 
<style type="text/css"> 
.parent{ 
  border:2px solid red; 
} 
.children{ 
  width:150px; 
  height:150px; 
  background-color:green; 
  float:left; 
} 
</style> 
</head> 
<body> 
  <div class="parent"> 
    <div class="children"></div> 
  </div> 
</body> 
</html>
复制代码

以上代码由于子元素使用浮动,导致父元素塌陷,也就是没有被子元素撑开,当然可以使用清除浮动来解决此问题,代码修改如下:

复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>CSS学习-蚂蚁部落</title> 
<style type="text/css"> 
.parent{ 
  border:2px solid red; 
  overflow:hidden; 
} 
.children{ 
  width:150px; 
  height:150px; 
  background-color:green; 
  float:left; 
} 
</style> 
</head> 
<body> 
  <div class="parent"> 
    <div class="children"></div> 
  </div> 
</body> 
</html>
复制代码

以上代码在父元素中添加overflow:hidden即可清除浮动,父元素被撑开了,但是IE6并不支持使用overflow清除浮动,这时使用zoom:1即可解决此问题,代码修改如下:

复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>CSS学习-蚂蚁部落</title> 
<style type="text/css"> 
.parent{ 
  border:2px solid red; 
  overflow:hidden; 
  *zoom:1; 
} 
.children{ 
  width:150px; 
  height:150px; 
  background-color:green; 
  float:left; 
} 
</style> 
</head> 
<body> 
  <div class="parent"> 
    <div class="children"></div> 
  </div> 
</body> 
</html>
复制代码

以上代码实现了对IE6浏览器的兼容,原理是在IE6下为元素应用zoom:1的时候可以激活haslayout属性,也就是将此属性值设置为true,这个时候此元素就具有了布局,这样就可以闭合浮动,这样父元素就不会出现没有被撑开的情况。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值