-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CSS @custom-media
at-rule to manage media queries
#714
Comments
There don't appear to be any tests in WPT for |
Worth noting that web developers are already using custom media queries en masse, they're just doing it through pre/post processors like SASS, PostCSS, and Lightning CSS. The custom media plugin for PostCSS that replicates the behavior of this spec has 5.7 million weekly downloads on NPM. Implementing would go a long way towards making tools like these optional instead of a mandatory means for writing maintainable CSS. |
It is also the second most viewed readme in our monorepo (after
|
I've added a patch with some tests for WPT: web-platform-tests/wpt#48480 |
Outrageous! Instead of creating new syntax, just restrict the use of CSS variables at the @rules level. |
@gsnedders Is there anything missing or that could be improved about this proposal? |
I'll repeat what I said last time, which is that this feature can be (partially) emulated by style queries (#828). @media (width <= 40em) {
:root { --screen: mobile; }
}
@media (width <= 70em) {
:root { --screen: desktop; }
}
@container style(--screen: mobile) {
/* styles */
}
@container style(--screen: desktop) {
/* styles */
} That's not to say we shouldn't pursue |
@mayank99 I don't think it can really emulate this. I also don't think it can be an equivalent tool for writing and maintaining CSS. You would want autocomplete, hover tooltips, ... |
@romainmenke I totally agree we should have all of these things. That's why I said "partially emulate" and included another clarification note at the end. It's not a proper replacement. |
Description
The Media Queries Level 5 specification introduces the
@custom-media
at-rule, which acts as an alias for complex and repetitive media queries. Instead of hardcoding media query breakpoints across multiple CSS files, developers can declare breakpoint values using@custom-media
rules. These custom media queries can then be reused, streamlining CSS management and improving maintainability.With
@custom-media
, developers can centralize all breakpoint definitions in one place, ensuring consistency across stylesheets. When a breakpoint value needs to change, only the@custom-media
declaration must be updated, and all instances across CSS files are automatically adjusted.Use them as aliases in multiple breakpoints:
Use them for grouping all your breakpoints in one place:
I was hoping my MDN PRs will be merged before Interop 2025, but as it was not merged yet, check the following PRs for more information and code examples:
@custom-media
at-rule mdn/content#35755@custom-media
at-rule mdn/browser-compat-data#24338Specification
Browser Bugs
Additional Signals
Real-World Application: Elementor Case Study
I currently work at Elementor, a popular WordPress page builder powering over 25% of all WordPress websites and ~11% of all websites globally.
We continuously strive to optimize our CSS for millions of websites. Currently, when our users update breakpoint sizes, we need to re-generate multiple CSS files.. With
@custom-media
support across all browsers, we will be able to simplify this by redefining new breakpoint size in a single line of code. This eliminates the need for full CSS regeneration because only the last declared@custom-media
rule is applied when multiple rules share the same name.The Way Forward
Just as Custom CSS Properties revolutionized styling by enabling dynamic values, Custom Media Queries will significantly enhance how developers handle responsive design. By enabling media query reusability and eliminating redundancy,
@custom-media
is poised to be a game changer in the future of CSS development.The text was updated successfully, but these errors were encountered: