css中inline-block代替浮動布局float:lef

來源:互聯網
上載者:User

基於各位前輩的辛勤勞動,下面得出使用inline-block替換float:left;的最佳方案。

html代碼

 代碼如下 複製代碼
<div class="list">
    <ul>
        <li><img src="images/pic02.jpg" alt=""></li>
        <li><img src="images/pic02.jpg" alt=""></li>
        <li><img src="images/pic02.jpg" alt=""></li>
        <li><img src="images/pic02.jpg" alt=""></li>
        <li><img src="images/pic02.jpg" alt=""></li>
        <li><img src="images/pic02.jpg" alt=""></li>
    </ul>
</div>

css代碼(來自taobaoued)

 代碼如下 複製代碼
.list {
font-size:0;/* 所有瀏覽器 */
*word-spacing:-1px;/* IE6、7 */
}
.list ul li{
font-size: 12px;
letter-spacing: normal;
word-spacing: normal;
vertical-align:top;
display: inline-block;
*display:inline;
*zoom:1;
}
@media screen and (-webkit-min-device-pixel-ratio:0){
/* firefox 中 letter-spacing 會導致脫離普通流的元素水平位移 */
.list{
letter-spacing:-5px;/* Safari 等不支援字型大小為 0 的瀏覽器, N 根據父級字型調節*/
}
}

最終效果圖完美相容

 代碼如下 複製代碼

<!doctype html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <!--[if lt ie 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <style type="text/css">
li{list-style: none;}
body{padding-bottom:44px;}
.fl{float:left;}
.fr{float:right;}
.header,.footer{background: #EDEDED;}
.header{width:100%; height:32px; line-height:32px; margin-bottom:12px;}
.footer{position:fixed; bottom:0; left:0; width:100%; height:32px; line-height:32px; _position:static; background:#000; opacity:.6; filter:alpha(opacity=60);}
.footer a{color:#fff;}
.footer a:hover{color:#FF6633;}
.header nav,.footer nav{width:980px; margin:0 auto;}
.footer .hd{background:#0099CC; color:#fff; height:32px; padding:0 10px; margin:0 10px; font-weight:bold;}
.footer span,.footer a{float:left;}
/* css start */
.list {
font-size:0;/* 所有瀏覽器 */
*word-spacing:-1px;/* IE6、7 */
max-width:820px;
margin:0 auto;
}
.list ul{_width:840px; margin-left:-20px;}
.list ul li{
max-width:400px;
height:279px; margin-left:20px;
margin-bottom:20px;
font-size: 12px;
letter-spacing: normal;
word-spacing: normal;
vertical-align:top;
display: inline-block;
*display:inline;
*zoom:1;
}
@media screen and (-webkit-min-device-pixel-ratio:0){
/* firefox 中 letter-spacing 會導致脫離普通流的元素水平位移 */
.list{
letter-spacing:-5px;/* Safari 等不支援字型大小為 0 的瀏覽器, N 根據父級字型調節*/
}
}
/*css end */
</style>
</head>
<body>
  <header class="header">
    <nav>
    
    </nav>
  </header>
  <!-- html start -->
<div class="list">
    <ul>
        <li><img src="../images/pic01.jpg" alt=""></li>
        <li><img src="../images/pic01.jpg" alt=""></li>
        <li><img src="../images/pic01.jpg" alt=""></li>
        <li><img src="../images/pic01.jpg" alt=""></li>
        <li><img src="../images/pic01.jpg" alt=""></li>
        <li><img src="../images/pic01.jpg" alt=""></li>
    </ul>
</div>
 
 
</body>
</html>

使用inline-block替換float:left的列表法不是一個hack,而是一個進步。

使用inline-block的時候,要注意在IE7和IE6下用背景替換文字的時候,會出現一個bug,解決方案是用font-size:0; line-height:0;替換text-indent:-9999px;,bug

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.