简体   繁体   English

有条件的CSS不起作用

[英]conditional css is not working

hi ai am triyng to using conditional css like this in my style sheet 嗨,我是Triyng,在我的样式表中使用像这样的条件CSS

 div.box {  
     width: 400px;  
     [if IE 6] width: 600px;  
     padding: 0 100px;  
 }

i want to set a different width to my div in ie6 , so i am trying this code and not working , how can i set a different width in ie6 我想在ie6中为我的div设置不同的宽度,所以我正在尝试此代码而无法正常工作,如何在ie6中设置不同的宽度

also i tried this in my style sheet 我也在我的样式表中尝试过

[if IE]
div.box {
         width: 600px;  
         padding: 0 100px; 
}
[endif]

The best way to do conditionals for ie6 is to add a conditional include in the head of the html page like this: 为ie6做条件的最好方法是在html页面的头部添加条件包含,如下所示:

<!--[if IE 6]>
  <link rel="stylesheet" href="[something like style/ie6.css goes here]" type="text/css">
<![endif]-->

and then just add your new widths as exceptions to elements in the ie6.css style sheet. 然后只需将新的宽度作为例外添加到ie6.css样式表中的元素即可。

Conditional comments are for use inside HTML documents, not inside CSS files. 条件注释只能在HTML文档中使用,而不能在CSS文件中使用。 IE's HTML parser will interpret the rule inside the comment - as far as I know, there's no equivalent logic in its CSS parser. IE的HTML解析器将解释注释中的规则-据我所知,其CSS解析器中没有等效的逻辑。

The following code will work: 以下代码将起作用:

<!--[if IE 6]>
<style type="text/css">
div.box {
         width: 600px;  
         padding: 0 100px; 
}
</style>
<![endif]-->

In most cases, it's better to use the conditional comment to include an external stylesheet, rather than just wrapping a block of inline styles, like so: 在大多数情况下,最好使用条件注释包括一个外部样式表,而不是仅包装一些内联样式,例如:

<!--[if IE 6]>
<link rel="stylesheet" href="/css/IEpatches.css" type="text/css" media="screen" charset="utf-8"/>
<![endif]-->

Edited to specify IE6 only as requested in the question. 编辑为仅根据问题要求指定IE6。

Have you tried? 你有没有尝试过?

<!--[if IE]>
// Your conditional CSS
<![endif]-->

I think that your formatting is wrong for your conditional statement. 我认为您的条件语句格式错误。

Conditional Comments 有条件的评论

Have you read the Conditional-CSS documentation? 您阅读过条件CSS文档吗? Specifically, do you run your CSS through the Conditional-CSS compiler? 具体来说,您是否通过条件CSS编译器运行CSS? Because if you don't, you'll just send the CSS to the browser unchanged - there is no way Conditional-CSS can do its magic then. 因为如果不这样做,您将原封不动地将CSS发送到浏览器-然后,条件CSS便无法发挥其魔力。

As you probably have learned by now, conditional comments are parsed in the HTML output, not within the CSS file itself, unless you are using third party software . 您可能已经知道,除非使用第三方软件 ,否则条件注释将在HTML输出中而不是CSS文件本身中进行解析。

Another way you can target IE within your CSS files without hacks : 您可以将CSS文件中的IE作为目标的另一种方式,而不会受到黑客攻击

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

It's become common to use variations of this technique for IE, I believe it was made popular by HTML5 Boilerplate . 在IE中使用此技术的变体已经很普遍,我相信它已被HTML5 Boilerplate所流行。 You are basically using conditional comments to add a class to the <html> tag (or body tag if you wish) so you can target elements in your CSS files like so: 基本上,您使用条件注释将类添加到<html>标记(如果需要,则添加到body标记)中,以便可以将CSS文件中的元素定位为:

.ie6 #header {
    /* some ie6 only styles here */
}

To me, this is more maintainable than using separate stylesheets, but suffers the very mild setback of other browsers reading (but not applying) the IE styles. 对我来说,这比使用单独的样式表更易于维护,但会遭受其他浏览器(但未应用)IE样式的轻微挫折。

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

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