web api interface
CSSPseudoElement
Limited availability
The CSSPseudoElement interface represents a pseudo-element.
Instances of this interface may be obtained by calling pseudo() or pseudo().
Instance properties
elementExperimental Read only- : Returns the ultimate originating
Elementof the pseudo-element.
- : Returns the ultimate originating
parentExperimental Read only- : Returns the immediate originating element of the pseudo-element.
typeExperimental Read only- : Returns the pseudo-element selector as a string.
Instance methods
pseudo()Experimental- : Returns a
CSSPseudoElementinstance representing a specific nested pseudo-element
- : Returns a
Description
The CSSPseudoElement interface represents a pseudo-element. You can retrieve a representation of a pseudo-element attached to a DOM element using the pseudo() method, or a representation of a nested pseudo-element (for example, the ::marker in ::before::marker) using the pseudo() method.
The type property returns a string representing the type of the pseudo-element. Supported types are:
The element and parent properties sound similar, but they have a difference in functionality:
- The
elementproperty always returns anElement: A reference to the ultimate originating element of the pseudo-element or nested pseudo-element. - The
parentproperty returns a reference to the pseudo-element’s immediate originating element: This can be either anElement, or aCSSPseudoElementin the case of a nested pseudo-element.
Examples
Basic example using Element.pseudo
Using pseudo-elements, most modern browsers will automatically add quotation marks around text inside a <q> element. (A style rule may be needed to add quotation marks in older browsers.) The example below demonstrates the basic properties of the CSSPseudoElement object representing the opening quotation mark.
const element = document.querySelector("q");
const cssPseudoElement = element.pseudo("::before");
console.log(cssPseudoElement.element); // Outputs [object HTMLQuoteElement]
console.log(cssPseudoElement.type); // Outputs '::before'