How to use the @supports rule in your CSS
Lea Verou introduces feature support detection with the simple and elegant @supports rule.
Have you ever used some form of CSS feature detection such as Modernizr to differentiate your CSS's behaviour based on whether a feature is supported? I bet most of you have. Although the cascading nature of CSS lets us provide fallbacks for many CSS features just by writing multiple declarations, sometimes we need more granular control over what is applied when some of our cutting edge bling isn't there.
Consider border-image. When it's unsupported, we usually want to provide a fallback background and a different border, which shouldn't apply when border-image applies. Another common case is new layout modules (Flexbox, Grid Layout and Regions); when they aren't available, we need to code an alternative layout using properties that could conflict with these technologies, which means they can't co-exist.
Fortunately, the CSS WG designed an elegant and pure CSS solution a while back called the @supports rule. With this rule, the border-image example would be written like:
.foo {
border: 15px solid transparent;
border-image: url(fancyborders.png) 33%;
}
@supports not (border-image: url(foo.png) 33%) {
.foo {
border: 3px solid rgba(0,0,0,.3);
background: url(alternative-bg.png) beige;
}
}
This is called a 'feature query' , which is similar to a media query. In practice, you'll want to check for prefixed versions as well:
@supports not (border-image: url(foo.png) 33%)
or (-moz-border-image: url(foo.png) 33%)
or (-webkit-border-image: url(foo.png) 33%) { ...
Besides or, and and not, operators are available, allowing for any kind of complex boolean logic. These rules can be nested, which greatly simplifies your code when using many complex feature queries.
There's a catch. It's unwise to use @supports for features that browsers supported before supporting @supports itself. Therefore, we cannot use it for many things yet. However, it's already supported by Firefox 22+, Chrome 28+ and Opera 12.1+. If IE 11 and Safari 7 also support it, we can start using it for features introduced at or after these versions.
The CSS WG also realised that using prefixes for @supports would kind of defeat the purpose (as authors would have to write multiple rules several times). Therefore, every browser that supports @supports is doing so without any prefix. There is no such thing as @-webkit-supports. When browsers were unsure about their implementations, they hid it behind a flag, like Firefox did in versions 17-27.
You may be thinking that this is great for CSS, but it doesn't help you detect CSS features through JavaScript, which is sometimes needed. Fortunately, it comes with its own elegant JavaScript API as well: CSS.supports() . It's used like so:
if (CSS.supports('border-image', 'url(foo.png) 33%')) { ... }
This function is also easy to polyfill, so it can be made available in browsers that haven't caught up. Hopefully in the future we'll be able to detect support for other CSS features, such as @rules and selectors, rather than just properties and values.
Words: Lea Verou
Lea Verou previously worked as a developer advocate at W3C and currently spends her days writing & designing her first book, titled CSS Secrets, which is due to be published with O'Reilly in 2014
This article originally appeared in net magazine issue 248.
Liked this? Read these!
- Examples of CSS for great web design inspiration
- Our favourite web fonts - and they don't cost a penny
- How to make an app: try these great tutorials
Thank you for reading 5 articles this month* Join now for unlimited access
Enjoy your first month for just £1 / $1 / €1
*Read 5 free articles per month without a subscription
Join now for unlimited access
Try first month for just £1 / $1 / €1
Get the Creative Bloq Newsletter
Daily design news, reviews, how-tos and more, as picked by the editors.
The Creative Bloq team is made up of a group of design fans, and has changed and evolved since Creative Bloq began back in 2012. The current website team consists of eight full-time members of staff: Editor Georgia Coggan, Deputy Editor Rosie Hilder, Ecommerce Editor Beren Neale, Senior News Editor Daniel Piper, Editor, Digital Art and 3D Ian Dean, Tech Reviews Editor Erlingur Einarsson and Ecommerce Writer Beth Nicholls and Staff Writer Natalie Fear, as well as a roster of freelancers from around the world. The 3D World and ImagineFX magazine teams also pitch in, ensuring that content from 3D World and ImagineFX is represented on Creative Bloq.