w3school 上的定义
伪类用于向某些选择器添加特殊的效果。
伪类
W3C 上的定义
The pseudo-class concept is introduced to permit selection based on information that lies outside of the document tree or that cannot be expressed using the other simple selectors.
A pseudo-class always consists of a “colon” (:) followed by the name of the pseudo-class and optionally by a value between parentheses.
Pseudo-classes are allowed in all sequences of simple selectors contained in a selector. Pseudo-classes are allowed anywhere in sequences of simple selectors, after the leading type selector or universal selector (possibly omitted). Pseudo-class names are case-insensitive. Some pseudo-classes are mutually exclusive, while others can be applied simultaneously to the same element. Pseudo-classes may be dynamic, in the sense that an element may acquire or lose a pseudo-class while a user interacts with the document.
伪类存在的意义是为了通过选择器找到那些不存在 DOM 树中的信息以及不能被常规 CSS 选择器获取到的信息。
伪类总是由“冒号”(:)和伪类的名称以及括号之间的值组成。
在选择器中包含的所有简单选择器序列中都允许使用伪类。在引导类型选择器或通用选择器(可能省略了)之后,伪类可以在简单的选择器序列中任意位置进行。伪类的名称是不区分大小写的。有些伪类是互斥的,而另一些则可以同时应用于相同的元素。伪类可以是动态的,因为元素可以在用户与文档交互时获得或丢失伪类。
Selector | Meaning | CSS |
---|---|---|
:active | 选择正在被激活的元素 | 1 |
:hover | 选择被鼠标悬浮着元素 | 1 |
:link | 选择未被访问的元素 | 1 |
:visited | 选择已被访问的元素 | 1 |
:first-child | 选择满足是其父元素的第一个子元素的元素 | 2 |
:lang | 选择带有指定 lang 属性的元素 | 2 |
:focus | 选择拥有键盘输入焦点的元素 | 2 |
:enable | 选择每个已启动的元素 | 3 |
:disable | 选择每个已禁止的元素 | 3 |
:checked | 选择每个被选中的元素 | 3 |
:target | 选择当前的锚点元素 | 3 |
:first-of-type | 选择满足是其父元素的第一个某类型子元素的元素 | 3 |
:last-of-type | 选择满足是其父元素的最后一个某类型子元素的元素 | 3 |
:only-of-type | 选择满足是其父元素的唯一一个某类型子元素的元素 | 3 |
:nth-of-type(n) | 选择满足是其父元素的第n个某类型子元素的元素 | 3 |
:nth-last-of-type(n) | 选择满足是其父元素的倒数第n个某类型的元素 | 3 |
:only-child | 选择满足是其父元素的唯一一个子元素的元素 | 3 |
:last-child | 选择满足是其父元素的最后一个元素的元素 | 3 |
:nth-child(n) | 选择满足是其父元素的第n个子元素的元素 | 3 |
:nth-last-child(n) | 选择满足是其父元素的倒数第n个子元素的元素 | 3 |
:empty | 选择满足没有子元素的元素 | 3 |
:in-range | 选择满足值在指定范围内的元素 | 3 |
:out-of-range | 选择值不在指定范围内的元素 | 3 |
:invalid | 选择满足值为无效值的元素 | 3 |
:valid | 选择满足值为有效值的元素 | 3 |
:not(selector) | 选择不满足selector的元素 | 3 |
:optional | 选择为可选项的表单元素,即没有“required”属性 | 3 |
:read-only | 选择有”readonly”的表单元素 | 3 |
:read-write | 选择没有”readonly”的表单元素 | 3 |
:root | 选择根元素 | 3 |
w3school 上的定义
CSS 伪元素用于向某些选择器设置特殊效果。
W3C 上的定义
Pseudo-elements create abstractions about the document tree beyond those specified by the document language. For instance, document languages do not offer mechanisms to access the first letter or first line of an element’s content. Pseudo-elements allow authors to refer to this otherwise inaccessible information. Pseudo-elements may also provide authors a way to refer to content that does not exist in the source document (e.g., the ::before and ::after pseudo-elements give access to generated content).
A pseudo-element is made of two colons (::) followed by the name of the pseudo-element.
This :: notation is introduced by the current document in order to establish a discrimination between pseudo-classes and pseudo-elements. For compatibility with existing style sheets, user agents must also accept the previous one-colon notation for pseudo-elements introduced in CSS levels 1 and 2 (namely, :first-line, :first-letter, :before and :after). This compatibility is not allowed for the new pseudo-elements introduced in this specification.
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. Note: A future version of this specification may allow multiple pseudo-elements per selector.
Selector | Meaning | CSS |
---|---|---|
::first-letter | 选择指定元素的第一个单词 | 1 |
::first-line | 选择指定元素的第一行 | 1 |
::after | 在指定元素的内容前面插入内容 | 2 |
::before | 在指定元素的内容后面插入内容 | 2 |
::selection | 选择指定元素中被用户选中的内容 | 3 |
W3C 指万维网联盟(World Wide Web Consortium)
如何阅读W3C规范(高博翻译版)
http://www.chinaw3c.org/how-to-read-spec-gb.html