在 Firefox 中忽略特定于 Webkit 的 CSS 选择器

发布于 2024-12-18 19:01:10 字数 997 浏览 2 评论 0原文

我正在开发一个 jQuery 主题,其中包括尽可能多的表单元素的样式。 最初它是为 Webkit (Chrome) 开发的。现在我想让它也适用于 Firefox。

问题是; Firefox 在某些特定于 Webkit 的语法方面存在问题。

例如:

input[type="range"]::-webkit-slider-thumb,
input[type=radio],
input[type=checkbox] {
    -webkit-appearance: none !important;
    -moz-appearance: none;
    width: 1.2em;
    height: 1.2em;
    border: 1px solid black;
    background: #666666 url(images/ui-bg_highlight-soft_50_666666_1x100.png) 50% 50% repeat-x;
}

问题是 input[type="range"]::-webkit-slider-thumb, 位。删除它,Firefox 就可以正常工作了。它还对其他语法执行此操作,例如 ::-webkit-file-upload-button::selection 以及使用 ::-webkit- 的所有其他内容... 标签。它可以识别自己的 ::-moz-... 标签,例如 ::-moz-selection 就可以了。

Webkit 似乎只是忽略 ::-moz- 标签。

有没有什么方便的方法可以让 Firefox 忽略 ::-webkit-... 标签或以其他方式处理这个问题,而不必维护每个 CSS 块的多个副本?

使用最新更新版本的 Chrome 和 Firefox。

I'm working on a jQuery theme which includes styling for as many form elements as possible.
Initially it was developed for Webkit (Chrome). Now I want to make it work with Firefox as well.

Problem is; Firefox has problems with some Webkit-specific syntax.

For example:

input[type="range"]::-webkit-slider-thumb,
input[type=radio],
input[type=checkbox] {
    -webkit-appearance: none !important;
    -moz-appearance: none;
    width: 1.2em;
    height: 1.2em;
    border: 1px solid black;
    background: #666666 url(images/ui-bg_highlight-soft_50_666666_1x100.png) 50% 50% repeat-x;
}

The problem is the input[type="range"]::-webkit-slider-thumb, bit. Remove it and Firefox works fine. It also does this for other syntax like ::-webkit-file-upload-button, ::selection and all other things using the ::-webkit-... labels. It recognizes it's own ::-moz-... labels, like ::-moz-selection just fine though.

Webkit seems to just ignore the ::-moz- labels.

Is there any convenient way to make Firefox ignore the ::-webkit-... labels or otherwise deal with this problem without having to maintain multiple copies of every CSS block?

Using freshly updated versions of Chrome and Firefox.

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

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

发布评论

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

评论(3

梦开始←不甜 2024-12-25 19:01:10

不幸的是,正如 CSS 规范所规定的那样,如果不复制声明块,这是不可能的 当遇到 CSS 规则中无法识别的选择器时,浏览器必须这样做:

选择器由直到(但不包括)第一个左大括号 ({) 的所有内容组成。选择器始终与 {} 块一起使用。当用户代理无法解析选择器(即,它不是有效的 CSS3)时,它必须 也忽略 {} 块。

在这种情况下,一个供应商的浏览器无法识别另一个供应商的前缀,因此它必须忽略该规则。

Unfortunately, it's not possible without duplicating the declaration blocks, as the CSS spec stipulates that browsers must behave this way when encountering unrecognized selectors in CSS rules:

The selector consists of everything up to (but not including) the first left curly brace ({). A selector always goes together with a {}-block. When a user agent can't parse the selector (i.e., it is not valid CSS3), it must ignore the {}-block as well.

In this case, it's one vendor's browser being unable to recognize another vendor's prefixes, so it has to ignore the rule.

情仇皆在手 2024-12-25 19:01:10

我必须阅读一点才能回答这个问题,这里有一些很好的资源,
Gecko 风格引擎 进一步阅读引擎实现,我仍然没有看到任何指示为什么它会放弃它,但我可以给你我最好的猜测, 我认为引擎正在删除整个选择器,假设 mozilla 实现 -moz-slider-thumb 伪选择器并尝试将其与 -webkit- 一起使用,它也会被删除。

我以前在所有浏览器中都见过这种行为,我认为它有时被用作针对某些浏览器的黑客攻击。

这会起作用,

input[type=radio],
input[type=checkbox] {
    -webkit-appearance: none !important;
    -moz-appearance: none;
    width: 1.2em;
    height: 1.2em;
    border: 1px solid black;
}

这不会

input[type="range"]::-webkit-slider-thumb,
input[type=radio],
input[type=checkbox] {
    -webkit-appearance: none !important;
    -moz-appearance: none;
    width: 1.2em;
    height: 1.2em;
    border: 1px solid black;
}

,或者

input[type="range"]::-moz-slider-thumb,
input[type=radio],
input[type=checkbox] {
    -webkit-appearance: none !important;
    -moz-appearance: none;
    width: 1.2em;
    height: 1.2em;
    border: 1px solid black;
}

我认为您将不得不重写两个或多个不同选择器上的属性值,这只会影响样式表的大小,因为引擎将不断删除它们不拥有的选择器。

我真的希望这至少有一点帮助。

编辑:

正如用户@BoltClock在评论中指出的,我的猜测是正确的,这里是规范的链接 w3.org/TR/css3-syntax/#rule-sets

I had to read a little bit to answer this question, here are some good resources,
Gecko Style Engine Further Reading on the Engine Implementation, Still i did not see any pointers as why it would drop it, but i can give you my best guess, I think the engine is dropping the whole selector, suppose that mozilla implements -moz-slider-thumb pseudo selector and try to use it with -webkit- and it will be dropped as well.

I have seen this behavior before in all browsers, and i think its being used as a hack to target some browsers sometimes.

This will work

input[type=radio],
input[type=checkbox] {
    -webkit-appearance: none !important;
    -moz-appearance: none;
    width: 1.2em;
    height: 1.2em;
    border: 1px solid black;
}

This wont

input[type="range"]::-webkit-slider-thumb,
input[type=radio],
input[type=checkbox] {
    -webkit-appearance: none !important;
    -moz-appearance: none;
    width: 1.2em;
    height: 1.2em;
    border: 1px solid black;
}

or this

input[type="range"]::-moz-slider-thumb,
input[type=radio],
input[type=checkbox] {
    -webkit-appearance: none !important;
    -moz-appearance: none;
    width: 1.2em;
    height: 1.2em;
    border: 1px solid black;
}

I think you will have to rewrite the properties-values on two or more different selectors, this will only affect the size of the stylesheet as the engines will keep dropping the selectors they dont own.

I really hope this helped a little bit at least.

EDIT:

As noted by user @BoltClock in the comments my guess was correct here is a link to the spec w3.org/TR/css3-syntax/#rule-sets

岁吢 2024-12-25 19:01:10

仅供参考,我最终寻求了不同的解决方案。

由于我的最终产品是样式表,因此我决定使用 CSS 编译器根据源文件生成 .CSS 文件。到目前为止一切正常。

我使用过 LessPHP 因为 .less 格式相当流行,而且我熟悉 PHP,但是其他的也可以。

请注意,我仅使用 LessPHP 来编译静态 .CSS 文件,因此该项目的最终用户不会需要它,除非他们想自己更改 .less 源文件。

FYI, I ended up going for a different solution.

Since my end product is a stylesheet, I decided to use a CSS compiler to generate the .CSS file based on a source file. So far it's working fine.

I've used LessPHP because the .less format is reasonably popular and I'm familiar with PHP, but any of the other ones will do.

Note that I'm using LessPHP only for compiling a static .CSS file, so it won't be a requirement for end-users of this project unless they want to change the .less source files themselves.

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