Repository navigation
Description
Description
Classically, the assignment of CSS Custom Properties begins at the top-level (or "root") element through the use of :root. This is common usage and allows the values to be inherited down through the DOM tree.
(It is also one of the methods by which the popular IE11 polyfill css-vars-ponyfill works. I realise that IE11 support is being dropped from WordPress Core, but solving the inheritance issue would also allow this ponyfill to work again with no additional effort.)
Gutenberg adds the generated CSS Custom Properties to the body element, and not :root, through applying global-styles using the wp_add_inline_style function. This leads to issues where third-party CSS authors connect the generated properties to their own Custom Properties on :root. The generated properties are not available at the top root level, and so the standard CSS cascade (inheritance) doesn't work.
Recommendation (and request!): apply the global CSS Custom Properties to :root, not body.
Step-by-step reproduction instructions
:root {
/* Theme author's definition */
--my-theme--primary--color: var(--wp--preset--color--red);
}
body {
/* WordPress Core definition */
--wp--preset--color--red: #f00;
}
h1 {
/* This value is noted as undefined*/
color: var(--my-theme--primary--color);
}
Screenshots, screen recording, code snippet
Here's a Codepen example of the code above: https://codepen.io/permanenttourist/pen/oNeYgVP?editors=1100
Environment info
- WordPress 5.8.1
- All modern browsers (and IE11 with ponyfill)
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
Activity
markhowellsmead commentedon Oct 21, 2021
(Aside: I've looked it up, and we're talking about
WP_Theme_JSON::ROOT_BLOCK_SELECTOR(wp-includes/class-wp-theme-json.php).aristath commentedon Nov 1, 2021
Related: #27478
colorful-tones commentedon Nov 1, 2021
I think that "classically" is misleading here. The transformation of usage of CSS Custom Properties has matured (rightly so) in the past few years. There is no classical usage in my mind, and merely an awareness and attempt at defining and shifting common practices.
I actually think it is a great idea to target and keep
bodyfor WP core Custom Properties. It leaves flexibility for theme and plugin authors to be able to leverage:rootto register their own Custom Properties, while still usingbodyto reuse WordPress core's should they choose to. So, let's educate them on how and when to use these different targets and all about the wonderful possibilities of CSS inheritance.markhowellsmead commentedon Nov 1, 2021
I agree that “classically” wasn't the right term to use. Progressive improvement is certainly the right way to go. Educating theme and plugin authors is certainly laudable, but they need to be educated to use CSS as it was intended, and as it's defined by the standards at the W3C. WordPress needs to follow the standards, and not divert from them without an essential reason.
It's definitely important to set the site-wide values as high up the cascade as they can go, in order to provide the maximum level of support. In this case, and according to the W3C definitions, that's the document root object
:root.This is especially important when, as in the case I've shown, the current WordPress Core logic leads to errors and usability problems which individual developers - myself included - have to work around by mutating core logic in every project.
markhowellsmead commentedon Nov 1, 2021
Additional: the problem is that if developers use the
:rootelement for their own definitions, they can't use any of the values and custom properties defined in theme.json (and, hence, on thebodyelement). This is shown in the code example I posted. Properties defined on thebodyelement can't be “inherited” back up to:rootbecause of the hierarchical CSS cascade.colorful-tones commentedon Nov 2, 2021
Correct, and in that case they would want to use the
bodyelement to target their CSS Custom Properties. I'm not seeing the problem.There are no standards, but only recommendations from W3C.
markhowellsmead commentedon Nov 2, 2021
The problem can be worked around, but it would involve every front end developer who ever works on theme and plugin development for WordPress having to recognise a special case. Where they may have to diverge from standard CSS practice to accommodate a system-specific decision.
I know. But deliberately going against the W3C recommendations was how we arrived at the various browser prefixing and cross-browser conflicts which have plagued the industry for so many years. This is a simple drop in the ocean, but I'm still not seeing the need to diverge from a procedure which is recommended and used by so many developers, both within and without the WordPress ecosystem.
Can you clarify which decisions were taken to arrive at the reason to use
bodyand not:root, please? That would help to understand why it has to be implemented in the way that it is.markhowellsmead commentedon Nov 2, 2021
If anyone's following along (or finds this thread), then the code in this Gist will parse the contents of
wp_headand move the definitions frombodyto:root. Thereby allowing the regular CSS cascade behaviour to be used, and to restore the functionality of css-vars-ponyfill for Internet Explorer 11 et. al.markhowellsmead commentedon Nov 3, 2021
Additional note: one result of adding the rules to
:rootand using the technique above to re-assign them is that these rules are then not applied to the basedivin the editor. This needs to be considered when considering any modification to the current status in core.keithdevon commentedon Nov 19, 2021
I agree with @markhowellsmead here.
It looks like this change was made here: #31302 to allow...
It would be great to know what this means and whether there is a way to use
:rootfor custom properties and still allow margin and padding styles onbody(if that was the original problem - it's unclear).@colorful-tones are you able to expand on this? How does using
bodyinstead of:rootimprove the situation for theme and plugin authors? I'm not saying that it doesn't - I just don't understand how it does!Can you start by educating me, please? 😂
webmandesign commentedon Apr 5, 2022
Hey guys. I was thinking that maybe the selector specificity for declaring CSS variables in global styles could be even lowered to
:where(:root)as mentioned in #40042.But if IE11 support with
css-vars-ponyfillis a concern here, maybe in such edge cases one could simply change the selector with code.23 remaining items
nextgenthemes commentedon Mar 15, 2024
Actually, I want to correct myself, you can easily overwrite Bootstraps CSS vars. In fact, WP putting custom vars out on the body makes even more sure vars get overwritten, even if themes styles come later. And I guess that is actually the reasoning for this.
So if you set the SCSS variable
$prefix: 'wp--custom--bs'in Bootstrap. You can then use this, to overwrite any variable put out, and you can also use any CSS variables from BS, you just need to pretend they are all in custom with thebs-prefix.So, not as bad as I thought. In fact, because by default the theme style comes after the inline styles from GB, if I get this right this works specifically because the vars are put out on the
body. I kind of hate to realize this because I have a bad feeling that WP is doing it different from anyone else.My suggestion if it is hard to change now would be to introduce a new
custom-rootsection totheme.jsonwhere you can specifically specify custom variables that are output on:root. Maybe that would be the best of both worlds, so vars for scrollbars and things you need on:rootcan be put there. That however would not fix the OPs issue and I guess I would prefer having to go the extra step to deal with this with multiple stylesheets in the correct order and the normal CSS cascade.Quote from the OP:
The order is wrong for the typical case where you enqueue your theme's stylesheet on
wp_enqueue_scriptsthe styles are actually put out after the GB inline styles. The result is the same, but it's this:But why not do it like this:
And then use
--wp--custom-primaryin your theme directly instead of--my-theme--primary--color?markhowellsmead commentedon Mar 15, 2024
I can see the point you're trying to make, @nextgenthemes, but the order of this specific CSS is irrelevant. The value
--my-theme--primary--coloron the:rootcannot inherit a definition frombody. I've updated the example at https://codepen.io/permanenttourist/pen/oNeYgVP?editors=1100 to show this.That is a use case for small projects and ones with few custom definitions. The projects I build for clients commonly have dozens—if not hundreds—of CSS custom properties for all manner of rules, so it's impractical to leave behind a solution which was introduced to CSS itself, which allows this to work without needing to implement system-specific workarounds.
Some might believe that WordPress can provide a solution which replaces the need for such custom development. That is not a realistic goal. Given that on big projects, there are always requirements for complex animation, layout shifts using CSS Grid across myriad breakpoints, and dynamic script-based interaction, sticking to years-long-established best practices will stop WordPress from being a hindrance.
WordPress encourages developers to use the theme.json solution it provides instead of using commonly-used best practices which have been in place for many years. The Web Standards Project began in 1998 and pushed developers and browser makers into working towards a common goal instead of working on proprietary solutions. It remains essential that WordPress doesn't actively stop W3C Recommendations from working, which it does at the moment by insisting that the top-level element is the
bodyelement and not thehtmlelement.markhowellsmead commentedon Mar 15, 2024
The pull request #42084 was working towards a solution for this, but was closed in 2023 because it was “old”. 🤔
markhowellsmead commentedon Mar 15, 2024
This comment #31302 (comment) also points out that people using JavaScript to interact with the custom properties will encounter problems because of the divergence from established practices and documentation.
nextgenthemes commentedon Mar 15, 2024
@markhowellsmead I said
So you do not need to tell me
I am aware, and I understand everything about this thing now, this was the reason for my long comment explaining it. I have no clue what you are on about W3C again, @colorful-tones already told you that you are wrong.
WP devs never did such thing! I am basically on your side, but this made up stuff is not helping your argument. Using a different element for CSS vars is for sure not a "proprietary solution".
Given 5K issues and how WP and GB works dev works, I would not get my hopes up into this changing any time soon, @ramonjd with the PR self closed in a kind of hilarious way.
Who is "we", the GB dev team? He killed his own work on it because it's "old". This looks like the end of this to me. Now everything is build on top of this bad idea and WP has an insane backwards compat policy, so they will probably just say they can't change it now because there is already so much build on top of it, if they care at all. It could an opt-in switch ...
But if it's just about "many CSS" vars, what is the actual difference besides the forced naming that can probably even changed with code if you declare them in a (S)CSS or a JSON file? Well I know one thing, it's not cached because it's inline in the HTML on every page again and again and that sucks for performance optimization. But WP is going the road of putting out all these chunks of inline stuff, it has positives and negatives ...
fabiankaegy commentedon Mar 15, 2024
@tellthemachines I'm curious whether you have any insights here with your recent work or lowering the specificity/ looking at css layers :)
markhowellsmead commentedon Mar 15, 2024
@nextgenthemes I'm sorry to read that you're so negative about the potential for improvement. I respect other people's opinions. I've referenced the W3C Recommendations throughout because of their long-standing place at the centre of platform-independent HTML and CSS development since the 1990s.
My comment about WordPress using
bodyas the top-level element is based on the fact that all of the CSS properties created by core are placed on this element. I certainly have not "made it up”. A review and improvement of the basis code is the purpose of this Github issue. (Inlining CSS is a separate topic which is outside the scope of this specific discussion.)colorful-tones commentedon Mar 15, 2024
@nextgenthemes, there is no wrong or correct, and we’re discussing this communally to reach a consensus by stating and understanding the nuances.
I appreciate @markhowellsmead opening this issue and diligently providing clarity and details to push the conversation forward. As well as the many other folks that have chimed in.
I worry that the proposed changes may have a dramatic impact, but I have little understanding of what the changes would look like. I would be happy to champion thorough solution testing and solicit community feedback if someone were to reopen or put forward a pull request. Perhaps we could target this for the next WordPress 6.6 release, as a proposed item. 🤔
markhowellsmead commentedon Mar 15, 2024
If there is a change made here, then it will probably have a notable impact, which stakeholders need to assess. It's an important decision which needs to be taken by consensus, not just based on my opinion or by the individual opinions pro and contra. Perhaps @ramonjd can add his opinions to the discussion, and consider re-opening his pull request if he feels that it remains relevant (and, specifically, technically viable based on the current codebase).
nextgenthemes commentedon Mar 17, 2024
TLTR:
@markhowellsmead
I told you that I am for this change, here you go making stuff up again. You act as if I am against this improvement, never did I say that. You're calling this "proprietary" is a stretch if I ever heard one. And I made perfectly clear that I do understand this issue multiple times, there is no point for repeating yourself and explaining it to me like I do not understand what your reasons are why you opened this. My guess spamming around here won't help either.
@colorful-tones
Based on what I read above, he claimed that putting it on
:rootis some kind of W3C standard. Then you corrected him that said the thing he cited was not an official W3C standard.Quoting you quoting their stuff:
So you are in fact right and he is wrong! Sorry, I am not playing this bullshit "understanding nuances" game. You know you are right, and I know it. But this forced niceness and pretending things that are not true is trendy these days. Fits perfectly, who puts his pronouns on his profile. ROFL. And after he was corrected, he brought up W3C again, completely ignoring the fact you stated. That was my issue with it.
People hate facts so much on the internet, you are not supposed to point them out, you are deemed "negative" for stating them and calling people out for being wrong. That even people who did the calling out come up with pretentious bullshit, it's hilarious. It's the absolute worst of Reddit, Hacker News also suffers from this, and now It's even in GitHub issues. It's so 2024
</rant>Are there filters for inline CSS, because at least we should have those. With a filter, we should just be able to
preg_replacethis in unofficially at least??annezazu commentedon Mar 17, 2024
Hey folks. Let's please center the discussion on the issue at hand. A lot of folks were tagged in this when the
@outreachhandle was evoked and it's our collective responsibility to be mindful of that. I'm all for strong opinions and have them myself but we're moving out of that space of productive dialogue with these last few exchanges.As for the PR being closed out, to add some context, it's encouraged that contributors working on the project regularly review their open PRs to ensure that those who might want to contribute with code (either with reviews, testing, or code) have a higher chance of jumping into active, relevant work. This is also so that those relying on a solution can understand whether something is in progress. This is tricky to get right as noted by the 1.1K pull requests and I appreciate the proactiveness there to close it out.
ramonjd commentedon Mar 17, 2024
I recently revisited the body/:root conundrum in an unrelated PR.
I originally wanted to flip "body" to ":root" across the codebase, but quickly discovered that CSS declarations in the body tag are there in order that theme.json styles can overwrite browser defaults such as margin. As confirmed here, I believe that was the intention behind #31302
CSS custom properties, on the other hand, may still be safely contained under
:rootorhtml. #42084 demonstrated this to some extent, but would need a far bit of testing I think.I'm happy to reopen and rebase that PR if folks are willing to help test it?
To add context to the citation and comments, #42084 was "old" in Github PR terms, since the codebase, in particular the theme JSON class, had changed a lot.
More relevant was that the PR went many months without interest from testers/reviewers. Furthermore, other priorities arose. It was closed to indicate that that specific PR was no longer "in progress", not because the topic was irrelevant or defunct. Thanks to @annezazu, who described it best above.
I recognize and appreciate @markhowellsmead's persistence and support on this issue.
It costs nothing to reopen a PR besides a bit of rebase tomfoolery 😄 I'm still convinced it's worth a shot.
It'd be great to get it widely tested as well. I'll make sure to promote it as much as I can among other core devs.
Cheers!
tellthemachines commentedon Mar 18, 2024
There shouldn't be any specificity-related consequences to this change either way.
:rootdoes have higher specificity thanhtml, but given it's the top-level element it would still be possible to override the values of properties lower down in the tree if needed.markhowellsmead commentedon Mar 18, 2024
Thanks for taking the time to look at this @ramonjd and @tellthemachines.
I'm appreciative of the work you're doing @ramonjd and thanks to the linked issues and pull requests, I have a better understanding that the topic of applying CSS selector rules—not custom properties—to the document root in order to resolve other issues has become particularly complex. I'll be honest and say that I can't appreciate the finer details without a deep dive, which I'd be happy to schedule in if you think it'd be appropriate.
I can now understand the use case of applying the top-level rules (background, margins etc.) from theme.json to the
bodyinstead of the:root/htmlelement thanks to #59809. This was the query which @keithdevon and I had a long while ago, when we were unsure where the connection lay between the custom properties and thebodymargins.However, the specific point in this particular issue relates to the assignment of CSS custom properties to what I believe is the “correct” root element. I don't feel that the custom properties and the selector-based rules necessarily need to be applied to the same element, though.