簡體   English   中英

你把IE條件放在css文件或html文件中嗎?

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

我嘗試將IE條件放入CSS文件中,但這似乎不起作用。 是否有CSS的構造,如果瀏覽器是IE,你可以告訴它使用這種背景顏色? 如果其他條件,我也找不到任何東西,它是否存在? 有人可以提供一個例子。

IE條件進入HTML,應該用於包含一個額外的CSS文件,它將根據IE hacks的需要覆蓋CSS。


例:

<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>

我從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>

然后我可以將條件信息放在css中

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

CSS中沒有這樣的條件,但是如果各種版本的IE之間的差異不顯着,你可以使用“Holly hack”:

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

[條件注釋]( http://msdn.microsoft.com/en-us/library/ms537512 ( VS.85).aspx)是HTML注釋,因此不能在CSS上下文中使用。

如果您只想將特定的CSS規則瞄准IE,則必須使用CSS hacks。

我建議使用類似於bendewey提出的解決方案的東西,但是改為圍繞html標簽的條件類。 據我所知,這首先在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]-->

然后在你使用的CSS中:

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

與使用額外div的解決方案相比,這具有一些優勢。 有關詳細信息,請查看上面的帖子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM