Is it possible to do a CSS @supports check on a @media rule?

前端 未结 1 1360
醉话见心
醉话见心 2021-01-04 22:45

The @support rule allows one to do a feature query on CSS properties. I am wondering whether it is possible to do a feature check on specifically @media rules?

For e

相关标签:
1条回答
  • 2021-01-04 23:23

    This does not seem to work. Should it work?

    No; @supports only supports property declarations, not at-rules or indeed any other grammatical construct in CSS. You're not looking to check for support for the @media rule anyway; you're trying to check for support for specific media features. But @supports doesn't support that either, even though media features share the same declaration syntax with property declarations.

    To that end, you don't need the @supports rule at all. To check if a browser supports a certain media feature, simply write a @media rule with a media query list containing both the media feature and its negation:

    @media not all and (pointer), (pointer) {
      p { color: green; }
    }
    <p>If this text is green, your browser supports the <code>pointer</code> media feature.

    (Note that Media Queries 4 removes the restriction of not requiring a media type from MQ3, so the negation really ought to be not (pointer), but no browser supports this yet and a media type is still required.)

    Browsers that don't recognize the pointer media feature will interpret the @media rule as @media not all, not all (in spite of the not in the not all and (pointer) media query). See section 3.2 of the spec, which says

    An unknown <mf-name> or <mf-value>, or disallowed <mf-value>, results in the value “unknown”. A <media-query> whose value is “unknown” must be replaced with not all.

    If you need to apply CSS for when a browser does not support a media feature, these error handling rules mean you'll need to take advantage of the cascade (and if you don't know the original values in advance, you may be stuck):

    p { color: red; }
    
    @media not all and (pointer), (pointer) {
      p { color: currentcolor; }
    }
    <p>If this text is red, your browser does not support the <code>pointer</code> media feature.

    0 讨论(0)
提交回复
热议问题