-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Allow authors to apply new css features (like cascade layers) while linking stylesheets #7540
Comments
cc @whatwg/css |
This still fetches bar.css, right? or will this |
WordPress is another use case where applying CSS cascade layers via linked stylesheets could greatly accelerate the use of layers across the web. /* Imagine if WordPress core could separate all of its enqueued styles into logical layers. */
@layer core, themes, plugins; Why CSS Conflicts Are So Prevalent in WordPressAs someone who writes CSS for a distributed WordPress plugin, I cannot predict what other themes, plugins, or frameworks will add styles alongside my own. As a result, CSS specificity and source-order conflicts have long been a major pain for WordPress theme and plugin developers who write clean CSS, only to have their styles lose out to greedier selectors in other themes and plugins. The Need for Widespread Adoption of Layers in WordPressThe success of CSS cascade layers in an ecosystem like WordPress depends on whether developers can reliably count on other themes and plugins to also use layers in a logical manner. If not, un-layered styles will continue to trump the responsible use of layered styles, and even those developers who are ready to adopt layers will feel the need to continue using high-specificity selectors of their own to defend against conflicts. But what if WordPress core could logically assign layers to the styles that are enqueued through its content management system even if the styles being loaded do not use layers internally? This is where assigning layers via Driving Adoption Through WordPress CoreIn WordPress development, the recommended method for enqueuing styles is to use the PHP function If it was possible to assign a layer with a In the past, we have seen that driving change through WordPress core has greatly accelerated the adoption of new browser features such as responsive images and the use of |
In the draft PR @domenic asks:
I suppose that's a question for @emilio, @lilles, @xiaochengh… someone from Webkit? |
@anttijk / @heycam / @nt1m would probably be reasonable points of contact from WebKit. So having import conditions ( But I'm not sure how that ties to cascade layers or |
To clarify, I was asking this question because this PR changes the behavior for |
If Regarding the transitional state: if |
Ah yeah, that should work, I thought the meta/source bits were also about layer= attributes |
Sorry, let me fill in some missing context. (This was discussed previously in a thread I opened on the wrong repo.) The proposal starts with a new The problem is with older browsers, and the desired fallback behavior when the The only way we know to avoid that is by having authors write a currently-invalid If we don't provide a solution that hides layered styles from old browsers, authors will need to fake that behavior, likely using a similar syntax and manually updating the But if we take this approach, the solution becomes re-usable for similar situations in the future. For example, the CSSWG is already working on an |
I see, in the @import rule syntax definition, |
I think it's totally reasonable to have an approach like this available for future, similar situations. I just am not sure it's strictly necessary for detecting cascade layer support, since engines will need to ship at least one of { |
This is not true, for what is worth: The stylesheet is loaded, but not applied because the media query doesn't match. It still appears in document.styleSheets, etc, and wastes network traffic. As far as I know this is a compat requirement too, because lots of old "load styles async" solutions use it: (It might be still worth doing, just wanted to point out the trade off might be a bit different) |
@emilio Sorry, yes - loaded but not applied. That still has the intended behavior in this case. @heycam The point of using |
Of course, that makes sense. Thanks for spelling it out. In terms of the mechanics of allowing |
As far as I understand the conversations/resolutions so far:
But (as far as I know) there is not a |
While it's true that there will be "dramatically unexpected & unpredictable consequences" in browsers that don't support it, is it really a good solution to just not include the styles at all? Wouldn't it be better if browsers just added the "long-term syntax" as quickly as possible and developers waited until they're happy with the level of browser support to use it? It doesn't feel like there's any great solution for old browsers so I don't see why it's worthwhile to cram in new syntax like this. |
Yes, it is best if authors can choose to only load layer-based styles when the This isn't a one-off syntax for layers, and it isn't particularly being 'crammed' anywhere. This exact syntax already exists in CSS, and is available anywhere CSS stylesheets are being imported - including the fact that supports & media queries are allowed side-by-side in the same syntax. This proposal brings HTML links more closely in line with the current functionality of CSS, using exactly the same syntax as CSS (but in an attribute), and will be useful for authors any time they want to do feature-support detection, now and into the future. |
Re #7540 (comment): Extending the |
|
This issue seems to have stalled. Is there extra info needed to get the ball rolling again? By the looks of it, I think we all agree that adding |
This has stalled on my end for a few reasons:
I would love to see this move forward. I don't feel like I'm in a position to make that happen on my own at this point. I would be happy to pick it up again with some help on the various specs, and some more clarity that this is an approach browsers will implement. |
I've received some pushback to the Maybe the failed closed requirement is not a hard requirement, since authors can use long-term syntax for layer: <link rel="stylesheet" layer="foo" href="bar.css"> transition period for layer: <style>
@import url(bar.css) layer(reset);
</style> long-term syntax for supports: <link rel="stylesheet" supports="at-rule(@scope)" href="bar.css"> transition period for supports: <style>
@import url(bar.css) supports(at-rule(@scope));
</style> |
The resistance I have for putting So I was more drawn the idea of using the existing working thing ( |
If media queries and supports are distinct concepts in CSS I'm inclined to agree we should not group them here. Bit surprising nobody pushed back on that during the CSS WG discussion, but maybe not everyone was paying attention. In that case we should add |
I was paying attention to this at the joint discussion and IIRC I raised this point (perhaps not loudly enough). but I didn't have a good solution for backwards compat at the time.
Yes, and while there is a legacy period relevant CSS can be wrapped in |
See w3c/csswg-drafts#9375 (comment) New proposal is to add a <link rel="stylesheet" condition="supports(at-rule(@scope))" href="bar.css"> Pro: easier to add new import conditions |
We'd also need to specify how they interact right? If media says to load but condition says not to, what wins? |
I'd suggest that I didn't see this mentioned, but isn't it a problem that browsers that don't support the |
They can be combined right?
This could be roughly equivalent to : @media (min-width: 300px) {
@supports (at-rule(@scope)) {
@media (min-height: 400px) {
/* ... * /
}
}
} It would need to be defined exactly how they combine, in which order and how it affects cascade layers. But I don't think one attribute should effectively disable the other. |
+1 to this.
@noamr unless I'm misunderstanding your question: you could mirror whatever e.g. if a browser erroneously loads |
Sounds good to me as a temporary mitigation! |
My original suggestion was to basically expand media="supports(…) prefers(…)"
media="media(…)"
media="dayofweek(…)" 🤪 But because condition="supports(…) prefers(…)"
condition="media(…)" That rename is entirely optional though, as That way:
Not a fan of this. Putting everything into |
If we were ok with just putting everything in the media attribute, we wouldn't be having this conversation in the first place. That was the original proposal, and it was rejected. The simpler short-term polyfill that came up in the CSSWG call today is that authors can add a reliably-failing media attribute along side the condition attribute, and then remove the media attr for browsers that support condition. This could be made even simpler (as proposed by @fantasai on the CSSWG telecon) by adding an optional <link rel="stylesheet" media="use-condition" condition="supports(at-rule(@scope))" href="bar.css"> |
I came to properly understand the reasons behind this during the meeting and am now OK with not being able to simply rename it now, nor in the future. So yes, |
I’m not sure, but would |
FWIW I also prefer |
Either of these would make sense to me:
|
I agree with @romainmenke. I do like the |
|
No, because links already have media (and soon supports). |
These are valid (as specified, not everything is implemented): @media (width: 300px) {}
@supports (display: grid) {}
@when media(width: 300px) {}
@when supports(display: grid) {}
@import "foo.css" (width: 300px);
@import "foo.css" layer(foo) supports(display: grid) (width: 300px); These are not valid (today): @when (width: 300px) {}
@import "foo.css" media(width: 300px); I think that I do think it is important that authors can intuitively copy/paste conditions between CSS and HTML. So if |
Yea, but it's not a new type of condition.
Exactly. It's a CSS syntax feature.
What makes this important? Those expressive conditionals seem to be a CSS syntax feature. What should be copy-pastable IMO is imports, and for that supports/media is enough. |
I am not fully sure what you mean, maybe we are saying the same thing :) I like how these align: <link rel="stylesheet" href="bar.css" condition="supports(display: grid) screen"> @import "bar.css" supports(display: grid) screen; I am not convinced of this: <link rel="stylesheet" href="bar.css" when="supports(display: grid) and media(screen)"> @import "bar.css" supports(display: grid) screen; But I think authors will get it. I don't like this: <link rel="stylesheet" href="bar.css" when="supports(display: grid) screen"> @import "bar.css" supports(display: grid) screen; Because (Keeping in mind that |
Not exactly. I'm saying that
One thing to remember is that in terms of imports/links, |
Thinking about this again, I'm not sure it's a good idea to introduce duplication of MQ for It seems better to separate import conditions into two parts, one for MQ and one for everything else, and let the new attribute for The syntax as compared to |
Given the CSSWG resolution "RESOLVED: the WG recommends having an attribute to enable @scope on the HTML link element", it seems worth asking, are the WHATWG okay with having separate layer and scope attributes on Apologies if that question is answered up thread but having read through I can't see a clear answer. |
I think the leaning has been towards individual attributes, which I support. I also think the much-discussed questions about providing support conditions (whatever shape that takes) should be split into a separate issue - and not block these new |
I opened this issue in the wrong repo initially, so summarizing that conversation here:
The problem
The CSSWG recently added a feature to CSS called Cascade Layers. It allows authors to more explicitly manage the cascade behavior of styles from different parts of a design system. That can be done using
@layer
blocks in the code, but in many cases authors will want to wrap entire (sometimes third-party) stylesheets into layers at the time they are linked/imported. The new CSS spec makes that possible with additinoal@import
syntax - but the@import
rules tends to be less performant than the HTML<link>
tag. For now, the best authors can do is use@import
inside HTML<style>
:Use cases
A few variations on the layer use-case include:
Cascade Layers are already supported in preview/beta versions of Safari, Chrome, and Firefox - and we expect them to appear in stable releases over the next few months. But the lack of HTML link support has been one of the largest points of concern/feedback from authors, and one that we can't address from the CSSWG.
Constraints
At first glance it may seem like this could be solved with a new
layer
attribute on the<link>
tag, but it would cause problems for old browsers that simply ignore the attribute, and continue to the load the stylesheets without any layering. Whatever solution we land on needs to allow authors more control over the fallback path for old browsers.It might also be good to plan for this sort of situation down the road, if we're able to find a solution that can potentially be used again for new features in the future?
[In the other thread we also encountered the constraint that extending
media
should not go beyond conditions that result in a boolean - so we're in favor of addingsupport()
syntax to the existing attribute, but not in favor of addinglayer()
to the same attribute.](I'm sure I missed some useful information here, so feel free to ask questions!)
Proposed Solution (from discussion)
The proposed spec changes would be:
media
attribute to support all 'import conditions' (I suppose we could clarify that term as a part of the CSS syntax to be referenced)layer
attribute…The long-term syntax for adding a layer is:
And the transitional syntax for handling browser support is:
The text was updated successfully, but these errors were encountered: