解決ie6上碰到的css兼容問題

ie6上css碰到的坑

前兩天在給一個項目作東西的時候,碰到一個有意思的項目,是須要兼容ie6,有一些碰到而且解決的問題,給你們寫下來,方便你們之後碰到相似的問題哈~javascript

能幫到你的話,點個讚唄?css

1.important支持的不夠好

1.1爲何說不夠好?

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

1.2代碼!代碼!!

咱們經過對顏色的控制說明這一切~java

<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 上圖!上圖!!

谷歌 ie6
1
1

1.4咱們發現了啥?

1.在同一個css代碼塊中的important沒那麼強力,會被後續的樣式覆蓋;
2.不過若是再也不同一個css代碼塊中寫的話,你大爺終究是你大爺~
3.因此咱們能夠利用這個漏洞,寫ie6專有的樣式了(僞hack)(慎用,不如用ie6的hack寫法「_」)
複製代碼

2.margin雙倍問題

2.1出現緣由

當float和margin同時設置時,就會出現margin雙倍的問題git

2.2代碼代碼!

<style type="text/css"> /** 包裹區 **/ .area { width: 200px; height: 200px; background-color: #00FFFF; } /** 底部指示區 **/ .footer { width: 200px; height: 100px; background-color: royalblue; } /** 測試,margin 的代碼塊 **/ .testMargin { width: 200px; height: 100px; float: left; margin: 50px; background-color: #0079CC; } /** 50px 指示區 **/ .checkLine { width: 50px; float: left; height: 100px; display: inline-block; background-color: #000; } /** 100px 指示區 **/ .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
1
1

2.4.怎麼解決?

方案1:

將div設置display:inlinegithub

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

方案2:

編寫ie6的hackweb

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

2.5解決結果

1

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.谷歌瀏覽器

1

2.IE6瀏覽器

1

3.4怎麼搞

IE6不支持png背景透明或半透明,因此img標籤中的圖片會帶有背景色,須要藉助css濾鏡來實現瀏覽器

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

在這裏,首先把background取消,而後使用css濾鏡來設置。其中屬性前面添加」_」下劃線,來表示,只在ie6上使用。bash

3.5現有的封裝好的方法

<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 指向元素自己。按照添加的順序正向執行
            element.addEventListener(type, handler, false);
        } else if (element.attachEvent) { // IE 事件處理程序,this 指向 window。按照添加的順序反向執行
            element.attachEvent("on" + type, handler);
        } else { // DOM0級 事件處理程序。只能綁定一個事件處理程序
            element["on" + type] = handler;
        }
    }
    addHandler(window,'load',correctPNG)
</script>
複製代碼

將這個js引入項目就能夠了app

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

4.1表現

本是inline的html元素設置爲inline-block後,會具備inline-block的特性; 但本是block的html元素設置爲inline-block後,會出現不在同行排布的狀況;

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.谷歌

1

2.ie6

1

4.4怎麼搞?

1.若無特殊要求,能夠把div改成span
2.能夠設置float屬性。如float爲right時,效果以下
複製代碼

1

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.谷歌

1

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

1

5.3 怎嘛整?

直接設置width、height。

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

6.1what?

原本我把select框的樣式給調的美美的,好比這樣

1

結果在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各個瀏覽器展現

谷歌
1
ie8
1
ie6
1

6.4有木有發現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>
複製代碼

差很少是這個效果了吧~(原生的也仍是很整齊的啊)

1

ie6上的css問題就先整理到這裏了,歡迎你們評論討論
備註:轉載註明出處
複製代碼
相關文章
相關標籤/搜索