可以在CSS中单独使用伪元素吗?

前端之家收集整理的这篇文章主要介绍了可以在CSS中单独使用伪元素吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
根据W3C,选择器的定义不包括伪元素:
https://www.w3.org/TR/css3-selectors/#selector-syntax

以上链接说:

A selector is a chain of one or more sequences of simple selectors
separated by combinators.

它还说:

A simple selector is either a type selector,universal selector,
attribute selector,class selector,ID selector,or pseudo-class.

关于如何使用伪元素,它说:

One pseudo-element may be appended to the last sequence of simple
selectors in a selector.

Only one pseudo-element may appear per selector,and if present it
must appear after the sequence of simple selectors that represents the
subjects of the selector.

那么这是否意味着伪元素只能是“有效”选择器的后缀而不应该单独使用?

解决方法

does that mean that a pseudo-element can only be a suffix to a “valid”
selector and should not be used alone?

你的结论不正确,因为universal selector *可以省略.

If a universal selector represented by * […] is immediately
followed by a 07001,then the * may be omitted and the
universal selector’s presence implied.

所以你可以单独使用一个伪元素,例如::之前,因为在引擎盖下它将被视为* ::之前.

::before {
  content: 'Hello!';
}
原文链接:https://www.f2er.com/css/215898.html

猜你在找的CSS相关文章