cssbrowser-feature-detection

How can I feature-detect CSS filters?


In some browsers, including Chrome stable, you can do this:

h3 {
  -webkit-filter: grayscale(1);
  filter: grayscale(1);
}

And wouldn’t you know it, the h1 will be rendered completely in grayscale. Everything old is new again.

Anyway — does anyone know of any way to feature-detect for this?

I need to be able to apply other styles if the filter won’t work.


Solution

  • You can now use CSS' build-in @support to conditionally apply styles. Note that the browser support for @support is good but not perfect. This is a nice article explaining how it works with several examples: https://iamsteve.me/blog/entry/feature-detection-with-css

    For instance, you can do something like this (see it live):

    @supports (filter: grayscale(1)) or (-webkit-filter: grayscale(1)) {
      h3 {
        -webkit-filter: grayscale(1);
        filter: grayscale(1);
      }
    }
    
    @supports not (filter: grayscale(1)) and not not (-webkit-filter: grayscale(1)) {
      h3 {
        color: #808080;
      }
    }