轉載網頁部落格:ie7bug:div容器下的img與div存在間隙

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   sp   div   on   

1、代碼及在瀏覽器上的顯示

ie7:

ie8+:

Firefox:

Chrome:

可以看出ie7上在div容器下添加img,div與img中有間隙,而在ie8+和其他瀏覽器上均顯示正常

網頁原始碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>    <head>        <title>ie7 bug: img and div</title>        <style type="text/css">            *{                margin:0;                padding:0;            }            body>div{                margin:50px auto;                width:800px;            }            div.div0{                float:left;                margin:10px 20px;                width:90px;                border:2px solid #ff0000;            }            div.div1{                float:left;                margin:10px 20px;                width:8px;                border:2px solid #ff0000;            }            div.div2{                float:left;                margin:10px 20px;                width:90px;                border:2px solid #ff0000;            }            div.div3{                float:left;                margin:10px 20px;                width:10px;                border:2px solid #ff0000;            }            div.div4{                float:left;                margin:10px 20px;                width:10px;                border:2px solid #ff0000;            }            img{                float:left;            }        </style>    </head>    <body>        <div>            <div class="div0">                <img src="login_on.gif">            </div>            <div class="div1">                <img src="led.png">            </div>            <div class="div2">                <img src="login_on.gif">                <img src="logout.png">            </div>            <div class="div3">                <img src="led.png">                <img src="reset.png">            </div>            <div class="div4">                <img src="actLink.png">                <img src="reset.png">            </div>        </div>    </body></html>
2、解決問題

解決這個bug有多種方法:

1) 在子標籤內加入css hack,*margin-bottom:-4px;

此方法不夠完美,只適合圖片的高度比較大的情況,可以看到中,利用此種方法只有第一個和第三個適合,其他的img與div的上部有間隙。

2) 給img加樣式display:block; 將圖片轉為區塊層級元素

如,img{display:block;}

3)設定圖片的垂直對齊

如,img{vertical-align:top;}

4) 設定父物件的字型大小為0px

如,body>div>div{font-size:0;}

5) 設定圖片的浮動屬性

如,img{float:left;} or img{float:right;}

轉載網頁部落格:ie7bug:div容器下的img與div存在間隙

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.