close
Skip to content

Proposal: Use cascade layers for all Gutenberg generated CSS #51128

@krokodok

Description

@krokodok

As a third party developer, I often find myself in situations where I have to write overly specific selectors to overwrite Gutenberg generated CSS. For example:

/* CSS from Gutenberg */
body .is-layout-constrained > * + * {
    margin-block-start: var(--wp--preset--spacing--vertical);
    margin-block-end: 0;
}

/* My CSS */
body .is-layout-constrained > .my-custom-element {
    margin: 0;
}

If all CSS generated by Gutenberg would be wrapped inside the new CSS concept of cascade layers, life would be much easier for me and my colleagues. For example:

@layer gutenberg, custom;

/* CSS from Gutenberg */
@layer gutenberg {
    body .is-layout-constrained > * + * {
        margin-block-start: var(--wp--preset--spacing--vertical);
        margin-block-end: 0;
    }
}

/* My CSS */
@layer custom {
    .my-custom-element {
        margin: 0;
    }
}

Cascade layers are a new technology that are supported since Chromium 99 and FireFox 97. In May 2023 it is supported by most modern browsers. The global usage is 90.19%.


Summary of previous discussions

This is not a new proposal, but rather the opportunity to have a discussion that was lead in multiple sub-discussions earlier on different topics. See:

grafik
Source: #19611 (comment)

It is also worth noting that this feature was planned as "Phase 3" of an initiative to improve block editor styles launched last summer:

Pros

  • Help to reduce overly specific CSS selectors.
  • Make third-party development much easier.
  • Help to have a nicer, cleaner CSS structure also within Gutenberg/Wordpress.
  • Make Gutenberg less intimidating for developers and users, thus lowering the barriers and reservations against it.

Cons

  • Cascade layers are not widely supported enough. But bear in mind that the last time those discussions were held was roughly a year ago. Support is way better now.
  • There still needs to be a fallback for browsers that don't support cascade layers. All layered CSS will be unreachable for them.
  • All un-layered CSS overwrites all layered CSS. There needs to be a way to control this or unforeseen theme & plugin interactions could overthrow the whole styling system.

Ideas

  • There should be a way to check if the current client's browser supports cascade layers. Only then they should be applied. Otherwise a fallback CSS without layers should be used.
  • Developers should have to opt-in to using CSS layers on their own responsibilities. Only then they want to benefit from using them and they make it their duty to ensure all used third-party plugins work correctly in this environment.

I feel this issue should not be a discussion on "if" to use this feature at all, but rather a "how and when". One year ago, this discussion was rather speculative. But now technology has changed, block editor styles advanced and many issues from back then are closed. We should prepare for "phase 3"! 🚀

Metadata

Metadata

Assignees

No one assigned

    Labels

    CSS StylingRelated to editor and front end styles, CSS-specific issues.[Type] EnhancementA suggestion for improvement.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions