使用条件注释很容易针对Internet Explorer浏览器特定的CSS规则:

<!--[if IE 6]>
...include IE6-specific stylesheet here...
<![endif]-->

有时是Gecko引擎(Firefox)行为不当。将CSS规则只针对Firefox而不是其他浏览器的最佳方法是什么?也就是说,不仅Internet Explorer应该忽略仅限于firefox的规则,WebKit和Opera也应该忽略。

注:我正在寻找一个“干净”的解决方案。在我看来,使用JavaScript浏览器嗅探器向我的HTML中添加一个“firefox”类并不算干净。我更希望看到一些依赖于浏览器功能的东西,就像条件注释只对IE来说是“特殊的”……


当前回答

此解决方案不依赖于打开JavaScript。

@-moz-document url-prefix() { h1 { 颜色:红色; } } <h1>这应该是FF</h1>中的红色

它基于另一个Mozilla特定的css扩展。这里有一个完整的CSS扩展列表:

Mozilla CSS扩展。机场当局请注意,它们大多已弃用!

有关这个特定的css扩展的更多信息,请参阅这个问题:@-moz-document url-prefix()做什么?

其他回答

下面是如何处理三种不同的浏览器:IE, FF和Chrome

<style type='text/css'>
/*This will work for chrome */
#categoryBackNextButtons
{
    width:490px;
}
/*This will work for firefox*/
@-moz-document url-prefix() {
    #categoryBackNextButtons{
        width:486px;
    }
}
</style>
<!--[if IE]>
<style type='text/css'>
/*This will work for IE*/
#categoryBackNextButtons
{
    width:486px;
}
</style>
<![endif]-->

如何应用CSS只Firefox

下面的解决方案为您提供了在更广泛的Firefox浏览器版本中仅支持Firefox的CSS…

@supports (-moz-appearance:button) and (contain:paint) {

  body {
    background: red;
  }

}

早在2006年,Mozilla/Firefox就支持-moz-appearance:按钮。但是@supports规则直到2019年才被支持,所以那将是最早支持该规则的Firefox浏览器。include:paint将Safari浏览器排除在规则之外。Internet Explorer和早期的Trident Edge浏览器不支持@supports,因此也被排除在CSS规则之外。没有已知的Chrome浏览器应该支持-moz-appearance:按钮,所以排除。

一如既往,我所有的CSS解决方案都是100% javascript免费:)

以下是针对Firefox浏览器的一些浏览器技巧,

使用选择器hacks。

_:-moz-tree-row(hover), .selector {}

JavaScript黑客

var isFF = !!window.sidebar;

var isFF = 'MozAppearance' in document.documentElement.style;

var isFF = !!navigator.userAgent.match(/firefox/i);

媒体查询黑客

这将工作,Firefox 3.6和以后

@media screen and (-moz-images-in-menus:0) {}

如果您需要更多信息,请访问browserhacks

现在,Firefox Quantum 57已经推出了对Gecko的重大改进(统称Stylo或Quantum CSS),您可能会发现自己处于必须区分遗留版本的Firefox和Firefox Quantum的情况。

从我的回答来看:

You can use @supports with a calc(0s) expression in conjunction with @-moz-document to test for Stylo — Gecko does not support time values in calc() expressions but Stylo does: @-moz-document url-prefix() { @supports (animation: calc(0s)) { /* Stylo */ } } Here's a proof-of-concept: body::before { content: 'Not Fx'; } @-moz-document url-prefix() { body::before { content: 'Fx legacy'; } @supports (animation: calc(0s)) { body::before { content: 'Fx Quantum'; } } } Targeting legacy versions of Firefox is a little tricky — if you're only interested in versions that support @supports, which is Fx 22 and up, @supports not (animation: calc(0s)) is all you need: @-moz-document url-prefix() { @supports not (animation: calc(0s)) { /* Gecko */ } } ... but if you need to support even older versions, you'll need to make use of the cascade, as demonstrated in the proof-of-concept above.

CSS支持可以从JavaScript中使用。

如果(CSS)。Supports ("(-moz-user-select:unset)")) { console.log(“火狐! !”) }

https://developer.mozilla.org/en-US/docs/Web/CSS/Mozilla_Extensions