CSS 伪元素用于设置元素的指定部分的样式。
例如,它可用于:
伪元素的语法:
selector::pseudo-element {
property: value;
}
这个::first-line
伪元素用于向文本的第一行添加特殊样式。
以下示例格式化所有 <p> 元素中文本的第一行:
笔记:这个::first-line
伪元素只能应用于块级元素。
以下属性适用于::first-line
伪元素:
注意双冒号符号 -::first-line
相对:first-line
双冒号取代了 CSS3 中伪元素的单冒号表示法。这是 W3C 试图区分伪类和伪元素。
单冒号语法用于 CSS2 和 CSS1 中的伪类和伪元素。
为了向后兼容,CSS2 和 CSS1 伪元素可以接受单冒号语法。
这个::first-letter
伪元素用于向文本的第一个字母添加特殊样式。
以下示例格式化所有 <p> 元素中文本的第一个字母:
笔记:这个::first-letter
伪元素只能应用于块级元素。
以下属性适用于 ::first-letter 伪元素:
伪元素可以与 HTML 类组合:
上面的示例将以红色且更大的尺寸显示 class="intro" 的段落的第一个字母。
也可以组合多个伪元素。
在以下示例中,段落的第一个字母将为红色,字体大小为 xx-large。第一行的其余部分将是蓝色的,并且是小写字母。该段落的其余部分将是默认的字体大小和颜色:
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
p::first-line {
color: #0000ff;
font-variant: small-caps;
}
亲自试一试 »
这个::before
伪元素可用于在元素内容之前插入一些内容。
以下示例在每个 <h1> 元素的内容之前插入图片:
这个::after
伪元素可用于在元素内容之后插入一些内容。
以下示例在每个 <h1> 元素的内容之后插入图片:
这个::marker
伪元素选择列表项的标记。
以下示例设置列表项标记的样式:
这个::selection
伪元素匹配用户选择的元素部分。
以下 CSS 属性可应用于::selection
:color
,background
,cursor
, 和outline
。
以下示例使所选文本在黄色背景上变为红色:
Selector | Example | Example description |
---|---|---|
::after | p::after | Insert something after the content of each <p> element |
::before | p::before | Insert something before the content of each <p> element |
::first-letter | p::first-letter | Selects the first letter of each <p> element |
::first-line | p::first-line | Selects the first line of each <p> element |
::marker | ::marker | Selects the markers of list items |
::selection | p::selection | Selects the portion of an element that is selected by a user |
Selector | Example | Example description |
---|---|---|
:active | a:active | Selects the active link |
:checked | input:checked | Selects every checked <input> element |
:disabled | input:disabled | Selects every disabled <input> element |
:empty | p:empty | Selects every <p> element that has no children |
:enabled | input:enabled | Selects every enabled <input> element |
:first-child | p:first-child | Selects every <p> elements that is the first child of its parent |
:first-of-type | p:first-of-type | Selects every <p> element that is the first <p> element of its parent |
:focus | input:focus | Selects the <input> element that has focus |
:hover | a:hover | Selects links on mouse over |
:in-range | input:in-range | Selects <input> elements with a value within a specified range |
:invalid | input:invalid | Selects all <input> elements with an invalid value |
:lang(language) | p:lang(it) | Selects every <p> element with a lang attribute value starting with "it" |
:last-child | p:last-child | Selects every <p> elements that is the last child of its parent |
:last-of-type | p:last-of-type | Selects every <p> element that is the last <p> element of its parent |
:link | a:link | Selects all unvisited links |
:not(selector) | :not(p) | Selects every element that is not a <p> element |
:nth-child(n) | p:nth-child(2) | Selects every <p> element that is the second child of its parent |
:nth-last-child(n) | p:nth-last-child(2) | Selects every <p> element that is the second child of its parent, counting from the last child |
:nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> element that is the second <p> element of its parent, counting from the last child |
:nth-of-type(n) | p:nth-of-type(2) | Selects every <p> element that is the second <p> element of its parent |
:only-of-type | p:only-of-type | Selects every <p> element that is the only <p> element of its parent |
:only-child | p:only-child | Selects every <p> element that is the only child of its parent |
:optional | input:optional | Selects <input> elements with no "required" attribute |
:out-of-range | input:out-of-range | Selects <input> elements with a value outside a specified range |
:read-only | input:read-only | Selects <input> elements with a "readonly" attribute specified |
:read-write | input:read-write | Selects <input> elements with no "readonly" attribute |
:required | input:required | Selects <input> elements with a "required" attribute specified |
:root | root | Selects the document's root element |
:target | #news:target | Selects the current active #news element (clicked on a URL containing that anchor name) |
:valid | input:valid | Selects all <input> elements with a valid value |
:visited | a:visited | Selects all visited links |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!