

Day 68: cascade layers and browser support
source link: https://www.matuzo.at/blog/2022/100daysof-day68/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Day 68: cascade layers and browser support
posted on December 28., 2022
It’s time to get me up to speed with modern CSS. There’s so much new in CSS that I know too little about. To change that I’ve started #100DaysOfMoreOrLessModernCSS. Why more or less modern CSS? Because some topics will be about cutting-edge features, while other stuff has been around for quite a while already, but I just have little to no experience with it.
Cascade layers are one of the most interesting and useful additions to CSS recently. It will change the way we write CSS, how we use selectors, naming conventions, and probably also more things that I can’t think of right now.
If you’re as excited about using cascade layers as I am, you have to consider browser support before you get started.
Browser support
All major desktop browsers started supporting cascade layers between February and April 2022, but your users might use an older version of Safari, IE11, or one of the mobile browsers that doesn’t support it yet.
Browser support: Chrome, Edge since version 99, Safari iOS/macOS 15.4, Firefox 97, Opera 86, Chrome for Android/Android Browser 108, Samsung Internet 18, Firefox for Android 107, Opera Mobile 72.
Feature queries
When you use cascade layers in a browser that doesn’t support them, styles in your layers will be ignored entirely. Only unlayered styles will be applied. I was trying to figure out a way to serve styles only to browsers that don’t support layers in order to provide some kind of basic fallback styling for them, but I had no luck.
There is a @supports feature in CSS that will allow us to test for support of @layer
and other at-rules, but the feature itself has no support in any browser yet.
@supports at-rule(@layer) {
/* code applied for browsers with layer support */
}
@supports not at-rule(@layer) {
/* fallback applied for browsers without layer support */
}
Polyfill
If you want to use cascade layers today, your best option is to use this PostCSS polyfill. Miriam Suzanne explains how to use it and how it works in Cascade Layers – There's a Polyfill for That!
npm install postcss @csstools/postcss-cascade-layers --save-dev
const postcss = require('postcss');
const postcssCascadeLayers = require('@csstools/postcss-cascade-layers');
postcss([
postcssCascadeLayers(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);
Further reading
Want more?
Overview: 100 Days Of More Or Less Modern CSS
My blog doesn't support comments yet, but you can reply via e-mail.
Recommend
-
9
Cascade Layers (CSS @layer) – Bram.usSkip to content When writing CSS, we developers have to carefully think about h...
-
7
Getting Started With CSS Cascade Layers ...
-
5
Hello, CSS Cascade Layers 11 Feb 2022 One of the most common causes of confusion in CSS is facing specificity while writing styles. For example, changing the display value for an element neve...
-
5
This is your complete guide to CSS cascade layers, a CSS feature that allows us to define explicit contained layers of specificity, so that we have full control over which styles take priority in a project without relying on specificity hacks...
-
15
Introduction to Cascade Layers in CSSHow to use Cascade Layers in CSSCascade layers are the latest addition to CSS. They allow developers to control the specificity and the source order across mult...
-
11
Using CSS Cascade Layers to Manage Custom Styles in a Tailwind Project DigitalOcean joining...
-
6
A Handy Use For Cascade Layers Tue Aug 23 2022 I was just preparing a demo for an
-
5
Day 37: cascade layers posted on November 15., 2022 It’s time to get me up to speed with modern CSS. There’s so much new in CSS that I know too little about. To change...
-
9
Day 74: using !important in cascade layers posted on January 5., 2023 It’s time to get me up to speed with modern CSS. There’s so much new in CSS that I know too little...
-
5
Cascade Layers are useless* posted on June 21., 2023 *if you don‘t understand the problems they solve and use them in combination with other solutions that tackle the same challenges albe...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK