Nando Muñoz

BlogCode and management

How to detect support for CSS pseudo-classes with vanilla JavaScript in your browser

At some point you may need to know whether a CSS pseudo-class is supported by the browser. I once did, with :autofill versus :-webkit-autofill.

Originally written in Spanish. This translation is automatic and may contain mistakes. Read the original in Spanish

A CSS book on the table
Foto de KOBU Agency en Unsplash.

At some point you may need to know whether a CSS pseudo-class is supported by the browser. I once did, with the :autofill property versus :-webkit-autofill, and now I'll tell you how I solved it.

Thanks to the :autofill property you can always apply a different style to an element such as an <input type="email"> or <input type="password"> once it has been autofilled, as in the typical login form.

In my work I needed to detect via JavaScript, only when the input is autofilled, but there is no JS event of the “autofill” kind. There is a way, a somewhat more sophisticated way of tackling the problem, devised by Tommy Brunn with the animationstart event but it never quite fitted for me.

The supportPseudoClass module

Browsing Stack Overflow, a user shared a very simple function to detect when a pseudo-class is supported.

https://gist.github.com/nandomunoz/a6096b7b63593afaa588bf45eabec947

    Written by

    Nando Muñoz

    Frontend tech lead · AEM, EDS & SFCC · Cádiz · Madrid