標準模式中的 IE 6&7 width 100% bug

來源:互聯網
上載者:User

在 web app 項目中經常遇到這個 bug,國外稱之為100% ≠ 100% bug,又分為兩種:

  1. div 的寬度 100% ≠ 100% (IE 6&7)需求:

    1. 標準模式
    2. #container 局部滾動
    3. #asie 固定寬度
    4. #content 自適應寬度

    再複雜一點還會要求兩列等高,可參考 http://www.99css.com/?p=40

    HTML

    <div id="container">    <div id="wrapper">        <div id="content">            <h2>Content</h2>        </div>    </div>    <div id="aside">        <h2>Aside</h2>    </div></div>

    當然,別忘了 DTD 聲明,因為這個只存在於標準模式

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    用之前介紹的HTML5 寫法亦可:

    <!DOCTYPE html>

    CSS

    /*簡單重設*/body, div, h2{margin:0;padding:0;}/*設定顏色,方便區分*/#container{background:yellow;}#content{background:#FF8539;}#aside{background:#B9CAFF;}/*去除html預設捲軸*/html{overflow-y:hidden;}/*關鍵布局代碼*/#container{height:300px;overflow:auto;}#wrapper{float:left;width:100%;margin-left:-200px;}#content{margin-left:200px;}#aside{float:right;width:200px;}

    #content, #aside{height:200px;}時,即高度未超出#container時一切正常

    #content, #aside{height:400px;}時,出現縱向捲軸

    正常顯示效果如下:

    IE 6&7 中 bug 出現:

    原因:IE 6&7 捲軸的寬度未算在 100% 中,理想的狀況是:#container 的寬度(100%) + #container 捲軸的寬度
    = body 的 100%,W3C對此的定義:

    In the case of a scrollbar being placed on an edge of the element’s box,
    it should be inserted between the inner border edge and the outer padding
    edge. Any space taken up by the scrollbars should be taken out of (subtracted
    from the dimensions of) the containing block formed by the element with
    the scrollbars.

    《Internet Explorer 100% Width Bug》中給出了思路:

    element_selector {width: expression(this.parentNode.offsetHeight >this.parentNode.scrollHeight ? '100%' :parseInt(this.parentNode.offsetWidth - XX) + 'px');}

    其中 XX 是捲軸的寬度,在 Windows XP 主題下是 17px,Windows 經典主題下稍微小一點,在其他第三方系統主題下有可能是不確定寬度。

    根據,簡單改進一下即可

    解決方案

    #wrapper{#width:expression(this.parentNode.offsetHeight > this.parentNode.scrollHeight ? '100%' : parseInt(this.parentNode.clientWidth) + 'px');}

    當然,寫在 js 中亦可,不過要注意不要漏掉 window 的 riseze 事件,另外,window 的 resize 事件在 IE 中有執行多次的
    bug

  2. body 的寬度 100% ≠ 100% (僅 IE6)通常表現為 iframe 出現縱向捲軸時同時出現橫向捲軸,簡單粗暴的使用body{overflow-x:hidden;}是不負責任的,有時會截斷要顯示的內容第一個頁面(父頁面)
    <iframe frameborder="0" height="300" scrolling="auto" src="iframe.html" width="500">

    第二個頁面(iframe)

    HTML

    <div></div>

    CSS

    body, div{margin:0;padding:0;}div{background-color:yellow;height:500px;}

    正常效果

    IE6

    解決方案(原理同上)

    body{_width:expression(this.parentNode.offsetHeight > this.parentNode.scrollHeight ? '100%' : parseInt(this.parentNode.clientWidth) + 'px');}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.