C

CSS Tutorial

Clean • Professional

CSS Pseudo-elements

2 minute

CSS Pseudo-elements

While pseudo-classes let us style elements based on their state, pseudo-elements allow us to style specific parts of an element or even insert extra content without changing the HTML.

Pseudo-elements are powerful because they help us:

  • Add decorative content without extra HTML.
  • Style specific parts of text (like first letter, selection, etc.).
  • Improve readability, accessibility, and UI design.
  • Keep HTML clean while making CSS more expressive.

Common CSS Pseudo-elements

learn code with durgesh images

::before – Insert Content Before an Element

The ::before pseudo-element is used to insert content before an element’s content. This is commonly used for adding icons, quotes, or decorations without extra HTML.

h1::before {
  content: "★ ";
  color: gold;
}

::after – Insert Content After an Element

The ::after pseudo-element inserts content after an element’s content. Often used for adding visual indicators, decorative lines, or helper text.

a::after {
  content: " 🔗";
  color: gray;
}

::marker – Style List Markers

The ::marker pseudo-element lets you style the bullet points or numbers in lists (<ul> and <ol>). This gives designers full control over how list markers appear.

li::marker {
  color: red;
  font-size: 18px;
}

::selection – Style Selected Text

The ::selection pseudo-element allows you to style the portion of text a user highlights with their mouse or keyboard.

p::selection {
  background: yellow;
  color: black;
}

Flow of CSS Pseudo-elements

learn code with durgesh images

CSS Pseudo-elements with Examples

Pseudo-elementExampleDescription
::afterp::after, div::afterInserts content after the specified element’s content.
::backdropdialog::backdropStyles the background (viewbox) behind a <dialog> or popover.
::beforep::before, div::beforeInserts content before the specified element’s content.
::file-selector-button::file-selector-buttonTargets the button of an <input type="file">.
::first-letterp::first-letterStyles the first letter of a block of text.
::first-linep::first-lineStyles the first line of a block of text.
::grammar-error::grammar-errorHighlights text flagged as grammatically incorrect by the browser.
::highlight()::highlight(custom-name)Allows custom text highlights using the ::highlight() API.
::markerli::markerStyles the list markers (bullets or numbers) in lists.
::placeholderinput::placeholder, textarea::placeholderStyles placeholder text inside form fields.
::selection::selectionStyles text selected by the user (highlighted).
::spelling-error::spelling-errorHighlights text flagged as misspelled by the browser.
::view-transition::view-transitionRepresents the root overlay container for view transitions.
::view-transition-group::view-transition-groupRepresents a single group of view transition snapshots.
::view-transition-image-pair::view-transition-image-pairContainer for an image pair in view transitions (old + new).
::view-transition-new::view-transition-newRepresents the new state of a view transition.
::view-transition-old::view-transition-oldRepresents the old state of a view transition.

Article 0 of 0