Skip to content
Latest: JSX in React explained clearly without any framework

Pseudo-Selectors in CSS – What Is a CSS Pseudo-Selector?

A CSS pseudo-selector does the following:

  • It selects a specific part of an HTML element
  • It selects an HTML element only when it is in a specific state

The two (2) types of pseudo-selectors are:

  • Pseudo-classes
  • Pseudo-elements

Let’s discuss the two selectors below.

What Is a CSS Pseudo-Class Selector

A CSS pseudo-class selects HTML elements when they are in a specific state—for instance, when an element is in the hover state.

Syntax of a pseudo-class selector

A pseudo-class selector consists of a colon (:) and the pseudo-state’s name you wish to target. Here’s the syntax:

:pseudo-state-name { style declarations }

Example: Apply DeepPink to the h1 heading when users hover over it

The pseudo-class selector (:hover) below selects an <h1> element when it is in the hover state (when users move their mouse pointer over it).

h1:hover {
color: DeepPink;
}

So, suppose the snippet below is the HTML document for the above CSS ruleset. In that case, DeepPink will get applied to the <h1> element when users point their mouse over it.

<h1>Hello there! 👋 I am Heading 1</h1>
<p>First paragraph immediately after heading 1</p>
<h2>Heading 2 is my name</h2>
<p>Second paragraph immediately after heading 2</p>
<p>Third paragraph</p>
<h2>Heading 2 is my name</h2>
<p>Fourth paragraph</p>

Try it on StackBlitz

Note that it is not compulsory to precede a pseudo-class with an element selector. For instance, here is a valid pseudo-class CSS ruleset:

:hover {
color: DeepPink;
}

Try it on StackBlitz

But typically, you want to add a selector before your pseudo-class to indicate the exact element you wish to target.

Common CSS pseudo-classes

Below are some of the widely used pseudo-classes.

Pseudo-classesFunction
:root

Selects the root element of a document.

:link

Selects elements that users have not visited.

:visited

Selects elements that users have visited.

:hover

Selects elements when users move their mouse pointer over them.

:active

Selects elements when users activate them.

:focus

Selects elements when users focus on them.

:empty

Selects elements that have no children.

:only-childSelects an element that has no siblings. In other words, it only matches a node if it is the only child element of its parent.

What Is a CSS Pseudo-Element Selector?

A CSS pseudo-element selects a specific part of an element.

Syntax of a pseudo-element selector

A pseudo-element selector consists of a double colon (::) and the pseudo-element’s name you wish to target. Here’s the syntax:

::pseudo-element-name { style declarations }

Example: Apply DeepPink and an xxx-large size to an h1 heading’s first letter

The pseudo-element selector (::first-letter) below selects an <h1> element’s first letter.

h1::first-letter {
color: DeepPink;
font-size: xxx-large;
}

So, suppose the snippet below is the HTML document for the above CSS ruleset. In that case, DeepPink and xxx-large font size will get applied to the <h1> element’s first letter.

<h1>Hello there! 👋 I am Heading 1</h1>
<p>First paragraph immediately after heading 1</p>
<h2>Heading 2 is my name</h2>
<p>Second paragraph immediately after heading 2</p>
<p>Third paragraph</p>
<h2>Heading 2 is my name</h2>
<p>Fourth paragraph</p>

Try it on StackBlitz

Common pseudo-elements

Below are some of the widely used pseudo-elements.

Pseudo-elementsFunction
::after

Selects an element’s last item and inserts some pseudo-content after it. In other words, it adds a pseudo-content as the selected element’s last child.

::before

Selects an element’s first item and inserts some pseudo-content before it. In other words, it adds a pseudo-content as the selected element’s first child.

::first-letter

Selects a block-level element’s first letter.

::first-line

Selects a block-level element’s first line.

::maker

Selects an li element’s bullet or number maker.

::selection

Selects the portion of a document that users highlight—for instance when a user clicks and drags the mouse pointer across a page’s text.