CSS - 伪元素



CSS **伪元素** 用于为元素的特定部分设置样式。浏览网页时,您可能注意到某些段落首字母比其余字母大。这种针对元素特定部分的样式设置是使用 CSS 中的伪元素完成的。在本教程中,我们将解释所有伪元素及其功能。

目录


 

什么是伪元素?

CSS 中的伪元素用于为元素的特定部分设置样式,这些部分不属于 DOM(文档对象模型)并且不存在于 HTML 标记中。例如,段落首字母、输入元素中的占位符文本或文档中的选定部分。

  • 伪元素用双冒号 (::) 表示法表示。
  • 选择器中只能使用一个伪元素。
  • 选择器中的伪元素必须出现在所有其他组件之后。例如,**p::last-line:hover** 是无效的。
  • 伪元素可以用来添加装饰性样式,创建特殊效果,并修改已经应用了状态的元素的某些部分的外观。例如,**p:hover::last-line** 是一个有效的语句,它在将鼠标悬停在段落上时选择段落的最后一行。

语法

selector::pseudo-element { property: value; }

浏览器支持四个原始伪元素的单冒号语法,即 ::before、::after、::first-line 和 ::first-letter。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

内容插入伪元素

在 CSS 中,伪元素 **::before** 和 **::after** 用于在任何元素之前和之后插入文本内容或图像。

示例

此示例演示如何使用 CSS 在段落的开头和结尾插入文本和图像。

Open Compiler
<!DOCTYPE html> <html> <head> <style> p:before { content: "NOTE:"; font-weight: bold; } p:after { content: url(/css/images/smiley.png); } </style> </head> <body> <p> We inserted intro at start and emoji at end. </p> </body> </html>

CSS 背景伪元素

在 CSS 中,**::backdrop** 伪元素用于设置处于模态上下文中的元素的背景样式,例如在显示 **<dialog>** 元素时其后面的背景。

示例

以下示例演示了 **::backdrop** 伪元素的使用。

Open Compiler
<!DOCTYPE html> <html> <head> <style> body{ height: 200px; } dialog { padding: 20px; border: 2px solid black; border-radius: 10px; } dialog::backdrop { /* Semi-transparent black */ background-color: rgba(0, 0, 0, 0.5); } </style> </head> <body> <h3> Backdrop Example </h3> <dialog id="myDialog"> <p> This is a dialog with a styled backdrop. </p> <button id="closeButton"> Close </button> </dialog> <button id="openButton">Open Dialog</button> <script> const dialog = document.getElementById('myDialog'); const openButton = document.getElementById('openButton'); const closeButton = document.getElementById('closeButton'); openButton.addEventListener('click', () => { dialog.showModal(); }); closeButton.addEventListener('click', () => { dialog.close(); }); </script> </body> </html>

CSS 提示伪元素

在 CSS 中,伪元素 **::cue** 与 Web 视频文本轨道一起使用,为媒体元素(如 **<video>** 和 **<audio>**)的字幕或标题等文本轨道的特定部分设置样式。

示例

以下示例演示了 ::cue 伪元素的使用。

Open Compiler
<!DOCTYPE html> <html> <head> <style> video { width: 100%; } video::cue { font-size: 1rem; color: peachpuff; } </style> </head> <body> <video controls src="/css/foo.mp4"> <track default kind="captions" srclang="en" src="/css/cue-sample.vtt" /> </video> </body> </html>

CSS 首字母伪元素

在 CSS 中,**::first-letter** 伪元素用于定位任何元素(如 **div**、**段落**、**span** 等)文本内容的首字母。

示例

以下示例演示了 **::first-letter** 伪元素的使用。

Open Compiler
<!DOCTYPE html> <html> <head> <style> p::first-letter { text-transform: uppercase; font-size: 2em; color: darkred; font-style: italic; } </style> </head> <body> <p> this is a paragraph with first letter in lowercase, we used ::first-letter pseudo-element to capitalize first-letter of paragraph with a larger font size and a different color. </p> </body> </html>

CSS 首行伪元素

在 CSS 中,**::first-line** 伪元素用于定位任何元素(如 **div**、**段落**、**span** 等)文本内容的首行。

示例

以下示例演示了 **::first-line** 伪元素的使用。

