codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎨

HTML & CSS

32 / 59 topics
28CSS Animation29CSS Transitions30CSS Transforms31CSS Variables32CSS Pseudo-elements33CSS Pseudo-classes
Tutorials/HTML & CSS/CSS Pseudo-Elements
🎨HTML & CSS

CSS Pseudo-Elements

Updated 2026-05-15
10 min read

CSS Pseudo-Elements

Introduction

In the world of web development, CSS (Cascading Style Sheets) is a fundamental tool for styling and enhancing the presentation of HTML elements. While CSS allows you to style entire elements, sometimes you need to target specific parts or states of an element that don't have their own HTML tags. This is where pseudo-elements come into play.

Pseudo-elements are special keywords prefixed with :: that allow you to style certain parts of a selected element without adding extra markup in your HTML. They provide a powerful way to add decorative elements, such as bullets, indentation, or even content that doesn't exist in the DOM.

Concept

CSS pseudo-elements are used to add stylistic elements before or after the content of an element, or to style specific parts of an element like the first line or letter. The most commonly used pseudo-elements include:

  • ::before: Inserts generated content before the content of a selected element.
  • ::after: Inserts generated content after the content of a selected element.
  • ::first-line: Styles the first line of a block-level element.
  • ::first-letter: Styles the first letter of a block-level element.
  • ::selection: Styles the portion of an element that is currently selected by a user.

These pseudo-elements are defined using double colons (::) to distinguish them from pseudo-classes (which use single colons, like :hover).

Examples

Let's explore some practical examples to understand how pseudo-elements work.

1. Using ::before and ::after

You can use ::before and ::after to add decorative elements or content without altering the HTML structure.

<div class="box">
  This is a box.
</div>
.box {
  position: relative;
  padding: 20px;
  border: 1px solid #ccc;
}

.box::before,
.box::after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  background-color: red;
}

.box::before {
  top: -5px;
  left: -5px;
}

.box::after {
  bottom: -5px;
  right: -5px;
}

<OutputBlock>
{`<div class="box">
  This is a box.
</div>

<style>
.box {
  position: relative;
  padding: 20px;
  border: 1px solid #ccc;
}

.box::before,
.box::after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  background-color: red;
}

.box::before {
  top: -5px;
  left: -5px;
}

.box::after {
  bottom: -5px;
  right: -5px;
}
</style>`}
</OutputBlock>

In this example, two small red squares are added to the corners of the `.box` element using `::before` and `::after`.

### 2. Styling the First Line and Letter

Pseudo-elements like `::first-line` and `::first-letter` can be used to style specific parts of text.

```html
<p class="text">
  This is a paragraph where the first line and letter are styled using pseudo-elements.
</p>
.text {
  font-size: 16px;
  line-height: 1.5;
}

.text::first-line {
  color: blue;
  font-weight: bold;
}

.text::first-letter {
  font-size: 24px;
  color: green;
}

<OutputBlock>
{`<p class="text">
  This is a paragraph where the first line and letter are styled using pseudo-elements.
</p>

<style>
.text {
  font-size: 16px;
  line-height: 1.5;
}

.text::first-line {
  color: blue;
  font-weight: bold;
}

.text::first-letter {
  font-size: 24px;
  color: green;
}
</style>`}
</OutputBlock>

Here, the first line of the paragraph is colored blue and made bold, while the first letter is enlarged and colored green.

### 3. Styling Selected Text

The `::selection` pseudo-element allows you to style text that a user has selected on the page.

```html
<p class="selectable">
  Select this text to see the custom selection styling.
</p>
.selectable {
  font-size: 16px;
}

.selectable::selection {
  background-color: yellow;
  color: black;
}

<OutputBlock>
{`<p class="selectable">
  Select this text to see the custom selection styling.
</p>

<style>
.selectable {
  font-size: 16px;
}

.selectable::selection {
  background-color: yellow;
  color: black;
}
</style>`}
</OutputBlock>

When you select text within the `.selectable` paragraph, it will have a yellow background and black text.

## What's Next?

Now that you've learned about pseudo-elements, you might want to explore how to use them effectively in your projects. In the next section, we'll dive into HTML comments, which are another useful feature for adding notes or temporarily removing parts of your code without affecting the final output.

Stay tuned for more advanced CSS techniques and tips!

PreviousCSS VariablesNext CSS Pseudo-classes

Recommended Gear

CSS VariablesCSS Pseudo-classes