CSS Pseudo-element

A CSS pseudo-element is used to style a specific part of a selected element. Pseudo-elements allow you to style the content of an element that is not actually in the HTML, such as the first letter or first line of a paragraph. Pseudo-elements are denoted by two colons (::) before the element name. There are different types of pseudo-elements that you can use to style elements in various ways.

Mandatory reading
To understand all the cases, see the CSS Selector reference

::before and ::after

The ::before and ::after pseudo-elements are used to insert content before or after the content of an element. These pseudo-elements are commonly used to add decorative elements or text to a webpage without modifying the HTML content.

Here's an example of how you can use the ::before pseudo-element to add a custom bullet point before each list item:


ul li::before {
  content: "\2022";
  color: red;
  margin-right: 10px;
}
Result:
  • List Item
  • List Item
  • List Item

By using the content property, you can add the bullet point symbol before each list item without modifying the actual HTML content.

::first-line and ::first-letter

The ::first-line and ::first-letter pseudo-elements are used to style the first line or first letter of a block of text. These pseudo-elements are commonly used to create drop caps or highlight the first line of a paragraph.

Here's an example of how you can use the ::first-letter pseudo-element to style the first letter of a paragraph:


p::first-letter {
  font-size: 2em;
  color: blue;
  float: left;
  margin-right: 5px;
}
Result:

Style applied on the first letter

By styling the first letter of a paragraph using the ::first-letter pseudo-element, you can create a visually appealing drop cap effect.

This is just a basic overview of CSS pseudo-elements. There are many more pseudo-elements that you can explore to enhance the styling of your webpages.