一、实例
XML/HTML Code复制内容到剪贴板
<ulclass="list"><li><ahref="#"title="">新闻一Benjamin</a><span>2014-05-06</span></li><li><ahref="#"title="">新闻一Benjamin</a><span>2014-05-06</span></li><li><ahref="#"title="">新闻一Benjamin</a><span>2014-05-06</span></li><li><ahref="#"title="">新闻一Benjamin</a><span>2014-05-06</span></li></ul>CSS Code复制内容到剪贴板
<styletype="text/css">body,ul{padding:0;margin:0;}ul{list-style-type:none;border:1pxsolid#ccc;}li{padding-left:0;}.list{width:300px;margin:100pxauto;}.listli{height:24px;line-height:24px;border-bottom:1pxsolid#ccc;margin-bottom:-1px;padding-left:10px;}.listlispan{float:rightright;margin-right:10px;}.listli.float_a{float:left;}.fix{*zoom:1;}.fix:after{display:block;content:".";visibility:hidden;clear:both;height:0;overflow:hidden;}</style>二、各浏览器效果图
三、原因分析
1.标准参考:
W3C CSS2.1规范文档里对于浮动元素与非浮动行内元素相邻时的情况有如下解释。以下是关键段落:
A floated box is shifted to the left or right until its outer edge touches the containing block edge or the outer edge of another float. If there’s a line box, the top of the floated box is aligned with the top of the current line box.
由上面的描述可以得到以下结论:如果一个元素浮动前是一个行内元素,则该元素浮动后,顶部应与其之前所在的行框顶部对齐。
2.问题描述:
IE6 IE7 IE8(Quick)下,若浮动元素之前存在兄弟行内非浮动元素,IE 会将浮动元素所在的“当前行”认为是其前边的兄弟行内元素所产生的匿名框的底边,导致该浮动元素折行。
四、解决方案
1.全浮动
CSS Code复制内容到剪贴板
<ulclass="list"><liclass="fix"><aclass="float_a"href="#"title="">新闻一Benjamin</a><span>2014-05-06</span></li><liclass="fix"><aclass="float_a"href="#"title="">新闻一Benjamin</a><span>2014-05-06</span></li><liclass="fix"><aclass="float_a"href="#"title="">新闻一Benjamin</a><span>2014-05-06</span></li><liclass="fix"><aclass="float_a"href="#"title="">新闻一Benjamin</a><span>2014-05-06</span></li></ul>2.放置浮动元素在前
CSS Code复制内容到剪贴板
<ulclass="list"><li><span>2014-05-06</span><ahref="#"title="">新闻一Benjamin</a></li><li><span>2014-05-06</span><ahref="#"title="">新闻一Benjamin</a></li><li><span>2014-05-06</span><ahref="#"title="">新闻一Benjamin</a></li><li><span>2014-05-06</span><ahref="#"title="">新闻一Benjamin</a></li></ul>3.定位position
4.IE Hack+margin负值