Heim  >  Artikel  >  Web-Frontend  >  CSS中zoom:1的作用是什么_html/css_WEB-ITnose

CSS中zoom:1的作用是什么_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:32:211458Durchsuche

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,这个时候此元素就具有了布局,这样就可以闭合浮动,这样父元素就不会出现没有被撑开的情况。

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=3371

更多内容可以参阅:http://www.softwhy.com/shili/

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn