条件 CSS 文件不会覆盖常规 CSS

发布于 2024-09-02 21:42:39 字数 224 浏览 10 评论 0原文

如果 IE 版本低于 8,我将使用条件注释链接到 css 文件(我们称之为“IEonly.css”)。我试图覆盖常规 css 文件中的一些属性。奇怪的是,IEonly.css 会正确设置新的 css 属性,但不会覆盖常规 CSS 文件中的属性!

(我正在尝试使此功能适用于 IE7)。

帮助!

编辑:我在 css 样式后添加了 !important 来看看它是否有帮助。事实并非如此。

I am using conditional comments to link to a css file (let's call it "IEonly.css") if the IE version is less than 8. I am trying to override some properties in the regular css file. Strangely enough, IEonly.css will set new css properties correctly, but it won't override the properties in the regular CSS file!

(I am trying to make this work for IE7).

Help!

EDIT: I added an !important after the css style to see if it would help. It didn't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

亣腦蒛氧 2024-09-09 21:42:40

给定多个样式表(即使某些样式表通过条件注释对其他浏览器隐藏),则 的正常规则级联将适用。

确保您的选择器适当具体,并且以正确的顺序应用样式表。

Given multiple stylesheets (even if some are hidden from other browsers with conditional comments) then the normal rules of the cascade will apply.

Make sure your selectors are suitably specific, and that you apply the stylesheets in the right order.

总以为 2024-09-09 21:42:40

如果您在两个样式表中使用相同的选择器,那么只要将条件 IE 样式表放在常规样式表之后就可以了。如果您这样做并且您的 IE 表没有被接受,那么您可能需要编写更具体的选择器。

#sidebar #nav li a { }

而不是...

#nav li a { }

li a { }

If you are using the same selectors in both stylesheets then you should be fine as long as you place the conditional IE stylesheet after the regular stylesheet. If you do that and your IE sheet isn't taking then you might need to write more specific selectors.

#sidebar #nav li a { }

instead of...

#nav li a { }

or

li a { }
凉城凉梦凉人心 2024-09-09 21:42:40

不要忘记,您还可以使用 !important 规则来覆盖 CSS 定义。 这里是有关该规则的 W3C 文档

Don't forget that you can also use the !important rule to override CSS definitions. Here is the W3C documentation on that rule.

岁月无声 2024-09-09 21:42:40

也许您可以重新组织样式表以默认为 IE 样式,并使用 if !IE 条件来覆盖“良好的浏览器”。

Perhaps you can reorganize the stylesheets to default to IE styles and use an if !IE conditional for "good browser" overrides.

弄潮 2024-09-09 21:42:40

根据我自己对类似问题的经验,我猜测 IEonly.css 文件中潜伏着一些错误或丢失的字符。追踪它们可能非常痛苦,因此请执行以下操作:

  1. 暂时从 IEonly.css 中删除所有 CSS,除了您将用于覆盖普通 CSS 的部分。测试一下这是否有效。如果是这样,请继续将代码粘贴回文件中,按您认为合适的部分进行。希望你能找到问题所在。
  2. 如果当文件中仅存在该部分代码时您的覆盖不起作用,请确保您拥有正确的选择器并且特异性良好。

您还可以尝试阅读 http://www.w3.org/TR /CSS2/cascade.html#important-rules 了解更多信息。

您能发布一些代码供我们查看吗?那会有帮助的。

Based on my own experience of similar problems I would guess that there are some bad or missing character lurking somewhere in your IEonly.css file. They can be a real pain to track down, so do the following:

  1. Temporarily remove all CSS from IEonly.css, except for the part that you will use to override the normal CSS. Test to see if this works. If it does, continue to paste the code back into the file, in sections as you see fit. Hopefully you'll find the problem.
  2. If your override did not work when only that part of the code existed in the file, make sure that you have the correct selectors and that the specificity is OK.

You can also try reading http://www.w3.org/TR/CSS2/cascade.html#important-rules for more information.

Can you publish some code for us to look at? That would help.

哭了丶谁疼 2024-09-09 21:42:40

我向该元素添加了一个类,并在带有类选择器的 IEonly 样式表和不带类选择器的常规样式表上引用了它。这导致 IEonly 样式声明覆盖常规样式声明。

I added a class to the element and referenced it on the IEonly stylesheet with the class selector and the regular style sheet without. This caused the IEonly style declaration to override the regular one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文