Open Compiler
<!DOCTYPE html> <html> <head> <style> p::first-line { background-color: #f0f0f0; color: darkred; font-style: italic; } </style> </head> <body> <p> This is a normal paragraph with no stylings, we used ::first-line pseudo-element to only style first-line of paragraph by adding a background color, font-style and text color </p> </body> </html>

CSS 文件选择器按钮伪元素

在 CSS 中,**::file-selector-button** 伪元素用于在现代浏览器中设置文件 **input** 元素 (<input type="file">) 的按钮样式。

示例

以下示例演示了 **::file-selector-button** 伪元素的使用。

Open Compiler
<!DOCTYPE html> <html> <head> <style> body { display: block; height: 100px; } input::file-selector-button { background-image:url(/css/images/border.png); background-size: 200%; border: 2px solid black; border-radius: 8px; font-weight: 600; color: rgb(6, 1, 9); padding: 15px; transition: all 0.25s; } </style> </head> <body> <h2> Select a file </h2> <input type="file"> </body> </html>

CSS 标记伪元素

在 CSS 中,**::marker** 伪元素用于设置 **有序列表** 和 **无序列表** 的标记样式。

示例

以下示例演示了 **::marker** 伪元素的使用。

Open Compiler
<!DOCTYPE html> <html> <head> <style> ol li::marker { color: rgb(11, 38, 241); font-weight: bold; } ul li::marker { content: url('/css/images/smiley.png') } </style> </head> <body> <h2>Numbered list</h2> <ol> <li>One</li> <li>Two</li> <li>Three</li> </ol> <h2>Bulleted list</h2> <ul> <li>One</li> <li>Two</li> <li>Three</li> </ul> </body> </html>

CSS 占位符伪元素

在 CSS 中,**::placeholder** 伪元素用于设置文本 **input** 元素 (<input type="text">) 内的默认文本样式。

示例

以下示例演示了 **::placeholder** 伪元素的使用。

Open Compiler
<!DOCTYPE html> <html> <head> <style> .form { border: 2px solid black; background: lightgray; padding: 25px; display: flex; flex-direction: column; gap: 10px; } input{ padding: 10px; background-color: cornsilk; } input::placeholder { color: grey; font-style: italic; font-size: 20px; } </style> </head> <body> <div class="form"> <h2> Your Details:</h2> <input type="text" placeholder="First Name"> <input type="text" placeholder="Last Name"> <input type="text" placeholder="Address"> <input type="text" placeholder="Phone"> </div> </body> </html>

CSS 选择伪元素

在 CSS 中,**::selection** 伪元素用于设置任何元素(如 **div**、**段落**、**span** 等)内用户选中文本的样式。

示例

以下示例演示了 **::selection** 伪元素的使用。

Open Compiler
<!DOCTYPE html> <html> <head> <style> .highlight::selection { color: yellow; background: brown; } </style> </head> <body> <p class="highlight"> Select Me!!! to see the effect. </p> <p> No style applied to me. </p> </body> </html>

多个伪元素

我们还可以向选择器添加多个伪元素,查看示例。

示例

以下示例演示了多个伪元素 (::first-line 和 ::first-letter) 的用法。

Open Compiler
<!DOCTYPE html> <html> <head> <style> p::first-line { text-decoration: underline; } p::first-letter { text-transform: uppercase; font-size: 2em; color: red; } </style> </head> <body> <p> the first line of this paragraph will be underlined and first letter is uppercase, 2em and red in color, as the pseudo-element ::first-line & ::first-letter is applied on p. The other lines are not underlined. </p> </body> </html>

所有 CSS 伪元素

下表显示了 CSS 中的所有伪元素

伪元素 描述 示例
::after 添加一个作为所选元素的最后一个子元素的伪元素。
::backdrop 用于设置对话框等元素的背景样式。
::before 添加一个作为所选元素的第一个子元素的伪元素。
::cue 用于设置带有视频文本轨道的媒体中的字幕和提示的样式。
::first-letter 将样式应用于块级元素第一行的首字母。
::first-line 将样式应用于块级元素的第一行。
::file-selector-button 表示 type="file" 的 <input> 的按钮。
::marker 选择列表项的标记框。
::part() 表示具有匹配 part 属性的影子树中的元素。
::placeholder 表示 <input> 或 <textarea> 元素中的占位符文本。
::selection 将样式应用于文档的选定部分(通过单击并拖动鼠标跨越文本进行选择)。
::slotted() 表示已放置到 HTML 模板内的插槽中的元素。
::grammar-error 用于设置浏览器内置语法检查工具已识别为语法错误的文本的样式。
::spelling-error 用于设置浏览器内置拼写检查工具已识别为拼写错误的文本的样式。
广告