📜  CSS-伪元素

📅  最后修改于: 2020-10-26 04:51:04             🧑  作者: Mango


CSS伪元素用于向某些选择器添加特殊效果。您无需使用JavaScript或任何其他脚本即可使用这些效果。伪元素的简单语法如下-

selector:pseudo-element {property: value}

CSS类也可以与伪元素一起使用-

selector.class:pseudo-element {property: value}

最常用的伪元素如下-

Sr.No. Value & Description
1

:first-line

Use this element to add special styles to the first line of the text in a selector.

2

:first-letter

Use this element to add special style to the first letter of the text in a selector.

3

:before

Use this element to insert some content before an element.

4

:after

Use this element to insert some content after an element.

:第一行伪元素

以下示例演示了如何使用:first-line元素向文档的第一行元素添加特殊效果。


   

   
      

This line would not have any underline because this belongs to nline class.

The first line of this paragraph will be underlined as defined in the CSS rule above. Rest of the lines in this paragraph will remain normal. This example shows how to use :first-line pseduo element to give effect to the first line of any HTML element.

它将产生以下链接-

:first-letter伪元素

下面的示例演示如何使用:first-letter元素向文档中的元素的首字母添加特殊效果。


   

   
      

First character of this paragraph will be normal and will have font size 10 px;

The first character of this paragraph will be 5em big as defined in the CSS rule above. Rest of the characters in this paragraph will remain normal. This example shows how to use :first-letter pseduo element to give effect to the first characters of any HTML element.

它将产生以下黑色链接-

:before伪元素

下面的示例演示如何使用:before元素在任何元素之前添加一些内容。


   

   
      

This line will be preceded by a bullet.

This line will be preceded by a bullet.

This line will be preceded by a bullet.

它将产生以下黑色链接-

:after伪元素

下面的示例演示如何使用:after元素在任何元素之后添加一些内容。


   

   
      

This line will be succeeded by a bullet.

This line will be succeeded by a bullet.

This line will be succeeded by a bullet.

它将产生以下黑色链接-