Skip to content

[css-mixins-1] Are @mixin rules valid within other at-rules? #12417

Closed

Description

@andruud

For example, are @mixin rules valid within @container?

@container (...) {
  @mixin --m() {
    /* style rules */
  }
}

If yes, we already know that --m() isn't defined conditionally based on CQ evaluation (https://drafts.csswg.org/css-conditional-5/#container-rule, last paragraph of chapter), but are the inner style rules affected?

div {
  @apply --m();
}

In other words, does the above @apply rule "remember" that the referenced mixin is enclosed by an @container? Any decision we make should hopefully answer the same question about @layer, @starting-style, etc.

My initial thought is that (for simplicity) we should make @mixin invalid within any at-rule except conditional rules with the "old school" processing model (i.e. currently @media and @supports).

Pinned by kbabbitt

Activity

  1. romainmenke commented on Jun 30, 2025

    @romainmenke
    Member

    My initial thought is that (for simplicity) we should make @mixin invalid within any at-rule except conditional rules with the "old school" processing model (i.e. currently @media and @supports).

    I somewhat agree with this.

    But I think mixin rules should also be valid in @layer rules to be able to use cascade layers to resolve name conflicts.

    However the inner styles should not be affected by this I think.
    I would expect :root to have color green in this example:

    @layer early-layer, later-layer;
    
    @layer later-layer {
      @mixin --m() { /* later layer declaration wins */
        color: green;
      }
    }
    
    @layer early-layer {
      @mixin --m() {
        color: red;
      }
    }
    
    :root {
      color: pink;
    }
    
    :root {
      @apply --m(); /* inner styles are unlayered */
    }

    Authors can also layer inner styles by writing:

    @layer a {
      @mixin --m() {
        @layer b {
          color: green;
        }
      }
    }
  2. andruud commented on Jun 30, 2025

    @andruud
    MemberAuthor

    But I think mixin rules should also be valid in @layer rules to be able to use cascade layers to resolve name conflicts.

    Ah, yeah, you're right. Damn. I was hoping we could just cleanly state "doesn't work (at all) within such and such", but I guess we do need to be more subtle about it---at least for @layer.

    However the inner styles should not be affected by this I think.

    Hopefully it will still be somewhat explainable to authors that layers affect which mixin definitions "wins", but not the definitions themselves.

  3. romainmenke commented on Jun 30, 2025

    @romainmenke
    Member

    This also raises some questions about the declared layer order.
    Do @layer rules inside mixins affect the layer order?
    Or only when those mixins are applied?

  4. added a commit that references this issue on Jul 4, 2025
  5. 47 remaining items

  6. added
    css-mixins-2@mixin, @contents, @apply, @private
    and removed on Sep 1, 2026
  7. self-assigned this
    on Sep 14, 2026
  8. added a commit that references this issue on Sep 14, 2026
  9. kbabbitt commented on Sep 29, 2026

    @kbabbitt
    Collaborator

    I don't think we need any normative changes for validity of @keyframes. I think it was just cited as an example of "unless otherwise specified, an at-rule is valid at the top level, and in select other circumstances," and the sections I linked to define those other circumstances.

  10. added a commit that references this issue on Oct 1, 2026
  11. added a commit that references this issue on Oct 2, 2026
  12. added a commit that references this issue on Oct 15, 2026
  13. added a commit that references this issue on Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions