CSS.supports Probes Feature Support

CSS.supports probes a single declaration with two arguments or a full @supports condition with one, driving progressive enhancement decisions.

JavascriptResilience

Code

javascriptsnippet
17 lines
// CSS.supports 探测浏览器是否支持某条声明或完整的 @supports 条件
console.log(CSS.supports('display', 'grid'));
console.log(CSS.supports('display', 'flex'));
console.log(CSS.supports('--accent', 'rebeccapurple')); // 自定义属性声明同样可探测

// 双参形式测"属性: 值";单参形式测完整条件语法
console.log(CSS.supports('(display: grid) and (backdrop-filter: blur(2px))'));
console.log(CSS.supports('display', 'turbowarp')); // 不存在的值 → false

// 特性探测驱动渐进增强:支持才启用玻璃效果,否则走实色降级
function glassSupported() {
  return CSS.supports('backdrop-filter', 'blur(4px)');
}

const toolbar = document.createElement('nav');
toolbar.classList.add(glassSupported() ? 'toolbar-glass' : 'toolbar-solid');
console.log('glass:', glassSupported(), '->', toolbar.className);

CSS.supports Probes Feature Support: the mechanics

CSS.supports Probes Feature Support comes down to the function glassSupported.

Watch out for

  • No third-party code is loaded at any point while this runs.
  • A browser is the runtime here: document are not available in Node.

Applying it

Signature: function glassSupported()

The run emits 1 labeled section — glass: — one per step.

Statements that matter

return CSS.supports('backdrop-filter', 'blur(4px)');

Interactive Tools for This Topic

FAQ

Does CSS.supports Probes Feature Support run in plain Node.js?
No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.