Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS Pseudo-elements

CSS Pseudo-elements


A CSS pseudo-element is a special selector that allows you to style specific parts of a page element without changing the HTML structure.

CSS pseudo-elements are used to add special effects to some selectors.

Common CSS pseudo-elements include ::before, ::after, ::first-line, ::first-letter, and so on.


Syntax

Syntax of pseudo-elements:

selector::pseudo-element {

    property: value;

}

CSS classes can also use pseudo-elements:

selector.class::pseudo-element {

    property: value;

}


:first-line pseudo-element

The "first-line" pseudo-element is used to set a special style for the first line of text.

In the following example, the browser will format the first line of text in the p element according to the styles in the "first-line" pseudo-element:

Example

p

:first-line

{

color:

#ff0000

;

font-variant:

small-caps

;

}

Try it »

Note:The "first-line" pseudo-element can only be used with block-level elements.

Note: The following properties can be applied to the "first-line" pseudo-element:


:first-letter pseudo-element

The "first-letter" pseudo-element is used to set a special style for the first letter of text:

Example

p

:first-letter

{

color:

#ff0000

;

font-size:

xx-large

;

}

Try it »

Note: The "first-letter" pseudo-element can only be used with block-level elements.

Note: The following properties can be applied to the "first-letter" pseudo-element: 


Pseudo-elements and CSS classes

Pseudo-elements can be combined with CSS classes: 



p.article:first-letter {color:#ff0000;}



文章段落

The example above makes the first letter of all paragraphs with class article red.


Multiple pseudo-elements

Multiple pseudo-elements can be used in combination.

In the following example, the first letter of the paragraph will be displayed in red with a font size of xx-large. The rest of the text in the first line will be blue and displayed in small capitals.

The remaining text in the paragraph will be displayed with the default font size and color:

Example

p

:first-letter

{

color:

#ff0000

;

font-size:

xx-large

;

}

p

:first-line

{

color:

#0000ff

;

font-variant:

small-caps

;

}

Try it »


CSS - :before pseudo-element

The ":before" pseudo-element can insert new content before the content of an element.

The following example inserts an image before each

element:

Example

h1

:before

{

content:

url

(

smiley

.

gif

)

;

}

Try it »


CSS - :after pseudo-element

The ":after" pseudo-element can insert new content after the content of an element.

The following example inserts an image before each

insert an image after the element:

Example

h1

:after

{

content:

url

(

smiley

.

gif

)

;

}

Try it »


All CSS pseudo-classes/elements

Selector Example Example Description
:link a:link Selects all unvisited links
:visited a:visited Selects all visited links
:active a:active Selects the currently active link
:hover a:hover the state when the mouse is over a link
:focus input:focus Selects an element that has focus after input
:first-letter p:first-letter Select each

element.

:first-line p:first-line Select each

The first line of an element

:first-child p:first-child The selector matches the

Element

:before p:before Insert content after each

Inserts content before an element

:after p:after Insert content after each

Inserts content after an element

:lang(language) p:lang(it) is

element whose lang attribute begins with a specified value