Why can't I combine ::selection and ::-moz-selection in one CSS rule? [duplicate]

会有一股神秘感。 提交于 2019-12-07 14:12:10

问题


I'm a big fan of using ::selection and ::-moz-selection to spruce up a website a bit - I hate the default blue and white colours!

However, I find one little thing that really bugs me: I can't put ::selection and ::-moz-selection in the same CSS rule, like this:

::selection, ::-moz-selection {
    background:#8A1920;
    color:#FFFF;
}

jsFiddle link

I find this quite annoying, because it means that if I want to change my background colour (or some other selection style) I have to edit two separate rules. It also violates a policy I follow almost religiously: D.R.Y. (don't repeat yourself).

I tested this in Chrome 28, Chrome Canary 30, Firefox 22, Firefox 23, IE 9 and IE 10 and they all yield the same result. What's going wrong here?

If they must remain separate, is there any way to have them join further on? Something like:

.selected {
    background:#8A1920;
    color:#FFF;
}

::selection {
    @copy-from(.selected); /* I know this line is completely made up */
}

::-moz-selection {
    @copy-from(.selected);
}

Would be really useful.


回答1:


This is from the documentation:

Gecko is the only engine requiring the prefix. Due to the fact that the CSS parsing rules require dropping the whole rule when encountering an invalid pseudo-element, two separate rules must be written: ::-moz-selection, ::selection {...}. The rule would be dropped on non-Gecko browsers as ::-moz-selection is invalid on them.

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



来源:https://stackoverflow.com/questions/18114493/why-cant-i-combine-selection-and-moz-selection-in-one-css-rule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!