前兩天在給一個項目作東西的時候,碰到一個有意思的項目,是須要兼容ie6,有一些碰到而且解決的問題,給你們寫下來,方便你們之後碰到相似的問題哈~javascript
能幫到你的話,點個讚唄?css
important某些狀況下不能決定最終的樣式屬性。html
咱們經過對顏色的控制說明這一切~前端
<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>
谷歌 | ie6 |
---|---|
![]() |
![]() |
1.在同一個css代碼塊中的important沒那麼強力,會被後續的樣式覆蓋; 2.不過若是再也不同一個css代碼塊中寫的話,你大爺終究是你大爺~ 3.因此咱們能夠利用這個漏洞,寫ie6專有的樣式了(僞hack)(慎用,不如用ie6的hack寫法「_」)
當float和margin同時設置時,就會出現margin雙倍的問題java
<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>
谷歌 | ie6 |
---|---|
![]() |
![]() |
將div設置display:inlinegit
.testMargin { width: 200px; height: 100px; float: left; display: inline; margin: 50px; background-color: #0079CC; }
編寫ie6的hack程序員
.testMargin { width:200px; height:100px; float:left; margin:50px; background-color:#0079CC; _margin: 50px 25px; }
<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>
<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">
IE6不支持png背景透明或半透明,因此img標籤中的圖片會帶有背景色,須要藉助css濾鏡來實現github
_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="https://jhcan333.github.io/can-Share/image/404.png",sizingMethod='scale');/*IE6有效*/ _background:none;/*IE6有效*/
在這裏,首先把background取消,而後使用css濾鏡來設置。其中屬性前面添加」_」下劃線,來表示,只在ie6上使用。web
<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引入項目就能夠了瀏覽器
本是inline的html元素設置爲inline-block後,會具備inline-block的特性;
但本是block的html元素設置爲inline-block後,會出現不在同行排布的狀況;
<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>
1.若無特殊要求,能夠把div改成span 2.能夠設置float屬性。如float爲right時,效果以下
<style type="text/css"> .min-div-class { min-width: 200px; min-height: 200px; background-color: #00FF00; } </style> <div class="min-div-class"></div>
直接設置width、height。
原本我把select框的樣式給調的美美的,好比這樣
結果在ie6上亂了套,源碼我就不寫了,直接寫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>
谷歌 | ![]() |
---|---|
ie8 | ![]() |
ie6 | ![]() |
高度~邊框 ~ 徹底很差整 ~
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>
差很少是這個效果了吧~(原生的也仍是很整齊的啊)
ie6上的css問題就先整理到這裏了,歡迎你們評論討論 備註:轉載註明出處
最近在搞一個和前端程序員相關的公號,除了技術分享以外,也增長了對於職業發展、生活記錄之類的文章,歡迎你們關注,一塊兒聊天、吐槽,一塊兒努力工做,認真生活!