Using @supports to fork CSS
When you’re building out a responsive web design, attempting to provide a single design that works everywhere, on every device, it’s a simple fact that you’ll frequently encounter situations when features or techniques are not supported on certain devices. It’s also a likely scenario with newer selectors, like :has()
, which we looked at earlier in the chapter. In these instances, you’ll likely want to create a fork in your CSS. If the browser supports a feature, provide one chunk of code; if it doesn’t, it gets different code.
This is the kind of situation that gets handled by if/else
or switch
statements in JavaScript. In CSS, we use the @supports
at-rule.
Now, before we dig into feature queries, it is worth pointing out a straightforward way of providing fallbacks for older browsers. Suppose we want to use some of our new viewport-related length units but ensure we have some kind of fallback for older...