CSS Pseudo-class

什么是伪类?

伪类用于定义元素的特殊状态。

例如,它可以用于:

  • 设置鼠标悬停在元素上时的样式
  • 为已访问和未访问链接设置不同的样式
  • 设置元素获得焦点时的样式
请将鼠标悬停在我上面

语法

伪类的语法:

selector:pseudo-class {
  property: value;
}

锚伪类

链接能够以不同的方式显示:

实例

/* 未访问的链接 */
a:link {
  color: #FF0000;
}
/* 已访问的链接 */
a:visited {
  color: #00FF00;
}
/* 鼠标悬停链接 */
a:hover {
  color: #FF00FF;
}
/* 已选择的链接 */
a:active {
  color: #0000FF;
}

亲自试一试

注意:a:hover 必须在 CSS 定义中的 a:linka:visited 之后,才能生效!a:active 必须在 CSS 定义中的 a:hover 之后才能生效!伪类名称对大小写不敏感。

伪类和 CSS 类

伪类可以与 CSS 类结合使用:

当您将鼠标悬停在例子中的链接上时,它会改变颜色:

实例

a.highlight:hover {
  color: #ff0000;
}

亲自试一试

悬停在 <div> 上

在 <div> 元素上使用 :hover 伪类的实例:

实例

div:hover {
  background-color: blue;
}

亲自试一试

简单的工具提示悬停

把鼠标悬停到

元素以显示

元素(类似工具提示的效果):

悬停到我上面来显示

元素。

嘿嘿,我在这里!

实例

p {
  display: none;
  background-color: yellow;
  padding: 20px;
}
div:hover p {
  display: block;
}

亲自试一试

CSS - :first-child 伪类

:first-child 伪类与指定的元素匹配:该元素是另一个元素的第一个子元素。

匹配首个

元素

在下面的例子中,选择器匹配作为任何元素的第一个子元素的任何

元素:

实例

p:first-child {
  color: blue;
}

亲自试一试

匹配所有

元素中的首个 元素

在下面的例子中,选择器匹配所有

元素中的第一个 元素:

实例

p i:first-child {
  color: blue;
}

亲自试一试

匹配所有首个

元素中的所有 元素

在下面的例子中,选择器匹配作为另一个元素的第一个子元素的

元素中的所有 元素:

实例

p:first-child i {
  color: blue;
}

亲自试一试

CSS - :lang 伪类

:lang 伪类允许您为不同的语言定义特殊的规则。

在下面的例子中,:lang 为属性为 lang="en" 的 元素定义引号:

实例






Some text A quote in a paragraph Some text.

亲自试一试

更多实例

为超链接添加不同样式
本例演示如何向超链接添加其他样式。
هاوسا: ƴaɓaɓa ɗan shirɗe ɗanɗin ɗanɗin :focus.
هاوسا: ƴanamici ɗanamici koye koye ɗan ɗan :focus pseudo-class.

All CSS pseudo-classes

Selector Example Example description
:active a:active هاوسا: ƴaɓaɓa ɗan shirɗe ɗanɗin ɗanɗin a ɗanɗin yimi.
:checked input:checked هاوسا: ƴaɓaɓa ɗan shirɗe ɗanɗin ɗanɗin <input> ɗanɗin wanda ɗa ɗanɗin yimi.
:disabled input:disabled هاوسا: ƴaɓaɓa ɗan shirɗe ɗanɗin ɗanɗin <input> ɗanɗin wanda ɗa ɗanɗin ɗanɗin.
:empty p:empty هاوسا: ƴaɓaɓa ɗan shirɗe ɗanɗin ɗanɗin <p> ɗanɗin wanda ɗa ɗanɗin ɗanɗin ɗanɗin ɗanɗin.
:enabled input:enabled 选择每个已启用的 <input> 元素。
:first-child p:first-child 选择作为其父的首个子元素的每个 <p> 元素。
:first-of-type p:first-of-type 选择作为其父的首个 <p> 元素的每个 <p> 元素。
:focus input:focus 选择获得焦点的 <input> 元素。
:hover a:hover 选择鼠标悬停其上的链接。
:in-range input:in-range 选择具有指定范围内的值的 <input> 元素。
:invalid input:invalid 选择所有具有无效值的 <input> 元素。
:lang(language) p:lang(it) 选择每个 lang 属性值以 "it" 开头的 <p> 元素。
:last-child p:last-child 选择作为其父的最后一个子元素的每个 <p> 元素。
:last-of-type p:last-of-type 选择作为其父的最后一个 <p> 元素的每个 <p> 元素。
:link a:link 选择所有未被访问的链接。
:not(selector) :not(p) 选择每个非 <p> 元素的元素。
:nth-child(n) p:nth-child(2) 选择作为其父的第二个子元素的每个 <p> 元素。
:nth-last-child(n) p:nth-last-child(2) 选择作为父的第二个子元素的每个<p>元素,从最后一个子元素计数。
:nth-last-of-type(n) p:nth-last-of-type(2) 选择作为父的第二个<p>元素的每个<p>元素,从最后一个子元素计数
:nth-of-type(n) p:nth-of-type(2) 选择作为其父的第二个 <p> 元素的每个 <p> 元素。
:only-of-type p:only-of-type 选择作为其父的唯一 <p> 元素的每个 <p> 元素。
:only-child p:only-child 选择作为其父的唯一子元素的 <p> 元素。
:optional input:optional Select <input> elements without the "required" attribute.
:out-of-range input:out-of-range Select <input> elements with values outside the specified range.
:read-only input:read-only Select <input> elements specified with the "readonly" attribute.
:read-write input:read-write Select <input> elements without the "readonly" attribute.
:required input:required Select <input> elements specified with the "required" attribute.
:root root Select the root element of the element.
:target #news:target Select the current active #news element (click on the URL containing the anchor name).
:valid input:valid Select all <input> elements with valid values.
:visited a:visited Select all visited links.

All CSS pseudo-elements

Selector Example Example description
::after p::after Insert content after each <p> element.
::before p::before Insert content before each <p> element.
::first-letter p::first-letter Select the first letter of each <p> element.
::first-line p::first-line Select the first line of each <p> element.
::selection p::selection Select the part of the element chosen by the user.