Skip to content

[css-pseudo] Can we make pseudo-elements first-class citizens in the DOM? #11559

Open
@bramus

Description

@bramus

Fun with Pseudos

In light of View Transitions I’ve been playing a lot with pseudo-elements trying to do all sorts of things with them from within JavaScript:

  • Access them
  • Get info about their position+size
  • Animate them
  • Get their animations
  • Access their styles

Accessing the pseudos themselves is in theory possible through Element.pseudo() but in practice it has no browser support1.

Some of the other features I listed above are made possible in some APIs through means of the pseudoElement option. When working with these it becomes quickly pretty clear that this pseudoElement is an afterthought that was introduced in order to get it to work quickly, instead of fully adopting the existence of CSSPseudoElement. See #4301 for example that chose to add the pseudoElement option to Element.animate instead of embracing CSSPseudoElement.

Since then other APIs have also been monkey-patched to accept a pseudoElement option:

So as for the things I want to do with pseudos this is the current state of affairs:

  • Access them: Theoretically using pseudo() but that has not shipped anywhere.
  • Get info about their position+size: Not directly possible (you could anchor a real Element to it and then do getBCR on that Element).
  • Animate them: Only if they already have animation attached to them that you can hijack
  • Get their animations: Theoretically using Element.getAnimations() but that is not specced yet. You can sniff ’m manually, though.
  • Access their styles: getComputedStyle() with pseudoElt param.

With more and more features leaning on pseudo-elements (select::picker, ::details-content, carousel pseudos, customizable select, etc.) this problem will become more visible to authors.

Making pseudo-elements first-class citizens

Instead of monkey-patching more APIs to accept a pseudoElement, I believe it would be nicer if pseudo-elements becamse first-class citizens of the DOM. All the pieces of the puzzle are there, it’s a matter of putting them together:

Just a starting point

Most likely I’m overlooking a bunch of things and I’m definitely lacking some nuances to some of the many pseudos that would allow/disallow this – so I’m very much looking forward to input on this one.

Footnotes

  1. Thankfully I didn’t really need the pseudos directly in my experiments. Because I only needed the animations+effects applied to them, I was able to sniff them out through document.getAnimations()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions