解決ie6的瀏覽器css兼容問題

ie6上css碰到的坑

前兩天在給一個項目做東西的時候,碰到一個有意思的項目,是需要兼容ie6,有一些碰到并且解決的問題,給大家寫下來,方便大家以后碰到類似的問題哈~

能幫到你的話,點個贊唄?

1.important支持的不夠好

1.1為什么說不夠好?

important某些情況下不能決定最終的樣式屬性。

1.2代碼!代碼??!

我們通過對顏色的控制說明這一切~

<style type="text/css">
    div {
        width: 100px;
        height: 100px;
        border: aliceblue 2px solid;
    }

    .frist {
        background-color: red !important;
        background-color: blue;
    }

    .second {
        background-color: red !important;
    }

    .third {
        background-color: blue;
    }

    .second {
        background-color: blue;
    }
</style>
<div class="frist"></div>
<div class="second third"></div>
<div class="second"></div>

1.3 上圖!上圖?。?/h3>
谷歌 ie6
image
image

1.4我們發(fā)現(xiàn)了啥?

1.在同一個css代碼塊中的important沒那么強力,會被后續(xù)的樣式覆蓋;
2.不過如果不再同一個css代碼塊中寫的話,你大爺終究是你大爺~
3.所以我們可以利用這個漏洞,寫ie6專有的樣式了(偽hack)(慎用,不如用ie6的hack寫法“_”)

2.margin雙倍問題

2.1出現(xiàn)原因

當float和margin同時設置時,就會出現(xiàn)margin雙倍的問題

2.2代碼代碼!

<style type="text/css">
     /** 包裹區(qū) **/
    .area {
        width: 200px;
        height: 200px;
        background-color: #00FFFF;
    }

     /** 底部指示區(qū) **/
    .footer {
        width: 200px;
        height: 100px;
        background-color: royalblue;
    }

     /** 測試,margin 的代碼塊 **/
    .testMargin {
        width: 200px;
        height: 100px;
        float: left;
        margin: 50px;
        background-color: #0079CC;
    }

     /** 50px 指示區(qū) **/
    .checkLine {
        width: 50px;
        float: left;
        height: 100px;
        display: inline-block;
        background-color: #000;
    }

     /** 100px 指示區(qū) **/
    .checkLine2 {
        width: 50px;
        height: 100px;
        display: inline-block;
        background-color: teal;
    }
</style>
<div class="area">
    <div class="testMargin"></div>
</div>
<div class="footer">
    <!-- 50px 指示塊 -->
    <span class="checkLine"></span>
    <!-- 100px 指示塊 -->
    <span class="checkLine2"></span>
</div>

2.3來看看效果

谷歌 ie6
2.3.1.png
image

2.4.怎么解決?

方案1:

將div設置display:inline

.testMargin {
    width: 200px;
    height: 100px;
    float: left;
    display: inline;
    margin: 50px;
    background-color: #0079CC;
}

方案2:

編寫ie6的hack

.testMargin {
    width:200px;
    height:100px;
    float:left;
    margin:50px;
    background-color:#0079CC;
    _margin: 50px 25px;
}

2.5解決結(jié)果

image

3.ie6下圖片的會帶有藍灰色背景色

3.1 css代碼

<style type="text/css">
    .pngImg {
        border: #FF0000 1px solid;
        width: 200px;
        height: 200px;
    }

    .jpgImg {
        border: black 1px solid;
        width: 200px;
        height: 200px;
    }

    .pngSpan {
        border: blue 1px solid;
        display: inline-block;
        width: 200px;
        height: 200px;
        background: transparent url(https://jhcan333.github.io/can-Share/image/ie6/404.png) no-repeat center top;
        background-size: cover;
        _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="https://jhcan333.github.io/can-Share/image/ie6/404.png", sizingMethod='scale'); /*IE6有效*/
        _background: none; /*IE6有效*/
    }

    .jpgSpan {
        border: brown 1px solid;
        display: inline-block;
        width: 200px;
        height: 200px;
        background: transparent url(https://jhcan333.github.io/can-Share/image/ie6/404.jpg) no-repeat center top;
        background-size: contain;
        _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="https://jhcan333.github.io/can-Share/image/ie6/404.jpg", sizingMethod='scale'); /*IE6有效*/
        _background: none; /*IE6有效*/
    }
</style>

3.2 html標簽

<span class="pngSpan"></span>
<img class="pngImg" src="https://jhcan333.github.io/can-Share/image/ie6/404.png">
<span class="jpgSpan"></span>
<img class="jpgImg" src="https://jhcan333.github.io/can-Share/image/ie6/404.jpg">

3.3展示效果

1.谷歌瀏覽器

image

2.IE6瀏覽器

image

3.4怎么搞

IE6不支持png背景透明或半透明,所以img標簽中的圖片會帶有背景色,需要借助css濾鏡來實現(xiàn)

_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="https://jhcan333.github.io/can-Share/image/404.png",sizingMethod='scale');/*IE6有效*/
_background:none;/*IE6有效*/

在這里,首先把background取消,然后使用css濾鏡來設置。其中屬性前面添加”_”下劃線,來表示,只在ie6上使用。

3.5現(xiàn)有的封裝好的方法

<script>
    function correctPNG() {
        var arVersion = navigator.appVersion.split("MSIE")
        var version = parseFloat(arVersion[1])
        if((version >= 5.5) && (document.body.filters)) {
            for(var j = 0; j < document.images.length; j++) {
                var img = document.images[j]
                var imgName = img.src.toUpperCase()
                if(imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
                    var imgID = (img.id) ? "id='" + img.id + "' " : ""
                    var imgClass = (img.className) ? "class='" + img.className + "' " : ""
                    var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
                    var imgStyle = "display:inline-block;" + img.style.cssText
                    if(img.align == "left") imgStyle = "float:left;" + imgStyle
                    if(img.align == "right") imgStyle = "float:right;" + imgStyle
                    if(img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
                    var strNewHTML = "<span " + imgID + imgClass + imgTitle +
                        " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" +
                        "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" +
                        "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
                    img.outerHTML = strNewHTML
                    j = j - 1
                }
            }
        }
    }
    function addHandler (element, type, handler) {
        if (element.addEventListener) { // DOM2級 事件處理程序,this 指向元素本身。按照添加的順序正向執(zhí)行
            element.addEventListener(type, handler, false);
        } else if (element.attachEvent) { // IE 事件處理程序,this 指向 window。按照添加的順序反向執(zhí)行
            element.attachEvent("on" + type, handler);
        } else { // DOM0級 事件處理程序。只能綁定一個事件處理程序
            element["on" + type] = handler;
        }
    }
    addHandler(window,'load',correctPNG)
</script>

將這個js引入項目就可以了

4.ie6下的display:inline-block異常問題

4.1表現(xiàn)

本是inline的html元素設置為inline-block后,會具有inline-block的特性;
但本是block的html元素設置為inline-block后,會出現(xiàn)不在同行排布的情況;

4.2上代碼

<style type="text/css">
    .span-inline-block {
        background-color: #7FFFD4;
        display: inline-block;
        width: 100px;
        height: 100px;
        margin: 5px;
    }

    .div-inline-block {
        background-color: #00ff00;
        display: inline-block;
        width: 100px;
        height: 100px;
        margin: 5px;
    }
</style>
<span class="span-inline-block"></span>
<span class="span-inline-block"></span>
<span class="span-inline-block"></span>
<span class="span-inline-block"></span>
<div class="div-inline-block"></div>
<div class="div-inline-block"></div>
<div class="div-inline-block"></div>
<div class="div-inline-block"></div>

4.3上圖

1.谷歌

image

2.ie6

image

4.4怎么搞?

1.若無特殊要求,可以把div改為span
2.可以設置float屬性。如float為right時,效果如下
image

5.ie6下min-height和min-width失效

5.1上代碼

<style type="text/css">
    .min-div-class {
        min-width: 200px;
        min-height: 200px;
        background-color: #00FF00;
    }
</style>
<div class="min-div-class"></div>

5.2上對比圖

1.谷歌

image

2.ie6(沒錯,這是一張空白的圖)

image

5.3 怎嘛整?

直接設置width、height。

6.ie6下的select寧死不從╥﹏╥...軟硬不吃!?(;′Д`?)

6.1what?

本來我把select框的樣式給調(diào)的美美的,比如這樣

image

結(jié)果在ie6上亂了套,源碼我就不寫了,直接寫demo

6.2 demo!我的代碼!??!

<style type="text/css">
    .selectArea{
        position: relative;
        width:100px;
        height:24px;
        line-height:20px;
        padding-left: 5px;
        border:1px  solid #0079cc;
        overflow: hidden;
    }
    .testSelect{
        width:150px;
        line-height:30px;
        margin: -3px 0px;
        border:0px solid transparent;
        outline: none;
        background: none;
        appearance: none;
        -moz-appearance: none;
        -webkit-appearance:none;
    }
    .dropdown{
        position: absolute;
        right:5px;
        top:10px;
    }
</style>
<div class="selectArea">
    <select class="testSelect">
        <option>1</option>
        <option>2</option>
        <option>3</option>
    </select>
    <img class="dropdown" src="https://jhcan333.github.io/can-Share/image/ie6/arrow.png">
</div>

6.3各個瀏覽器展示

谷歌
image
ie8
image
ie6
image

6.4有木有發(fā)現(xiàn)ie6下select不聽話?

高度~邊框 ~ 完全不好整 ~

6.5如何解決?

Ie6上看起來整齊就好了,不要什么花里胡哨的東西了~hash走起! (T_T)

<style type="text/css">
    .selectArea {
        position: relative;
        width: 100px;
        height: 24px;
        line-height: 20px;
        padding-left: 5px;
        border: 1px solid #0079cc;
        overflow: hidden;
        _border: 0px #fff solid;
        _padding: 0px;
        _overflow: auto;
    }

    .testSelect {
        width: 150px;
        line-height: 30px;
        margin: -3px 0px;
        border: 0px solid transparent;
        outline: none;
        background: none;
        appearance: none;
        -moz-appearance: none;
        -webkit-appearance: none;
        _width: 100px;
        _margin: 0px;
    }

    .dropdown {
        position: absolute;
        right: 5px;
        top: 10px;
        _display: none;
    }
</style>

差不多是這個效果了吧~(原生的也還是很整齊的?。?/p>

image
ie6上的css問題就先整理到這里了,歡迎大家評論討論
備注:轉(zhuǎn)載注明出處
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • ? JavaScript 3 1. HTML對象獲取問題 32. const問題 33. event.x與even...
    橫沖直撞666閱讀 3,274評論 0 7
  • 1. 默認的內(nèi)外邊距不同 問題: 各個瀏覽器默認的內(nèi)外邊距不同 解決: *{margin:0;padding:0;...
    jslxm閱讀 916評論 0 2
  • 一、如何調(diào)試 IE 瀏覽器? 在IE7以上的版本中可以通過按快捷鍵F12調(diào)出開發(fā)人員調(diào)試框,如下圖IE7以上調(diào)試工...
    dengpan閱讀 654評論 0 2
  • 一、如何調(diào)試 IE 瀏覽器? IE調(diào)試的一般方法(css): 使用高版本IE控制臺(對于IE7以上)IE11的開發(fā)...
    婷樓沐熙閱讀 611評論 0 6
  • 一、問答部分: 1. 如何調(diào)試 IE 瀏覽器? 如果是IE7版本以上可以使用自帶的開發(fā)者工具,按F12,即可打開:...
    小木子2016閱讀 630評論 0 0

友情鏈接更多精彩內(nèi)容