ie6上css碰到的坑

柴进
• 阅读 1609

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

谷歌 ie6
ie6上css碰到的坑 ie6上css碰到的坑

1.4我们发现了啥?

1.在同一个css代码块中的important没那么强力,会被后续的样式覆盖;
2.不过如果不再同一个css代码块中写的话,你大爷终究是你大爷~
3.所以我们可以利用这个漏洞,写ie6专有的样式了(伪hack)(慎用,不如用ie6的hack写法“_”)

2.margin双倍问题

2.1出现原因

当float和margin同时设置时,就会出现margin双倍的问题

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
ie6上css碰到的坑 ie6上css碰到的坑

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解决结果

ie6上css碰到的坑

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.谷歌浏览器

ie6上css碰到的坑

2.IE6浏览器

ie6上css碰到的坑

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上使用。

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引入项目就可以了

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

ie6上css碰到的坑

2.ie6

ie6上css碰到的坑

4.4怎么搞?

1.若无特殊要求,可以把div改为span
2.可以设置float属性。如float为right时,效果如下

ie6上css碰到的坑

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

ie6上css碰到的坑

2.ie6(没错,这是一张空白的图)

ie6上css碰到的坑

5.3 怎嘛整?

直接设置width、height。

6.ie6下的select宁死不从╥﹏╥...软硬不吃!ヘ(;´Д`ヘ)

6.1what?

本来我把select框的样式给调的美美的,比如这样

ie6上css碰到的坑

结果在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各个浏览器展示

谷歌 ie6上css碰到的坑
ie8 ie6上css碰到的坑
ie6 ie6上css碰到的坑

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>

差不多是这个效果了吧~(原生的也还是很整齐的啊)

ie6上css碰到的坑

ie6上的css问题就先整理到这里了,欢迎大家评论讨论
备注:转载注明出处

最近在搞一个和前端程序员相关的公号,除了技术分享之外,也增加了对于职业发展、生活记录之类的文章,欢迎大家关注,一起聊天、吐槽,一起努力工作,认真生活!
ie6上css碰到的坑

点赞
收藏
评论区
推荐文章
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
微前端项目部署方案
本文旨在通过部署微前端项目的实践过程中沉淀出一套部署方案,针对项目分别部署在不同的服务器上的场景,就一些重点步骤、碰到的问题做了一些总结。
Wesley13 Wesley13
4年前
Oracle一条SQL语句时快时慢
今天碰到一个非常奇怪的问题问题,一条SQL语句在PL/SQLdeveloper中很慢,需要9s,问题SQL:SELECT\FROMGG\_function\_locationfWHEREf.parent\_id'03000000000001'; 表GG\_function\_location有5千万的数据,parent\_id上
Stella981 Stella981
4年前
Jquery中prop( )和attr( )的区别和用法
 1.碰到的“坑”<label<inputtype"checkbox"class"checkbox"id"apple"苹果<inputtype"checkbox"class"checkbox"id"banana"香蕉<inputtype"checkbox"clas
Stella981 Stella981
4年前
OpenLayers IE 6 下 PNG 图片显示问题
众所周知,ie6下不支持png图片透明,一般的解决办法就是加滤镜效果。最近在使用OpenLayers的透明图层的时候碰到了这个问题,看了官方的API,实在没找到,后来在源码里找到了:alpha属性,它的作用就是OpenLayers在ie5.5andie<7下面支持png透明用的,不多说,直接上代码:newOpenLay
Stella981 Stella981
4年前
DIV+CSS兼容解决DIV最大宽度和最小宽度问题
在制作网页中,我们经常会碰到min/maxwidth,min/maxheight在IE6底下是无效的,这也是web设计师最头疼的问题之一,以下的方法可以解决这些难题,并且比较简约。当然,如果你还有更好的方法,希望能在主页留言给我:<divstyle"maxwidth:250px;"这段文字内容,ax/min在ie7和firfo
Wesley13 Wesley13
4年前
MySQL 8.0.22 Bug #101504 对应解决思路
大家好,我是知数堂SQL优化班老师网名:骑龟的兔子版本 :Serverversion:8.0.22MySQLCommunityServerGPL由于种种原因,需要把视图合并功能关掉,但是就碰到了,如下问题。如果不关掉下面的问题就不会碰到。setsessionoptimizer_switch'der
Stella981 Stella981
4年前
Linux安装gcc时碰到的有关问题解决(解决gcc依赖有关问题)
Linux安装gcc时碰到的有关问题解决(解决gcc依赖有关问题)rpm安装gcc时碰到的有关问题解决(解决gcc依赖有关问题)提示:error:Faileddependencies:cloogppl0.15isneededbygcc4.4.74.el6.x86\_64cpp4.4.74.el6
Stella981 Stella981
4年前
Hibernate通用序列化方案,避免延迟加载问题及序列化整个数据库至
在使用Ajax:HibernateEntityjson, FlexRemoteObject: HibernateEntityActionScriptObject的过程,经常碰到如下问题:问题:1.Hibernate碰到延迟加载的属性访问时如果session被关闭则抛出LazyInitializationException
EasyExcel碰到问题记录
作者:京东工业孙磊1、富文本中文字设置不同颜色和字体不生效StringstringCellValuecell.getStringCellValue();if(StringUtils.isNotBlank(stringCellValue)&&stringCe
京东云开发者 京东云开发者
4个月前
EasyExcel碰到问题记录
1、富文本中文字设置不同颜色和字体不生效StringstringCellValuecell.getStringCellValue();if(StringUtils.isNotBlank(stringCellValue)&&stringCellValue.c
柴进
柴进
Lv1
我说我不爱他但是我在心里知道那是假话
文章
5
粉丝
0
获赞
0