【问题标题】:What exactly happens when a browser doesn't support feature queries?当浏览器不支持功能查询时究竟会发生什么?
【发布时间】:2018-12-13 09:30:02
【问题描述】:

特征查询对于有条件地加载 CSS 很有用。它们允许您仅向支持指定功能的浏览器提供一段 CSS 代码。

@supports (feature-name: feature-value) {
    /* Some CSS code here, for browsers that support feature-name: feature-value */
}

但是,许多旧版浏览器不支持功能查询。

https://caniuse.com/#feat=css-featurequeries

对于不支持功能查询的浏览器,功能查询中的 CSS 会发生什么变化?浏览器会加载并使用它吗?还是直接跳过或忽略?

【问题讨论】:

    标签: css browser-feature-detection


    【解决方案1】:

    功能查询以及其中的所有内容都会被不支持它们的浏览器忽略。

    @supports (feature-name: feature-value) {
        /*  CSS inside the feature query is visible 
            only to browsers that support feature queries.
            Invisible to other browsers, 
            even if they support feature-name: feature-value. */
    }
    

    对于这些浏览器,您需要使用其他功能检测工具,例如 Modernizr

    CSS media queries 类似。如果浏览器不支持媒体查询/功能查询,它只会跳过它们以及里面的所有内容。

    【讨论】:

    • 考虑到这种行为,我想知道我是否应该写 @supports 以及包括 Modernizr 检测以涵盖所有领域?这将是更多的代码..
    • 有些重叠。 CSS 文件中的@supports 是测试 CSS 功能的简单方法。 Modernizr 更全面,还可以测试 JavaScript 功能。如果您使用的是 Modernizr,那么使用 @supports 可能是多余的。
    猜你喜欢
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 2011-01-29
    • 2023-04-01
    • 2015-12-25
    • 2017-03-07
    相关资源
    最近更新 更多