Do you put IE conditionals in the css file or in t

2020-07-11 09:06发布

I tried putting the IE conditional in a CSS file, but that didn't appear to work. Is there a construct for CSS so you can tell it to use this background color if the browser is IE? I also couldn't find anything on if then else conditionals, does it exist? Can someone provide an example.

标签: css
5条回答
Bombasti
2楼-- · 2020-07-11 09:21

There's no such conditionals in CSS, but you can use the "Holly hack" if the differences between various versions of IE aren't significant:

div.class { /* whatever */ }
* html div.class { /* IE-only */ }
查看更多
聊天终结者
3楼-- · 2020-07-11 09:22

I've taken my cue from jQuery and use my conditional formatting to create container elements

<body class="center">
<!--[if IE 5]><div id="ie5" class="ie"><![endif]-->
<!--[if IE 6]><div id="ie6" class="ie"><![endif]-->
<!--[if IE 7]><div id="ie7" class="ie"><![endif]-->
<!--[if IE 8]><div id="ie8" class="ie"><![endif]-->
    <div class="site text-left">

    </div>
<!--[if IE]></div><![endif]-->
</body>

then I can put the conditional information in css like such

.site { width:500px; }
.ie .site { width:400px; }
#ie5 .site { width:300px; }
查看更多
ゆ 、 Hurt°
4楼-- · 2020-07-11 09:30

The [conditional comments](http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx) are HTML comments and thus cannot be used in a CSS context.

If you want to aim specific CSS rules just to IE, you have to use CSS hacks.

查看更多
疯言疯语
5楼-- · 2020-07-11 09:32

I would recommend to use something similar to the solution proposed by bendewey, but go for conditional classes around the html tag instead. As far as I know this was first mentioned in Paul Irish's Blog ( http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ )

<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]>    <html class="ie7"> <![endif]-->
<!--[if IE 8 ]>    <html class="ie8"> <![endif]-->
<!--[if IE 9 ]>    <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->

and then in the css you use:

.box {background: blue;}
.ie7 .box {background: green;}

This has some advantages in comparison to the solution using an extra div. For the details check the post above.

查看更多
闹够了就滚
6楼-- · 2020-07-11 09:38

The IE conditional(s) go in the HTML, and should be used to include an additional CSS file that will overwrite CSS as needed for IE hacks.


Example:

<head>
    <style type="text/css">
    @import url(/styles.css);
    </style>
    <!--[if lte IE 6]>
        <link rel="stylesheet" type="text/css" href="ie6.css" />
    <![endif]-->
</head>
查看更多
登录 后发表回答