CSS Pseudo-elements

What are Pseudo-Elements?

A CSS pseudo-element is a keyword added to a selector that lets you style a specific part of the selected element(s). For example, ::first-line can be used to change the font of the first line of a paragraph.

CSS pseudo-elements are used to add special effects to some selectors. You do not need to use JavaScript or any other script to use those effects.

Example -

The most commonly used pseudo-elements are as follows −

Value Description
:first-line Use this element to add special styles to the first line of the text in a selector.
:first-letter Use this element to add special style to the first letter of the text in a selector.
:before Use this element to insert some content before an element.
:after Use this element to insert some content after an element.

The ::first-line Pseudo-element

The following example demonstrates how to use the :first-line element to add special effects to the first line of elements in the document.

The ::first-letter Pseudo-element

The following example demonstrates how to use the :first-letter element to add special effects to the first letter of elements in the document.

Pseudo-elements and CSS Classes

Multiple Pseudo-elements

Several pseudo-elements can also be combined.In the following example, the first letter of a paragraph will be red, in an xx-large font size. The rest of the first line will be blue, and in small-caps. The rest of the paragraph will be the default font size and color:

CSS - The ::before Pseudo-element

The following example demonstrates how to use the :before element to add some content before any element.

CSS - The ::after Pseudo-element

The following example demonstrates how to use the :after element to add some content after any element.

CSS - The ::selection Pseudo-element

All CSS Pseudo Elements

SelectorExampleDescription
::afterp::afterInsert something after the content of each <p> element
::beforep::beforeInsert something before the content of each <p> element
p::first-letterp::first-letterSelects the first letter of each <p> element
::first-linep::first-lineSelects the first line of each <p> element
::selectionp::selectionSelects the portion of an element that is selected by a user

All CSS Pseudo Classes

SelectorExampleDescription
:activea:activeSelects the active link
:checkedinput:checkedSelects every checked <input> element
:disabledinput:disabledSelects every disabled <input> element
:emptyp:emptySelects every <p> element that has no children
:enabledinput:enabledSelects every enabled <input> element
:first-childp:first-childSelects every <p> elements that is the first child of its parent
:first-of-typep:first-of-typeSelects every <p> element that is the first <p> element of its parent
:focusinput:focusSelects the <input> element that has focus
:hovera:hoverSelects links on mouse over
:in-rangeinput:in-rangeSelects <input> elements with a value within a specified range
:invalidinput:invalidSelects 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-childp:last-childSelects every <p> elements that is the last child of its parent
:last-of-typep:last-of-typeSelects every <p> element that is the last <p> element of its parent
:linka:linkSelects 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

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-typep:only-of-typeSelects every <p> element that is the only <p> element of its parent
:only-childp:only-childSelects every <p> element that is the only child of its parent
:optionalinput:optionalSelects <input> elements with no "required" attribute
:out-of-rangeinput:out-of-rangeSelects <input> elements with a value outside a specified range
:read-onlyinput:read-onlySelects <input> elements with a "readonly" attribute specified
:read-writeinput:read-writeSelects <input> elements with no "readonly" attribute
:requiredinput:requiredSelects <input> elements with a "required" attribute specified
:rootrootSelects the document's root element
:target#news:targetSelects the current active #news element (clicked on a URL containing that anchor name)
:validinput:validSelects all <input> elements with a valid value
:visiteda:visitedSelects all visited links