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
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
CSSPseudoElement | Chrome No support No | Edge No support No | Firefox
Full support
63
| IE No support No | Opera No support No | Safari ? | WebView Android No support No | Chrome Android No support No | Firefox Android
Full support
63
| Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No |
element | Chrome No support No | Edge No support No | Firefox
Full support
67
| IE No support No | Opera No support No | Safari ? | WebView Android No support No | Chrome Android No support No | Firefox Android
Full support
67
| Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No |
type | Chrome No support No | Edge No support No | Firefox
Full support
63
| IE No support No | Opera No support No | Safari ? | WebView Android No support No | Chrome Android No support No | Firefox Android
Full support
63
| Opera Android No support No | Safari iOS No support No | Samsung 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.