Comments on: A Complete Guide to CSS Cascade Layers https://css-tricks.com/css-cascade-layers/ Tips, Tricks, and Techniques on using Cascading Style Sheets. Mon, 03 Jun 2024 17:19:29 +0000 hourly 1 https://wordpress.org/?v=6.5.5 By: Geoff Graham https://css-tricks.com/css-cascade-layers/#comment-1824044 Mon, 03 Jun 2024 17:19:29 +0000 https://css-tricks.com/?p=363766#comment-1824044 In reply to Michael M.

Nice catch! Got that all cleaned up. ✨

]]>
By: Michael M https://css-tricks.com/css-cascade-layers/#comment-1824040 Mon, 03 Jun 2024 05:16:46 +0000 https://css-tricks.com/?p=363766#comment-1824040 I like the coloured cascade image (https://css-tricks.com/wp-content/uploads/2022/02/layers-tall-outlines2.svg) but aren’t classes meant to be more important than elements? This image currently reads as if it says * -> .class -> element -> #ID.

]]>
By: Koos Looijesteijn https://css-tricks.com/css-cascade-layers/#comment-1807211 Thu, 07 Dec 2023 20:44:50 +0000 https://css-tricks.com/?p=363766#comment-1807211 In reply to Jake Montgomery.

If I understood the article correctly, this could be the way to deal with that:

@layer important, default, utilities

Now if I use your example again:

@import url(blabla.css) layer(utilities);

And blabla.css contains some style with !important, we should be able to override that with another important style in the the important layer?

]]>
By: Hans Grimm https://css-tricks.com/css-cascade-layers/#comment-1803215 Mon, 20 Mar 2023 10:14:15 +0000 https://css-tricks.com/?p=363766#comment-1803215 In reply to Noam.

Great stuff! Currently applying cascade layers to a new project. About !important: I find that using cascade layers all but elimininates the need for that, even proactive use such as overrides.

]]>
By: Phil Wolstenholme https://css-tricks.com/css-cascade-layers/#comment-1802930 Sun, 12 Feb 2023 16:31:19 +0000 https://css-tricks.com/?p=363766#comment-1802930 Just a quick note to say that a Postcss polyfill is now available: https://www.npmjs.com/package/@csstools/postcss-cascade-layers

It’s also included in postcss-preset-env: https://preset-env.cssdb.org/features/#cascade-layers

]]>
By: EnlightenedWebDeveloper https://css-tricks.com/css-cascade-layers/#comment-1802556 Sat, 31 Dec 2022 21:39:56 +0000 https://css-tricks.com/?p=363766#comment-1802556 I see no value whatsoever in ever using layers, except for the one case where their !important overrides that in all other non-layer styles in a typical web page. That’s it. And even that is confusing as its reversed with first layers overriding later layers. Super dumb design.

The fact is, the minute you modify any element in your author sheets, like div, p, span, etc., you can never override them using layers using any cascade, specificity. etc. And most modern web developers create ‘reset’ sheets to change their element designs. So layers die.

The sad reality is, pre-processors, variables, layers, and other new web tech was designed by people that never understood how cascading style sheets work and how text-based properties inherit. If they did, they would see that hey never need those tools. You don’t need to change things that inherit, say from the body element of a typical web page. And when you do, you already have a weighted and origin-like, layer-like system using ID and Class that works exactly the same as sheet origin and layer origin precedence with a fraction of the CSS code needed!

Example, any HTML modified by ID can never be overridden by a more weighted set of classes. They remain in their own layer, essentially.

This is why CSS has worked perfectly for 20+ years and rarely every changes or needs to change.

]]>
By: Miriam Suzanne https://css-tricks.com/css-cascade-layers/#comment-1797060 Mon, 12 Sep 2022 21:52:35 +0000 https://css-tricks.com/?p=363766#comment-1797060 In reply to Charlie Coplestone.

Sorry, a small typo there. For load-css(), you need the built-in ‘meta’ module from Sass:

@use 'sass:meta';

@layer bootstrap {
  @include meta.load-css('~bootstrap/scss/bootstrap');
}
]]>
By: Miriam Suzanne https://css-tricks.com/css-cascade-layers/#comment-1797059 Mon, 12 Sep 2022 17:34:19 +0000 https://css-tricks.com/?p=363766#comment-1797059 In reply to Charlie Coplestone.

I don’t know what version of Sass you are using, but Sass doesn’t have any special handling for layers. So it is likely treating your import as a CSS-import (rather than a Sass-import) because it has non-Sass syntax.

If ~bootstrap/scss/bootstrap provides both Sass features (mixins, variables, etc) and also has direct CSS output, those will need to be handled a bit differently. You want to put the CSS-output into a layer, but keep the Sass features available anywhere. That should be possible using Sass modules and more specific Bootstrap paths. Something like…

Forward all the Sass features from one file:

// bootstrap-sass.scss
// Use the configuration syntax as needed
@forward "~/bootstrap/scss/functions";
@forward "~/bootstrap/scss/variables";
@forward "~/bootstrap/scss/maps";
@forward "~/bootstrap/scss/mixins";

Then in your main file, you can @use the Sass features you’ve forwarded, and load the CSS from bootstrap separately:

// main.scss
@use 'bootstrap-sass' as bs;

@layer bootstrap {
  @include load-css('~bootstrap/scss/bootstrap');
}

.example {
  @include bs.border-end-radius($alert-border-radius);
}
]]>
By: Miriam Suzanne https://css-tricks.com/css-cascade-layers/#comment-1797058 Mon, 12 Sep 2022 17:06:07 +0000 https://css-tricks.com/?p=363766#comment-1797058 In reply to Patryk P.

You can use all: revert-layer to roll back all layer styles, similar to using all: revert to roll back author styles.

Media queries can absolutely change layer order. The @layer rules are allowed inside media-queries, and then only impact the order of layers when the media-query is applied. You could do something like:

@media print { @layer my, print, layer, order; }
@media screen { @layer my, screen, layer, order; }
]]>
By: Charlie Coplestone https://css-tricks.com/css-cascade-layers/#comment-1797044 Sun, 11 Sep 2022 17:06:19 +0000 https://css-tricks.com/?p=363766#comment-1797044 Hey :)

Having a play around with cascade layers for a new project and I seem to be getting an ‘Undefined mixin’ error when providing bootstrap with its own layer like so:

`// Layers order
@layer bootstrap, theme, utilities, third-party;

// Variables
@import “1-variables/app”;

// Theme mixins
@import “2-mixins/badge”;
@import “2-mixins/button”;
@import “2-mixins/modal”;
@import “2-mixins/switch”;
@import “2-mixins/tabs”;
@import “2-mixins/theme”;

// Bootstrap
@layer bootstrap {
@import “~bootstrap/scss/bootstrap”;
}

// Theme components
@layer theme {
@import “3-components/accordion”;
@import “3-components/alert”;
@import “3-components/avatar”;
}`

Or even:

@import '~bootstrap/scss/bootstrap' layer(bootstrap);

The error:

ERROR in ./resources/sass/phoenix/phoenix.scss
Module build failed (from ./node_modules/laravel-mix/node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleBuildError: Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: Undefined mixin.

26 │ @include border-end-radius($alert-border-radius);

If I then import bootstrap outside of a layer, the project builds fine.

As you can see the order of my layers states that bootstrap is first, so this is odd to me.

Has anyone else ran into this issue?

]]>
By: Patryk P https://css-tricks.com/css-cascade-layers/#comment-1796490 Sat, 23 Jul 2022 19:46:10 +0000 https://css-tricks.com/?p=363766#comment-1796490 Hey! Great article :)
There is one question I would like to ask.
Is there a way to revert whole layer? Just like single css rule: color: rever-layer.
I mean its possible to dynamically change layer order depending on media queries or class names?

]]>
By: Miriam Suzanne https://css-tricks.com/css-cascade-layers/#comment-1795774 Mon, 16 May 2022 23:30:46 +0000 https://css-tricks.com/?p=363766#comment-1795774 In reply to Mark.

Hi Mark, in theory the answer is ‘yes’ – since any styles from anywhere can be added to a layer. But the practical answer probably depends a lot on the exact tool-chain being used to ship your components, and also the tool chain used to apply them. I can’t speak to that without a lot more detail, and even then there’s a limited set of tools I have experience with. I would file an issue with whoever is shipping the components, to see if they have advice.

]]>
By: Mark https://css-tricks.com/css-cascade-layers/#comment-1795767 Mon, 16 May 2022 06:04:27 +0000 https://css-tricks.com/?p=363766#comment-1795767 Is there a way I can assign a layer to third party components that do not expose a stylesheet so that I can keep the styles of the third party components one layer below my own layer.

]]>
By: Miriam Suzanne https://css-tricks.com/css-cascade-layers/#comment-1794991 Tue, 15 Mar 2022 16:05:07 +0000 https://css-tricks.com/?p=363766#comment-1794991 In reply to Jerzy.

All new features are broken in old browsers, there’s no way to avoid that. The backwards-compatibility requirement is that new features shouldn’t break old CSS in any browser.

In this case, as with media-queries, any code inside an @layer will be hidden from old browsers. And there’s a polyfill in development which would be able to generate and link a legacy stylesheet automatically. It’s not an ideal situation, but it is a temporary one.

]]>
By: Jerzy https://css-tricks.com/css-cascade-layers/#comment-1794851 Sat, 05 Mar 2022 21:49:22 +0000 https://css-tricks.com/?p=363766#comment-1794851 How about backwards compatibility with older browsers? There would be need for two stylesheets – with and without layers. This is not how CSS should work. There should be no breaking changes.

]]>