How to adjust CSS to display correctly in IE 6?

狂风中的少年 提交于 2020-01-24 17:48:05

问题


I have the following HTML+CSS that may be displayed on some small number of older Windows XP machines with IE 6 as a web browser. On a newer browser it should look like this:

but IE 6 makes it look like this:

So I was curious if there's anything I can do to try to fix this for IE 6?

Here's a fiddle to show the code.

The image used was this:

html:

<span id="lbl01">User Commands:</span>
<div class="divSmBtns">
<a href="#" id="idBtn" draggable="false" class="smtbBtnCopy smtbBtn" title="whatever"></a>
</div>

Css:

.divSmBtns{
    margin: -10px 10px 0px 8px;
    padding: 0px;
    display: inline-table;
}
.smtbBtnCopy{
    background-image: url(copy.gif);
}
.smtbBtn{
    width: 12px;
    height: 10px;
    margin: 0px;
    padding: 1px;
    background-size: 6px;
    background-repeat: no-repeat;
    background-color: transparent;
    border: 1px solid transparent;
    display: inline-block;
    background-position: center center;
    cursor: pointer;
    box-sizing: border-box;
    outline: none;
}
.smtbBtn:hover{
    border: 1px solid #ccc;
}
.smtbBtn:active{
    background-size: 5px;
}

回答1:


In order to support IE6, you will need to make some compromises with your mark-up and keep it simple.

As I said in the comments, two problems.

  1. display: inline-table is not supported in IE6. Use display: inline;
  2. background-size is not supported in IE6. Use an img instead of a background image.

A very simple solution would be to remove the div wrapping your a, and instead use the a to wrap an img. Something like this:

<span id="...">User Commands:</span>
<a href="#" id="..." class="smtbBtn " title="...">
    <img src='...' alt='...' />
</a>

And then, use that image for your hover interactions:

.smtbBtn { vertical-align: middle; }
.smtbBtn img { height: 14px; border: 1px solid transparent; }
.smtbBtn:hover img, .smtbBtn:active img { 
    border: 1px solid #f00; 
}

That will work well across all browser including the modern ones as well as IE going back to IE5.

Demo Fiddle: https://jsfiddle.net/abhitalks/mcgLkdc9/6/embedded/result/

Snippet:

.smtbBtn { vertical-align: middle; }
.smtbBtn img { height: 14px; border: 1px solid transparent; }
.smtbBtn:hover img, .smtbBtn:active img { 
    border: 1px solid #f00; 
}
<span id="lbl01">User Commands:</span>
<a href="#" id="idBtn" draggable="false" class="smtbBtnCopy smtbBtn" title="Copy">
    <img src='http://i.stack.imgur.com/6cALN.gif' alt='' />
</a>

Edit:

As was pointed out that IE6 will not support transparent for border-color, you will have to resort to either hacks or conditional comments (as suggested in the other answer).

  1. Hack: You could prefix a property by _ (underscore) which will be recognized only by IE6 and ignored by other browsers. Similarly prefixing with an * (asterisk) will be recognized only by IE7.

CSS:

.smtbBtn img { height: 14px; border: 1px solid transparent; }
.smtbBtn img { _border-color: pink; _filter: chroma(color=pink); }

Fiddle 2: https://jsfiddle.net/abhitalks/mcgLkdc9/8/embedded/result/

  1. Conditional comments: You could use conditional comments to inject HTML i.e. in your case an img tag with a specific class for those styles. Remember that conditional comments are used for HTML not CSS.

Markup:

<a href="#" id="idBtn" draggable="false" class="smtbBtnCopy smtbBtn" title="Copy">
    <!--[if IE 6]>
        <img src='http://i.stack.imgur.com/6cALN.gif' class='ie6img' alt='' />
    <![endif]-->
    <!--[if gte IE 7]>
        <img src='http://i.stack.imgur.com/6cALN.gif' alt='' />
    <![endif]-->
    <!--[if !(IE)]><!-->
        <img src='http://i.stack.imgur.com/6cALN.gif' alt='' />
    <!--<![endif]-->
</a>

CSS:

.smtbBtn img { height: 14px; border: 1px solid transparent; }
.ie6img {
    border-color: pink; filter: chroma(color=pink);
}

Fiddle 3: https://jsfiddle.net/abhitalks/a9f80cqf/4/embedded/result/

Note: I have not tested the above two methods in IE6. I am using developer tools emulation in IE11. Which means, you will have to tweak that based on your experience.



来源:https://stackoverflow.com/questions/33296153/how-to-adjust-css-to-display-correctly-in-ie-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!