Mastering CSS Pseudo Classes and Pseudo Elements: A Comprehensive Guide

Wasiu Akindoyin
Web 3 Digitals
Published in
6 min readApr 1, 2024

· Introduction
· What are Pseudo-classes?
1. Hover
2. nth-child(n)
3. Focus
4. First-Child
· What are Pseudo-elements?
1. Before
2. After
3. Selection
· Differences between Pseudo-classes and Pseudo-elements
1. Target Elements:
2. Syntax:
3. Examples:
4. Purpose:
· Conclusion

Introduction

When it comes to designing web pages, Cascading Style Sheets (CSS) offer a wide range of tools to achieve the intended visual appearance. Pseudo-classes and pseudo-elements are included in these tools, enabling developers to select specific elements based on different criteria.

This article will explain the concepts of pseudo-classes and pseudo-elements, their differences, and how they can be utilized to enhance the design of web pages.

What are Pseudo-classes?

Pseudo-classes are keywords added to selectors to style elements based on their state or position in the document structure. They enable developers to apply styles to elements that are not solely represented by the HTML markup. Pseudo-classes are indicated by a colon ( :) in CSS selectors. Some examples of pseudo-classes are as follows:

1. Hover

The :hover pseudo-class activates when the user hovers their mouse over an element, like a button. This creates a visual sign, indicating that the element is interactive and can be clicked or interacted with.

Syntax:

element:hover {
/* styles */
}

The HTML element tag mentioned above is the one you intend to focus on when it is hovered over (e.g., a button, div). The :hover pseudo-class defines the styles that will be activated when the mouse hovers over the element.

button:hover {
background-color: blue;
}

In the given code example, when a user moves their mouse over any button element, the background color of the button will be updated to the specified blue color.

2. nth-child(n)

This selector in CSS allows you to select elements based on their position within a parent element.

Syntax:

element:nth-child(n) {
/* styles */
}

The element keyword in the provided code refers to the HTML element you wish to target (e.g., p, div, li). The :nth-child(n) pseudo-class enables you to choose child elements based on their position. The n can be a positive integer that selects the nth child element. For instance, :nth-child(2) selects the second child element.

Code Example:

ul li:nth-child(2) {
background-color: lightblue;
}

The provided code example targets the second li item within each ul and sets a background color of light blue for it.

3. Focus

The :focus pseudo-class in CSS is used for styling elements that are presently in focus. This can happen when a user clicks on an input field or when they move to it using the keyboard.

Syntax:

element:focus {
/* styles */
}

The element keyword mentioned in the syntax above refers to the HTML element that you wish to select when it is in focus (e.g., input, button, a). The :focus pseudo-class defines the styles that will be activated when the element is in focus.

Code Example:

input:focus {
border-color: blue;
}

Whenever the input element in the code example is clicked on and receives focus, its border color will change to the specified blue color.

4. First-Child

The :first-child pseudo-class in CSS allows you to target and style the first immediate child element within its parent element.

Syntax:

parent-element:first-child {
/* styles */
}

The parent-element is the HTML element that serves as the parent container for other elements. It can be used to select specific elements, such as ul, li, or any other element that can contain child elements. The :first-child pseudo-class is used to target the first child element directly within the specified parent element.

Code Example:

ul li:first-child {
background-color: lightblue;
}

The provided code example selects the first li element in each ul (unordered list) and adds a light blue background color to it.

What are Pseudo-elements?

Pseudo-elements, on the other hand, allow developers to style specific parts of an element. They are used to add decorative effects or content to elements with pseudo-elements without modifying the HTML markup. Pseudo-elements are preceded by two colons (::) in CSS selectors. Some examples of pseudo-elements are as follows:

1. Before

The ::before pseudo-element in CSS gives you the ability to include content before the main content of an element. This feature allows you to add decorative elements, improve accessibility, or produce distinctive visual effects.

Syntax:

element::before {
content: "your content";
/* other styles */
}

The element keyword in the syntax provided indicates the HTML element where you wish to insert content before its current content. You can select different elements such as h1, p, buttons, and so on. The ::before pseudo-element indicates that you want to apply styles to the content before the actual content of the element.

The content: "your content"; property defines the content you want to insert. It can be some text or an image URL enclosed in quotes (e.g., content: url("image.png");).

Code Example:

h1::before {
content: "Welcome, ";
font-weight: bold;
}

The provided code example adds the text "Welcome, " in front of the text within every h1 element on your website. The added text is formatted with a bold font weight.

2. After

The ::after pseudo-element in CSS, similar to its counterpart ::before, it allows you to insert content after the actual content of an element. This opens up various possibilities for adding visual enhancements, improving accessibility, or crafting unique design elements.

Syntax:

element::after {
content: "your content";
/* other styles */
}

The element keyword in the provided code refers to the HTML element to which you intend to add the content after its current content. You have the flexibility to select different elements such as p, div, buttons, and more.

The ::after pseudo-element is used to style the content that comes after the actual content of an element. By using the content: "your content"; property, you can specify the content that you want to insert, which can be text or an image URL enclosed in quotes (e.g., content: url("image.png"); ).

Code Example:

p::after {
content: " - See more";
font-style: italic;
color: blue;
}

The provided code example adds the text " — See more" after the content of every p element on your webpage. Additionally, the inserted text is formatted with an italic font style and a blue color.

3. Selection

The ::selection pseudo-element in CSS enables the customization of the text that a user has currently highlighted. It is often used to enhance the visual presentation of selected text on a website, thereby enhancing readability and the overall user experience.

Syntax:

::selection {
/* styles */
}

The ::selection pseudo-element is used to specifically style the text that has been selected within any element.

Code Example:

p::selection {
color: white;
background-color: red;
}

The provided code example modifies the text color of the selected content within all the p tags to white, while also adding a red background color behind the selection.

Differences between Pseudo-classes and Pseudo-elements

Pseudo-classes and pseudo-elements are used in CSS for applying styles to elements depending on specific conditions or for adding decorative elements to elements without altering the HTML. Nevertheless, there are distinct differences between the two, which include:

1. Target Elements:

  • Pseudo-classes are used to select elements based on their state or position within the document structure. An example of this is the :hover pseudo-class, which selects an element when the user hovers over it.
  • Pseudo-elements target specific parts of an element or create virtual elements that do not exist in the HTML markup. For example, ::before creates a virtual element before the content of an element.

2. Syntax:

  • Pseudo-classes in CSS selectors are indicated by a single colon (:). For instance, :hover.
  • Pseudo-elements in CSS selectors are indicated by two colons (::). For instance, :::before.

3. Examples:

Pseudo-classes: :hover, :active, :focus, :nth-child(n).

Pseudo-elements: ::before, ::after, ::first-line, ::selection.

4. Purpose:

  • Pseudo-classes are used to style elements based on user interaction or document structure. For example, :hover is used to change the style of an element when the user hovers over it.
  • Pseudo-elements are used to add decorative elements or content to elements without modifying the HTML. For example, ::before is used to insert content before the content of an element.

Conclusion

In summary, pseudo-classes and pseudo-elements are effective tools in CSS that enable developers to design engaging and visually attractive web pages. Mastering their usage can elevate your CSS proficiency and improve the overall user experience of your websites. Keep in mind, that practice is essential! Explore various pseudo-classes and pseudo-elements to fully utilize their capabilities and breathe life into your web designs.

--

--

Wasiu Akindoyin
Web 3 Digitals

Front-end Developer | Technical Writer | Simplifying complex software concepts through code and real life analogies.