【问题标题】:Why do non-ie browsers read IE conditionals with !important?为什么非 ie 浏览器使用 !important 读取 IE 条件?
【发布时间】:2012-09-20 11:54:10
【问题描述】:

当使用带有 !important 的 IE 条件(例如下面的代码)时,无论浏览器如何,背景都默认为带有 !important 的背景色 即使在 IE 中有条件的

如何重写这一点,以便浏览器接受小于 IE9 的背景颜色和 IE9 和其他浏览器中的图像(高度/宽度:100%,无重复)?

注意:我计划在默认情况下主题自动设置颜色或图像的 Wordpress 网站中使用它,因此我必须使用 !important 来覆盖默认主题样式。

body {
<!--[if lt IE 9]>
background-color:#c1beb2 !important;
background:#c1beb2 !important;
<![endif]-->
<!--[if gte IE 9]>
background-image: url("http://example.com/wp-content/example.png") !important;
background-position:left bottom;
background-size: 100% 100%;
background-repeat: no-repeat;
<![endif]-->
<![if !IE]>
background-image: url("http://example.com/wp-content/example.png") !important;
background-position:left bottom;
background-size: 100% 100%;
background-repeat: no-repeat;
<![endif]>
}

【问题讨论】:

  • 你能整理出一个 jsfiddle 让我们试试看吗?
  • 那是...不是您使用 IE 条件的方式...

标签: html css internet-explorer cross-browser


【解决方案1】:

好的,等一下。据我所知,这些(!)浏览器开关在 css 中不起作用。因此,您需要像这样在 html 中进行切换部分:

<!--[if lt IE 9]>
  <link rel="stylesheet" type="text/css" href="IE9.css">
<![endif]-->
<!--[if gte IE 9]>
  <link rel="stylesheet" type="text/css" href="IEgte9.css">
<![endif]-->

然而,这是一种糟糕的风格。通常,您不希望浏览器具有单独的样式。在极少数情况下,您也需要。检查 html5 样板以获取有关此的一些信息。

【讨论】:

  • 我有一个挑剔的客户,他希望背景图像 100% 拉伸为渐变。有更好的主意吗?它在 ie9(非兼容模式)和 chrome/firefox/safari 中运行良好...但不适用于 ie8 等旧浏览器。
【解决方案2】:

条件 cmets 旨在用于 HTML,而不是 CSS。

也就是说,在你的 HTML 中链接到一个特定于 IE 的样式表:

<!--[if lte IE 9]> 
<link rel="stylesheet" href="/ie.css" type="text/css" media="no title" charset="utf-8" />
<![endif]-->

这是正确的做法。如果您不想创建一个完整的其他 IE 特定样式表,您可以依赖 CSS IE Hacks

【讨论】:

  • 哦,让我试试看,确认它工作正常...请 5 分钟。
  • 好的,我已经在结束头标签之前添加了它:&lt;!--[if lt IE 9]&gt; &lt;link rel="stylesheet" href="http://example.com/ie.css" type="text/css" media="no title" charset="utf-8" /&gt; &lt;![endif]--&gt; ` example.com/notie.css" type= "text/css" media="no title" charset="utf-8" /> ` 但它仍然无法正常工作。我做错了什么?
  • 您希望特定于 IE 的样式表出现在您的主样式表之后,因此它的特定 CSS 会覆盖您的初始 CSS。请记住,在 CSS 中:最后一个样式获胜。
  • 就在结束头标签之前,其余的都在顶部附近,所以没关系。还有其他想法吗?
  • 好吧,你不应该需要 notie.css 文件。您应该只拥有普通的 CSS 文件,背景声明为您希望在大多数浏览器中出现的方式(如果需要,使用 !important)。在你的普通 CSS 文件之后,有条件链接到你的 IE 特定的 CSS,然后重新声明你想要的背景,可能也使用 !important 声明。
猜你喜欢
  • 2010-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-30
  • 2010-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多