首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

伪类 | Pseudo-classes

一个CSS 伪类是一个添加到选择器的关键字,它指定所选元素的特殊状态。例如,当用户悬停在上面时,:hover可以用来改变按钮的颜色。

代码语言:javascript
复制
div:hover {
  background-color: #F89B4D;
}

伪类允许您不仅通过与文档树的内容相关,而且还能通过与外部因素如导航器的历史记录(例如:visited),内容的状态(如:checkedon某些表单元素)或鼠标的位置(例如:hover,可让您知道鼠标是否位于元素上方)来将样式应用于元素上。

注意:与伪类相比,pseudo-elements可以用来设置元素的特定部分

语法

代码语言:javascript
复制
selector:pseudo-class {
  property: value;
}

像常规类一样,您可以在选择器中链接尽可能多的伪类。

标准伪类的索引

  • :active
  • :any
  • :checked
  • :default
  • :disabled
  • :empty
  • :enabled
  • :first
  • :first-child
  • :first-of-type
  • :fullscreen
  • :focus
  • :hover
  • :indeterminate
  • :in-range
  • :invalid
  • :last-child
  • :last-of-type
  • :left
  • :link
  • :only-child
  • :only-of-type
  • :optional
  • :out-of-range
  • :read-only
  • :read-write
  • :required
  • :right
  • :root
  • :scope
  • :target
  • :valid
  • :visited

规范

Specification

Status

Comment

Fullscreen API

Living Standard

Defined :fullscreen.

HTML Living Standard

Living Standard

No change from HTML5.

Selectors Level 4

Working Draft

Defined :any-link, :local-link, :scope, :active-drop-target, :valid-drop-target, :invalid-drop-target, :current, :past, :future, :placeholder-shown, :user-error, :blank, :nth-match(), :nth-last-match(), :nth-column(), :nth-last-column(), and :matches(). No significant change for pseudo-classes defined in Selectors Level 3 and HTML5 (though semantic meaning not taken over).

HTML5

Recommendation

Defined the semantic meaning, in the HTML context, of :link, :visited, :active, :enabled, :disabled, :checked, and :indeterminate. Defined :default, :valid, :invalid, :in-range, :out-of-range, :required, :optional, :read-only, :read-write, and :dir().

CSS Basic User Interface Module Level 3

Candidate Recommendation

Defined :default, :valid, :invalid, :in-range, :out-of-range, :required, :optional, :read-only, and :read-write, but without the associated semantic meaning.

Selectors Level 3

Recommendation

Defined :target, :root, :nth-child(), :nth-last-of-child(), :nth-of-type(), :nth-last-of-type(), :last-child, :first-of-type, :last-of-type, :only-child, :only-of-type, :empty, and :not(). Defined the syntax of :enabled, :disabled, :checked, and :indeterminate, but without the associated semantic meaning. No significant change for pseudo-classes defined in CSS Level 2 (Revision 1).

CSS Level 2 (Revision 1)

Recommendation

Defined :lang(), :first-child, :hover, and :focus. No significant change for pseudo-classes defined in CSS Level 1.

CSS Level 1

Recommendation

Defined :link, :visited, and :active, but without the associated semantic meaning.

扫码关注腾讯云开发者

领取腾讯云代金券