ie6上css碰到的坑
前兩天在給一個項目做東西的時候膊畴,碰到一個有意思的項目,是需要兼容ie6樊销,有一些碰到并且解決的問題饵蒂,給大家寫下來声诸,方便大家以后碰到類似的問題哈~
能幫到你的話,點個贊唄苹享?
1.important支持的不夠好
1.1為什么說不夠好双絮?
important某些情況下不能決定最終的樣式屬性。
1.2代碼得问!代碼6谂省!
我們通過對顏色的控制說明這一切~
<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 上圖宫纬!上圖7倌印!
谷歌 | 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)原因
當(dāng)float和margin同時設(shè)置時孤个,就會出現(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設(shè)置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下圖片的會帶有藍(lán)灰色背景色
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標(biāo)簽
<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標(biāo)簽中的圖片會帶有背景色,需要借助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濾鏡來設(shè)置。其中屬性前面添加”_”下劃線淆九,來表示统锤,只在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元素設(shè)置為inline-block后唧席,會具有inline-block的特性擦盾;
但本是block的html元素設(shè)置為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.可以設(shè)置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 怎嘛整?
直接設(shè)置width症见、height喂走。
6.ie6下的select寧死不從╥﹏╥...軟硬不吃!?(;′Д`?)
6.1what谋作?
本來我把select框的樣式給調(diào)的美美的芋肠,比如這樣
image
結(jié)果在ie6上亂了套,源碼我就不寫了遵蚜,直接寫demo
6.2 demo!我的代碼L亍!吭净!
<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>
差不多是這個效果了吧~(原生的也還是很整齊的啊)
image
ie6上的css問題就先整理到這里了不撑,歡迎大家評論討論
備注:轉(zhuǎn)載注明出處