“Layout”是一个 IE/Win 的私有概念,它决定了一个元素如何显示以及约束其包含的内容、如何与其他元素交互和建立联系、如何响应和传递应用程序事件/用户事件等。
这种渲染特性可以通过某些 CSS 属性被不可逆转地触发。而有些 HTML 元素则默认就具有“layout”。
微软的开发者们认为元素都应该可以拥有一个“属性(property)”(这是面向对象编程中的一个概念),于是他们便使用了 hasLayout,这种渲染特性生效时也就是将 hasLayout 设成了 true 之时。
术语
当我们说一个元素“得到 layout”,或者说一个元素“拥有 layout” 的时候,我们的意思是指它的微软专有属性 hasLayout http://msdn.microsoft.com/worksh ... rties/haslayout.asp 为此被设为了 true 。一个“layout元素”可以是一个默认就拥有 layout 的元素或者是一个通过设置某些 CSS 属性得到 layout 的元素。
而“无layout元素”,是指 hasLayout 未被触发的元素,比如一个未设定宽高尺寸的干净 div 元素就可以做为一个“无layout祖先”。
给一个默认没有 layout 的元素赋予 layout 的方法包括设置可触发 hasLayout = true 的 CSS 属性。参考默认 layout 元素以及这些属性列表。没有办法设置 hasLayout = false , 除非把一开始那些触发 hasLayout = true 的 CSS 属性去除。
hasLayout 的问题不管新手还是老手,不管设计师或者程序员可能都遇到过。Layout 在显示盒子时有着不同寻常而且难以预料的效果,而且有时甚至会牵连到他们的孩子元素。
一个元素是否具有“layout”可能会引发如下的一些问题:
IE 很多常见的浮动 bug 。
元素本身对一些基本属性的异常处理问题。
容器和其子孙之间的边距重叠(margin collapsing)问题。
使用列表时遇到的诸多问题。
背景图像的定位偏差问题。
使用脚本时遇到的浏览器之间处理不一致的问题。
上面的列表只是列出一个大概,也不完善。下面的文章将尽可能详细彻底的描述有无“layout”所带来的各种问题。
不同于标准属性,也不像某些浏览器的私有 CSS 属性,layout 无法通过某一个 CSS 声明直接设定 。也就是说没有“layout属性”这么一个东西,元素要么本身自动拥有 layout,要么借助一些 CSS 声明悄悄地获得 layout。
默认layout元素
下列元素应该是默认具有 layout 的:
<html>, <body> <table>, <tr>, <th>, <td> <img> <hr> <input>, <select>, <textarea>, <button> <iframe>, <embed>, <object>, <applet> <marquee>
IE6绝对定位的bug及其解决办法。position:absolute定位在IE6下存在left和bottom的定位错误问题:
<div style=”position:relative;border:1px solid orange;text-align:center;”>
<a href=”http://www.52css.com”>http://www.52css.com/</a>
<div style=”position:absolute;top:0;left:0;background:#CCC;”>52CSS</div>
</div>
<!–IE6下的left定位错误–>
<hr />
<div style=”position:relative;border:1px solid orange;text-align:right;”>
<a href=”http://www.52css.com”>http://www.52css.com/</a>
<div style=”position:absolute;top:0;left:0;background:#CCC;”>52CSS</div>
</div>
上面这段代码在IE6中定位错误。
解决办法有两种:
1、给父层设置zoom:1触发layout。
2、给父层设置宽度(width)。
<hr />
<div style=”position:relative;border:1px solid orange;zoom:1;text-align:center;”>
<a href=”http://www.52css.com”>http://www.52css.com/</a>
<div style=”position:absolute;top:0;left:0;background:#CCC;”>52CSS</div>
</div>
<!–解决方法2 设置width–>
<hr />
<div style=”position:relative;width:99%;border:1px solid orange;text-align:center;”>
<a href=”http://www.52css.com”>http://www.52css.com/</a>
<div style=”position:absolute;top:0;left:0;background:#CCC;”>52CSS</div>
</div>
下面的这段代码在IE6下,bottom定位错误:
<hr />
<div style=”position:relative;border:1px solid orange;text-align:center;”>
<a href=”http://www.52css.com”>http://www.52css.com/</a>
<div style=”position:absolute;bottom:0;left:0;background:#CCC;”>52CSS</div>
</div>
解决办法和left定位类似:
方法1是给父层设置zoom触发layout。
方法2是给父层设置高度(height)。
<hr />
<div style=”position:relative;border:1px solid orange;zoom:1;text-align:center;”>
<a href=”http://www.52css.com”>http://www.52css.com/</a><br />
<a href=”http://www.52css.com”>http://www.52css.com/</a>
<div style=”position:absolute;bottom:0;left:0;background:#CCC;”>52CSS</div>
</div>
<!–解决方法2 设置height–>
<hr />
<div style=”position:relative;height:60px;border:1px solid orange;text-align:center;”>
<a href=”http://www.52css.com”>http://www.52css.com/</a>
<div style=”position:absolute;bottom:0;left:0;background:#CCC;”>52CSS</div>
</div>
IE6中很多Bug都可以通过触发layout得到解决,以上的解决方法无论是设置zoom:1还是设置width和height其实都是为了触发layout。下列的CSS属性或取值会让一个元素获得layout:
position:absolute 绝对定位元素的包含区块(containing block)就会经常在这一方面出问题
float:left|right 由于layout元素的特性,浮动模型会有很多怪异的表现
display:inline-block 当一个内联级别的元素需要layout的时候就往往符用到它,这也可能也是这个CSS属性的唯一效果—-让某个元素有layout
width: 除auto外的任何值
height: 除auto外的任何值
zoom: 除auto外的任何值