[css-cascade] Proposal: @layer initial
should always be the first layer
#10094
Labels
Projects
@layer initial
should always be the first layer
#10094
Problem
Currently, layers all need to be explicitly defined. This means the page author needs to be very disciplined in setting their layer order upfront, before adding any “real” styles.
This almost always involves creating a layer named something like “defaults” early on.
This works, but is problematic for a few reasons:
@layer defaults
can potentially declare layers that come first.defaults
if they want to declare low-priority styles later.defaults
layer.Proposal
Allow authors to declare all low-priority styles in a layer named
initial
.initial
is one of the layer names that are "reserved for future use", so this layer can be automatically set up by the browser, such that:@layer initial
exists implicitly, regardless of the page’s layer order, similar to the implicit “outer” (unlayered) layer.Polyfill
In my testing, I found that the three major browsers do not do anything special to the “reserved” layer names, even though the spec says these must be invalid at parse time. I initially thought this was a bug and/or maybe the spec should be changed (see #10067), but then I realized that the current browser behavior makes this feature somewhat easy to polyfill, by setting
@layer initial;
as the very first style. This could even be done automatically by frameworks.Use cases
This solves all of the problems described above, and makes cascade layers easier to incorporate into existing workflows.
initial
layer, without having to come up with a carefully-coordinated layer setup.@layer initial
will provide a canonical way to deprioritize certain styles. It "always works" and is not impacted by order of appearance or existing layer architecture.@layer initial
, with a guarantee that they will cascade first, regardless of how/where they are imported.initial
layer.@layer initial
for their default styles, even though.adoptedStyleSheets
is ordered after the inner context’s own.styleSheets
.initial
layer will still cascade before the shadow-root's layered and unlayered styles.Open questions
initial
also be implicitly set up for nested sub-layers? (I think yes)initial
?The text was updated successfully, but these errors were encountered: