一个CSS伪类可以命名空间吗? [英] Can a CSS pseudo-class be namespaced?

查看:106
本文介绍了一个CSS伪类可以命名空间吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CSS中使用命名空间时,您可以使用命名空间类型选择器来选择any或no namespace中的任何 div 元素:

  * | div 

它是有效的CSS命名空间一个伪类吗?例如:

  * |:first-child 

它似乎工作在基于WebKit的浏览器,它似乎不工作在IE9(下面IE9不支持命名空间)和Firefox。我不在乎它的浏览器/不工作,我只需要知道它是否是一个有效的结构。



这里是一个小提琴



我可以在 CSS语法,它无效。

解决方案

命名空间作为文档语言的一部分不直接应用于伪类或伪元素,因为它们在CSS中定义,而不是文档语言(例如XML)。然而,元素类型和属性在文档语言中定义,而不是CSS,这就是为什么它们可以命名空间。因此,在简单选择器序列中,通用选择器具体意味着任何类型。



通用选择器仅在使用时用于其他简单选择器和伪元素没有命名空间(这就是为什么选择器像 .foo #target [type = text] :first-child :: before 通常用于类似HTML的语言,其中CSS最常用于样式)。从规范


如果由 * (即没有命名空间前缀)表示的通用选择器不是简单选择器序列选择器,或紧跟一个伪元素,则可以省略 * ,并且通用选择器的存在。


因此,在您的示例中,选择器无效,因为在<$ c $之间没有通用选择器或类型选择器c> | 和

  / *这些都是无效的* / 
* |:first-child
ns | :: first-letter
| :: before
/ pre>

如果指定命名空间,则必须指定通用选择器,如果不选择特定类型:

  * | *:first-child 
* | * :: before

ns 命名空间中选择元素时也是如此:

  ns | *:first-child 
ns | * :: before


$ b b

或者当选择不在命名空间中的元素时:

  | *:first-child 
| * :: before


When using namespaces in CSS, you can select, for example, any div element in "any or no namespace" by using a namespaced type selector:

*|div

Is it valid CSS to namespace a pseudo-class? For example:

*|:first-child

It seems to work in WebKit based browsers, and it seems to not work in IE9 (below IE9 does not support namespaces at all) and Firefox. I don't care what browsers it does/doesn't work in, I just need to know whether it is a valid construct.

Here's a fiddle.

From what I can make out in the CSS grammar, it is not valid. But I may be misreading the grammar.

解决方案

Namespaces, as part of the document language, do not apply directly to pseudo-classes or pseudo-elements, as they're defined in CSS and not the document language (e.g. XML). Element types and attributes, however, are defined in the document language as opposed to CSS, which is why they can be namespaced. Consequently, in a sequence of simple selectors, the universal selector specifically means "any type".

The universal selector is only implied for other simple selectors and pseudo-elements when used without a namespace (this is why selectors like .foo, #target, [type="text"], :first-child and ::before are valid, and generally used with languages like HTML where CSS is most commonly used for styling). From the spec:

If a universal selector represented by * (i.e. without a namespace prefix) is not the only component of a sequence of simple selectors selectors or is immediately followed by a pseudo-element, then the * may be omitted and the universal selector's presence implied.

Therefore, in your example, the selector is invalid because there's neither a universal selector or a type selector between the | and the ::

/* These are all invalid */
*|:first-child
ns|::first-letter
|::before

If you specify a namespace, you must specify the universal selector if you don't select a specific type instead:

*|*:first-child
*|*::before

The same goes when selecting elements in an ns namespace:

ns|*:first-child
ns|*::before

Or when selecting elements that are not in a namespace:

|*:first-child
|*::before

这篇关于一个CSS伪类可以命名空间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