CSSPseudoElement

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The CSSPseudoElement interface represents a pseudo-element that may be the target of an event or animated using the Web Animations API. Instances of this interface may be obtained by calling Element.pseudo().

Properties

CSSPseudoElement.element Read only
Returns the originating/parent Element of the pseudo-element.
CSSPseudoElement.type Read only
Returns the pseudo-element selector as a CSSOMString.

Methods

CSSPseudoElement implements Animatable, so it has the following methods available to it:

Animatable.animate()
A shortcut method to create and run an animation on a pseudo-element. Returns the created Animation object instance.
Animatable.getAnimations()
Returns an array of Animation objects currently active on the pseudo-element.

CSSPseudoElement extends EventTarget, so it also inherits the following methods:

EventTarget.addEventListener()
Registers an event handler of a specific event type on the pseudo-element.
EventTarget.dispatchEvent()
Dispatches an event to this pseudo-element.
EventTarget.removeEventListener()
Removes an event listener from the 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 would cause the opening quotation mark of the first <q> element in the page to flash red twice in a row.

const element = document.querySelector('q');
const cssPseudoElement = element.pseudo('::before');
myAnimation(cssPseudoElement);

function myAnimation(cssPseudoElement) {
	let color;
	switch (cssPseudoElement.type) {
		case '::before':
			color = 'red';
			break;
		case '::after':
			color = 'green';
			break;
		default:
			color = 'blue';
			break;
	}
	cssPseudoElement.animate([
		{ color: 'currentColor' },
		{ color: color }
	], {
		duration: 250,
		direction: 'alternate',
		iterations: 4
	});
}

Adding additional animations using getAnimations()

The example below uses CSS Animations to animate the opacity of a pseudo-element, causing it to flash once per second. By calling either Document.getAnimations or Element.getAnimations, it is possible to obtain a reference to the animated CSSPseudoElement by reading the animation's effect target. In the example below, the pseudo-element will have a second animation placed upon it programmatically, causing its colour to alternate with every flash in supporting browsers.

HTML

<div></div>

CSS

div::before {
	content: "Hello, world!";
	animation-name: cssAnimation;
	animation-duration: 500ms;
	animation-direction: alternate;
	animation-iteration-count: infinite;
}

@keyframes cssAnimation {
	from {
		opacity: 0.99;
	}
	to {
		opacity: 0.2;
	}
}

JavaScript

const animations = document.getAnimations({subtree: true});
const cssPseudoElement = animations[0].effect.target;
cssPseudoElement.animate([
	{ color: 'currentColor' },
	{ color: 'red' }
], {
	duration: 1000,
	direction: 'alternate',
	iterations: Infinity
});

Result

Specifications

Specification Status Comment
Web Animations
The definition of 'CSSPseudoElement' in that specification.
Working Draft Added inheritance of the Animatable interface.
CSS Pseudo-Elements Level 4
The definition of 'CSSPseudoElement' in that specification.
Working Draft Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
CSSPseudoElement
Experimental
Chrome No support NoEdge No support NoFirefox Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.animations-api.getAnimations.enabled preference. To change preferences in Firefox, visit about:config.
No support 47 — 63
Disabled
Disabled From version 47 until version 63 (exclusive): this feature is behind the dom.animations-api.core.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera No support NoSafari ? WebView Android No support NoChrome Android No support NoFirefox Android Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.animations-api.getAnimations.enabled preference. To change preferences in Firefox, visit about:config.
No support 47 — 63
Disabled
Disabled From version 47 until version 63 (exclusive): this feature is behind the dom.animations-api.core.enabled preference. To change preferences in Firefox, visit about:config.
Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
element
Experimental
Chrome No support NoEdge No support NoFirefox Full support 67
Disabled
Full support 67
Disabled
Disabled From version 67: this feature is behind the dom.animations-api.getAnimations.enabled preference. To change preferences in Firefox, visit about:config.
No support 63 — 67
Alternate Name Disabled
Alternate Name Uses the non-standard name: parentElement
Disabled From version 63 until version 67 (exclusive): this feature is behind the dom.animations-api.getAnimations.enabled preference. To change preferences in Firefox, visit about:config.
No support 47 — 63
Alternate Name Disabled
Alternate Name Uses the non-standard name: parentElement
Disabled From version 47 until version 63 (exclusive): this feature is behind the dom.animations-api.core.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera No support NoSafari ? WebView Android No support NoChrome Android No support NoFirefox Android Full support 67
Disabled
Full support 67
Disabled
Disabled From version 67: this feature is behind the dom.animations-api.getAnimations.enabled preference. To change preferences in Firefox, visit about:config.
No support 63 — 67
Alternate Name Disabled
Alternate Name Uses the non-standard name: parentElement
Disabled From version 63 until version 67 (exclusive): this feature is behind the dom.animations-api.getAnimations.enabled preference. To change preferences in Firefox, visit about:config.
No support 47 — 63
Alternate Name Disabled
Alternate Name Uses the non-standard name: parentElement
Disabled From version 47 until version 63 (exclusive): this feature is behind the dom.animations-api.core.enabled preference. To change preferences in Firefox, visit about:config.
Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
type
Experimental
Chrome No support NoEdge No support NoFirefox Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.animations-api.getAnimations.enabled preference. To change preferences in Firefox, visit about:config.
No support 47 — 63
Disabled
Disabled From version 47 until version 63 (exclusive): this feature is behind the dom.animations-api.core.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera No support NoSafari ? WebView Android No support NoChrome Android No support NoFirefox Android Full support 63
Disabled
Full support 63
Disabled
Disabled From version 63: this feature is behind the dom.animations-api.getAnimations.enabled preference. To change preferences in Firefox, visit about:config.
No support 47 — 63
Disabled
Disabled From version 47 until version 63 (exclusive): this feature is behind the dom.animations-api.core.enabled preference. To change preferences in Firefox, visit about:config.
Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
User must explicitly enable this feature.
User must explicitly enable this feature.
Uses a non-standard name.
Uses a non-standard name.

See also