Skip to content

CSS 排版技术探讨

继续玩CSS…
同样先建立XHTML,四个层嵌入一个层中…

HTML:
  1.  <div id="content">
  2.    <div id="one">one</div>
  3.    <div id="two">two</div>
  4.    <div id="three">three</div>
  5.    <div id="four">four</div>
  6.  </div>
  7.  </body>

1. 有点特殊的margin

开始...

CSS:
  1. #content {
  2.     float: left;
  3.     background-color: #F00
  4. }
  5.  #one {
  6.     padding: 10px;
  7.     height: 80px;
  8.     background-color: #060;
  9.     width: 80px
  10. }
  11.  #two {
  12.     margin: 20px;
  13.     height: 60px;
  14.     background-color: #060;
  15.     width: 60px
  16. }
  17.  #three {
  18.     padding: 10px;
  19.     margin: 10px;
  20.     height: 60px;
  21.     background-color: #060;
  22.     width: 60px
  23. }
  24.  #four {
  25.     height: 100px;
  26.     background-color: #060;
  27.     width: 100px
  28. }

IE6,firefox1.5,opera8.5表现一样,有点特殊的是这样上下坚着排列的两个层距离会取margin并集...
2-1.gif

two和three之间的距离是20px,而不是我预想中的20+10=30px...

2.一切从float开始

下面唱主角的是float,似乎各浏览器比我还不懂它究竟是啥意思…
先给#one加一个float: left; 然后…
ff和opera浮动层one挡住了因one浮动而位置上移的two..并把two层中的文字往下挤。
ie6中的one把two向右挤,但又没有挤出一个one的100px宽度来..测了下好象是83px..搞不懂..

2-2.gif
2-3.gif

接着..给#two加一个 clear:both…ie和opera正常,轮到ff发傻了...

2-4.gif

似乎很好的贯彻了margin不影响当前行的高度..
所以当你希望所有的层竖着排列时,尽量别有float这个东东出现。

3. IE双倍浮动边距错误

这个错误是我在发现后google出解决方案来的...本来网上有更好的说明,但舍不得我下面的尝试,还是写出来吧...

CSS:
  1. #one {
  2.     padding: 10px;
  3.     height: 80px;
  4.     background-color: #060;
  5.     width: 80px
  6. }
  7.  #two {
  8.     float: left;
  9.     margin: 20px;
  10.     height: 60px;
  11.     background-color: #060;
  12.     width: 60px
  13. }
  14.  #three {
  15.     float: left;
  16.     padding: 10px;
  17.     margin: 10px;
  18.     height: 60px;
  19.     background-color: #060;
  20.     width: 60px
  21. }
  22.  #four {
  23.     clear: both;
  24.     height: 100px;
  25.     background-color: #060;
  26.     width: 100px
  27. }

然后...

2-5.gif

IE中two的左边和three的右边都是双倍的margin..解决方法是在#two和#three中都加上 display: inline;

顺便说一下,横着排列的两个层距离是margin之和,与竖着排列是有区别的。

4.关于IE计算2个div之间的距离时候,会把1px的border计算在内,而mozilla没有;

这是网上很多人认同的说法,我实验了下...

CSS:
  1. #one {
  2.     padding: 10px;
  3.     height: 80px;
  4.     background-color: #060;
  5.     width: 80px
  6. }
  7.  #two {
  8.     float: left;
  9.     margin: 10px;
  10.     padding: 10px;
  11.     height: 60px;
  12.     background-color: #060;
  13.     width: 60px;
  14.     display: inline
  15. }
  16.  #three {
  17.     float: left;
  18.     height: 100px;
  19.     background-color: #060;
  20.     width: 100px
  21. }
  22.  #four {
  23.     clear: both;
  24.     height: 100px;
  25.     background-color: #060;
  26.     width: 100px
  27. }

结果...
2-6.gif

看来这个说法是错的,ie6盒模型解析的是正确的。

补充..MS对于IE6对CSS的支持有文章

http://www.microsoft.com/china/MSDN/library/NetComm/webteam08062001.mspx?mfr=true

Related Posts

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*