简体   繁体   English

你把IE条件放在css文件或html文件中吗?

[英]Do you put IE conditionals in the css file or in the html file?

I tried putting the IE conditional in a CSS file, but that didn't appear to work. 我尝试将IE条件放入CSS文件中,但这似乎不起作用。 Is there a construct for CSS so you can tell it to use this background color if the browser is IE? 是否有CSS的构造,如果浏览器是IE,你可以告诉它使用这种背景颜色? I also couldn't find anything on if then else conditionals, does it exist? 如果其他条件,我也找不到任何东西,它是否存在? Can someone provide an example. 有人可以提供一个例子。

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. IE条件进入HTML,应该用于包含一个额外的CSS文件,它将根据IE hacks的需要覆盖CSS。


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>

I've taken my cue from jQuery and use my conditional formatting to create container elements 我从jQuery中获取了我的提示并使用我的条件格式来创建容器元素

<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 然后我可以将条件信息放在css中

.site { width:500px; }
.ie .site { width:400px; }
#ie5 .site { width:300px; }

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: CSS中没有这样的条件,但是如果各种版本的IE之间的差异不显着,你可以使用“Holly hack”:

div.class { /* whatever */ }
* html div.class { /* IE-only */ }

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. [条件注释]( http://msdn.microsoft.com/en-us/library/ms537512 ( VS.85).aspx)是HTML注释,因此不能在CSS上下文中使用。

If you want to aim specific CSS rules just to IE, you have to use CSS hacks. 如果您只想将特定的CSS规则瞄准IE,则必须使用CSS hacks。

I would recommend to use something similar to the solution proposed by bendewey, but go for conditional classes around the html tag instead. 我建议使用类似于bendewey提出的解决方案的东西,但是改为围绕html标签的条件类。 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/ ) 据我所知,这首先在Paul Irish的博客中提到过( 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: 然后在你使用的CSS中:

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

This has some advantages in comparison to the solution using an extra div. 与使用额外div的解决方案相比,这具有一些优势。 For the details check the post above. 有关详细信息,请查看上面的帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM