Re: [csswg-drafts] Defining `@custom-media` inside conditional at-rules (#12536)

Yes, almost by definition anything has to be allowed inside of "basic" conditional rules, the ones we can use in an import, *because* the stylesheet can be loaded by a conditional import. I think we'd need a super strong reason to violate that, but I think that doesn't apply here. The same applies to cascade layers.

The similarity to #12417 is actually very deep; it's basically the exact same problem, where a definition can theoretically affect what other definitions exist, at the stylesheet-parsing level. It's also, however, closely related to the custom env() issue, where we want env() to be usable in `@media` but also want `@env` to be usable inside of conditional stylesheets, and even directly inside of @media rules.

I feel like custom-media actually falls closer to env here, as we similarly have a JS API that can take up the slack of defining "truly global" values if we need. So I'd prefer we solved it [the same way](https://drafts.csswg.org/css-mixins/#env-rule), and make the `@custom-media` rule *lexically scoped*. Standard last-wins behavior if there are multiple instances of a given name in a lexical scope, but each name only applies within the scope of the sheet/rule they're defined in, and nested scopes unless there's another definition there. The JS API, then, would be used to define custom medias that work across stylesheets.

This avoids all issues of circularity, or of values changing across the execution of a page except in well-defined (and I think understandable) nesting ways. The downside is that it does lean on JS for some use-cases, but I think there's enough useful cases that are ok with just using a custom MQ within the same sheet it's defined that we're probably okay.

-- 
GitHub Notification of comment by tabatkins
Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/12536#issuecomment-3134089719 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Tuesday, 29 July 2025 21:11:20 UTC