Back to Content

Pseudo-elements

files/en-us/web/css/reference/selectors/pseudo-elements/index.md

latest13.1 KB
Original Source

A CSS pseudo-element is a keyword added to a selector that lets you style a specific part of the selected element(s).

[!NOTE] This page is an index of all pseudo-elements in CSS. The CSS pseudo-elements page introduces the module that defines some, but not all, of these pseudo-elements.

Syntax

css
selector::pseudo-element {
  property: value;
}

For example, {{CSSxRef("::first-line")}} can be used to change the font of the first line of a paragraph.

css
/* The first line of every <p> element. */
p::first-line {
  color: blue;
  text-transform: uppercase;
}

Double colons (::) are used for pseudo-elements. This distinguishes pseudo-elements from pseudo-classes that use a single colon (:) in their notation. Note, browsers support single colon syntax for the original four pseudo-elements: ::before, ::after, ::first-line, and ::first-letter.

Pseudo-elements do not exist independently. The element of which a pseudo-element is a part is called its originating element. A pseudo-element must appear after all the other components in the complex or compound selector. The last element in the selector is the originating element of the pseudo-element. For example, you can select a paragraph's first line using p::first-line but not the first-line's children. So p::first-line > * is invalid.

A pseudo-element can be selected based on the current state of the originating element. For example, p:hover::first-line selects the first line (pseudo-element) of a paragraph when the paragraph itself is being hovered (pseudo-class).

[!NOTE] When a selector list contains an invalid selector, the entire style block is ignored.

Typographic pseudo-elements

  • {{CSSxRef("::first-line")}}
    • : The first line-box of the originating element.
  • {{CSSxRef("::first-letter")}}
    • : The first letter, number, or symbol character on the first line of its originating element.
  • {{CSSxRef("::cue")}}

Highlight pseudo-elements

Selects document sections based on content and document status, enabling those areas to be styled differently to indicate that status to the user.

  • {{CSSxRef("::grammar-error")}}
    • : A portion of text that the browser thinks is grammatically incorrect.
  • {{CSSxRef("::highlight()")}}
  • {{CSSxRef("::search-text")}}
    • : Search results identified by the user agent's "Find" or "Find in page" text search feature.
  • {{CSSxRef("::selection")}}
    • : The portion of a document that has been selected.
  • {{CSSxRef("::spelling-error")}}
    • : A portion of text that the browser thinks is misspelled.
  • {{CSSxRef("::target-text")}}
    • : The document's target element. The target element is identified using the URL's fragment identifier.

Tree-Abiding pseudo-elements

These pseudo-elements behave like regular elements, fitting seamlessly within the box model. They act as a child element that can be styled directly within the originating element hierarchy.

  • {{CSSxRef("::before")}}
    • : Creates a pseudo-element that is the first child of the selected element.
  • {{CSSxRef("::after")}}
    • : Creates a pseudo-element that is the last child of the selected element.
  • {{CSSxRef("::column")}}
  • {{CSSxRef("::marker")}}
    • : The automatically generated marker box of a list item.
  • {{CSSxRef("::backdrop")}}
    • : The backdrop of the originating element rendered in the top layer.
  • {{CSSxRef("::scroll-button()")}}
    • : Creates a button that can control the scrolling of the {{glossary("scroll container")}} to which it is applied.
  • {{CSSxRef("::scroll-marker")}}
    • : Creates a pseudo-element that is a scroll marker — a scroll target button for its originating element nested in a scroll-marker group.
  • {{CSSxRef("::scroll-marker-group")}}
    • : Generates a container before or after a scroll container to contain the {{cssxref("::scroll-marker")}} pseudo-elements generated on the element or its descendants.

Element-backed pseudo-elements

These pseudo-elements are real elements that are not otherwise selectable.

  • {{CSSxRef("::details-content")}}
    • : The expandable/collapsible contents of a {{HTMLElement("details")}} element.
  • {{cssxref("::part()")}}
  • {{cssxref("::slotted()")}}
    • : Any element placed into a slot inside an HTML template.

The pseudo-elements are related to form controls.

  • {{CSSxRef("::checkmark")}}
    • : Targets the checkmark placed inside the currently-selected <option> element of a customizable select element to provide a visual indication of which one is selected.
  • {{CSSxRef("::file-selector-button")}}
    • : The button of an {{HTMLElement("input") }} of type="file".
  • {{CSSxRef("::picker()")}}
  • {{CSSxRef("::picker-icon")}}
    • : The picker icon inside form controls that have an icon associated with them. In the case of a customizable select element, it selects the arrow that points down when the select is closed.
  • {{CSSxRef("::placeholder")}}
    • : The placeholder text in an input field.

Alphabetical index

Pseudo-elements defined by a set of CSS specifications include the following:

A

  • {{CSSxRef("::after")}}

B

  • {{CSSxRef("::backdrop")}}
  • {{CSSxRef("::before")}}

C

  • {{CSSxRef("::column")}}
  • {{CSSxRef("::checkmark")}}
  • {{CSSxRef("::cue")}} (and {{cssxref("::cue()")}})

D

  • {{CSSxRef("::details-content")}}

F

  • {{CSSxRef("::file-selector-button")}}
  • {{CSSxRef("::first-letter")}}
  • {{CSSxRef("::first-line")}}

G

  • {{CSSxRef("::grammar-error")}}

H

  • {{CSSxRef("::highlight()")}}

M

  • {{CSSxRef("::marker")}}

P

  • {{cssxref("::part()")}}
  • {{CSSxRef("::picker()")}}
  • {{CSSxRef("::picker-icon")}}
  • {{CSSxRef("::placeholder")}}

S

  • {{CSSxRef("::scroll-button()")}}
  • {{CSSxRef("::scroll-marker")}}
  • {{CSSxRef("::scroll-marker-group")}}
  • {{CSSxRef("::selection")}}
  • {{cssxref("::slotted()")}}
  • {{CSSxRef("::spelling-error")}}

T

  • {{CSSxRef("::target-text")}}

V

  • {{cssxref("::view-transition")}}
  • {{cssxref("::view-transition-image-pair()")}}
  • {{cssxref("::view-transition-group()")}}
  • {{cssxref("::view-transition-new()")}}
  • {{cssxref("::view-transition-old()")}}

Non-standard pseudo-elements

Non-standard vendor-prefixed pseudo-elements include:

-moz- prefix

  • {{CSSxRef("::-moz-color-swatch")}}
  • {{CSSxRef("::-moz-focus-inner")}}
  • {{CSSxRef("::-moz-list-bullet")}}
  • {{CSSxRef("::-moz-list-number")}}
  • {{CSSxRef("::-moz-meter-bar")}}
  • {{CSSxRef("::-moz-progress-bar")}}
  • {{CSSxRef("::-moz-range-progress")}}
  • {{CSSxRef("::-moz-range-thumb")}}
  • {{CSSxRef("::-moz-range-track")}}

-webkit- prefix

  • {{CSSxRef("::-webkit-inner-spin-button")}}
  • {{CSSxRef("::-webkit-meter-bar")}}
  • {{CSSxRef("::-webkit-meter-even-less-good-value")}}
  • {{CSSxRef("::-webkit-meter-inner-element")}}
  • {{CSSxRef("::-webkit-meter-optimum-value")}}
  • {{CSSxRef("::-webkit-meter-suboptimum-value")}}
  • {{CSSxRef("::-webkit-progress-bar")}}
  • {{CSSxRef("::-webkit-progress-inner-element")}}
  • {{CSSxRef("::-webkit-progress-value")}}
  • {{CSSxRef("::-webkit-scrollbar")}}
  • {{CSSxRef("::-webkit-search-cancel-button")}}
  • {{CSSxRef("::-webkit-search-results-button")}}
  • {{CSSxRef("::-webkit-slider-runnable-track")}}
  • {{CSSxRef("::-webkit-slider-thumb")}}

Nesting pseudo-elements

You can chain some pseudo-element selectors together to style pseudo-elements nested inside other pseudo-elements. The following nested pseudo-element combinations are supported:

  • {{CSSxRef("::after")}}
    • ::after::marker: Selects the {{CSSxRef("::marker")}} pseudo-element of an ::after pseudo-element, when ::after is styled as a list item, with {{CSSxRef("display", "display: list-item")}}.
  • {{CSSxRef("::before")}}
    • ::before::marker: Selects the {{CSSxRef("::marker")}} pseudo-element of a ::before pseudo-element, when ::before is styled as a list item, with {{CSSxRef("display", "display: list-item")}}.

Check out the individual pseudo-element reference pages for examples and browser compatibility information.

Highlight pseudo-elements inheritance

Highlight pseudo-elements, such as {{CSSxref("::selection")}}, {{CSSxref("::target-text")}}, {{CSSxref("::highlight()")}}, {{CSSxref("::spelling-error")}}, and {{CSSxref("::grammar-error")}}, follow a consistent inheritance model that differs from regular element inheritance.

When you apply styles to highlight pseudo-elements, they inherit from both:

  1. Their parent elements (following normal inheritance).
  2. The highlight pseudo-elements of their parent elements (following highlight inheritance).

This means that if you style both a parent element's highlight pseudo-element and a child element's highlight pseudo-element, the child's highlighted text will combine properties from both sources.

Here is a concrete example.

First, we have some HTML that includes two nested {{htmlelement("div")}} elements. Some of the included text content is contained directly inside the parent <div>, and some is nested inside the child <div>.

html
<div class="parent">
  Parent text
  <div class="child">Child text</div>
</div>

Next we include some CSS, which selects the parent and child <div> elements separately and gives them different {{cssxref("color")}} values, and selects the parent and child's selected text ({{cssxref("::selection")}}). This gives each <div> a different {{cssxref("background-color")}} and sets a different text color on the parent selection.

css
/* Style for the parent element */
.parent {
  color: blue;
}

/* Style for the parent's selected text */
.parent::selection {
  background-color: yellow;
  color: red;
}

/* Style for the child element */
.child {
  color: green;
}

/* Style for the child's selected text */
.child::selection {
  background-color: orange;
}

The example renders as follows:

{{EmbedLiveSample("highlight_inheritance", , "150")}}

Try selecting the text in both the parent and child elements. Notice that:

  1. When you select the parent text, it uses the yellow background and red text color defined in .parent::selection.
  2. When you select the child text, it uses:
    • The orange background from .child::selection.
    • The red text color inherited from the parent's ::selection pseudo-element.

This demonstrates how the child's highlight pseudo-element inherits from both its parent element and the parent's highlight pseudo-element.

CSS custom properties (variables) in highlight pseudo-elements inherit from their originating element (the element they're being applied to), not through the highlight inheritance chain. For example:

css
:root {
  --selection-color: lightgreen;
}

::selection {
  color: var(--selection-color);
}

.blue {
  --selection-color: blue;
}

When using the universal selector with highlight pseudo-elements, it prevents highlight inheritance. For example:

css
/* This prevents highlight inheritance */
*::selection {
  color: lightgreen;
}

/* Prefer this to allow inheritance */
:root::selection {
  color: lightgreen;
}

Specifications

{{Specifications}}

See also