How to detect CSS variable support with JavaScript?

Sometimes, we want to detect CSS variable support with JavaScript.

In this article, we’ll look at how to detect CSS variable support with JavaScript.

How to detect CSS variable support with JavaScript?

To detect CSS variable support with JavaScript, we can use the CSS.supports method.

For instance, we write:

console.log(CSS.supports('color', 'var(--fake-var)'))

to check if we can set the color CSS property to var(--fake-var).

If this returns true, then CSS variables is supported.

Conclusion

To detect CSS variable support with JavaScript, we can use the CSS.supports method.