Skip to content

CSS Custom Properties on BODY lead to DOM inheritance issues #35840

Closed

Description

@markhowellsmead

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

  1. markhowellsmead commented on Oct 21, 2021

    @markhowellsmead
    Author

    (Aside: I've looked it up, and we're talking about WP_Theme_JSON::ROOT_BLOCK_SELECTOR (wp-includes/class-wp-theme-json.php).

  2. added
    [Feature] Themes
    [Type] Bug
    CSS Styling
    on Oct 25, 2021
  3. aristath commented on Nov 1, 2021

    @aristath
    Member

    Related: #27478

  4. colorful-tones commented on Nov 1, 2021

    @colorful-tones
    Member

    Classically, the assignment of CSS Custom Properties begins at the top-level (or "root") element

    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 body for WP core Custom Properties. It leaves flexibility for theme and plugin authors to be able to leverage :root to register their own Custom Properties, while still using body to 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.

  5. markhowellsmead commented on Nov 1, 2021

    @markhowellsmead
    Author

    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.

  6. markhowellsmead commented on Nov 1, 2021

    @markhowellsmead
    Author

    I actually think it is a great idea to target and keep body for WP core Custom Properties. It leaves flexibility for theme and plugin authors to be able to leverage :root to register their own Custom Properties, while still using body to reuse WordPress core's should they choose to.

    Additional: the problem is that if developers use the :root element for their own definitions, they can't use any of the values and custom properties defined in theme.json (and, hence, on the body element). This is shown in the code example I posted. Properties defined on the body element can't be “inherited” back up to :root because of the hierarchical CSS cascade.

  7. colorful-tones commented on Nov 2, 2021

    @colorful-tones
    Member

    Additional: the problem is that if developers use the :root element for their own definitions, they can't use any of the values and custom properties defined in theme.json (and, hence, on the body element).

    Correct, and in that case they would want to use the body element to target their CSS Custom Properties. I'm not seeing the problem.

    WordPress needs to follow the standards, and not divert from them without an essential reason.

    There are no standards, but only recommendations from W3C.

  8. markhowellsmead commented on Nov 2, 2021

    @markhowellsmead
    Author

    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.

    There are no standards, but only recommendations from W3C.

    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 body and not :root, please? That would help to understand why it has to be implemented in the way that it is.

  9. markhowellsmead commented on Nov 2, 2021

    @markhowellsmead
    Author

    If anyone's following along (or finds this thread), then the code in this Gist will parse the contents of wp_head and move the definitions from body to :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.

  10. markhowellsmead commented on Nov 3, 2021

    @markhowellsmead
    Author

    Additional note: one result of adding the rules to :root and using the technique above to re-assign them is that these rules are then not applied to the base div in the editor. This needs to be considered when considering any modification to the current status in core.

  11. keithdevon commented on Nov 19, 2021

    @keithdevon

    I agree with @markhowellsmead here.

    It looks like this change was made here: #31302 to allow...

    allow for things like margin and padding

    It would be great to know what this means and whether there is a way to use :root for custom properties and still allow margin and padding styles on body (if that was the original problem - it's unclear).

    I actually think it is a great idea to target and keep body for WP core Custom Properties. It leaves flexibility for theme and plugin authors to be able to leverage :root to register their own Custom Properties, while still using body to reuse WordPress core's should they choose to.

    @colorful-tones are you able to expand on this? How does using body instead of :root improve the situation for theme and plugin authors? I'm not saying that it doesn't - I just don't understand how it does!

    So, let's educate them on how and when to use these different targets and all about the wonderful possibilities of CSS inheritance.

    Can you start by educating me, please? 😂

  12. added
    Global Styles
    on Apr 5, 2022
  13. webmandesign commented on Apr 5, 2022

    @webmandesign
    Contributor

    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-ponyfill is a concern here, maybe in such edge cases one could simply change the selector with code.

  14. 23 remaining items

  15. nextgenthemes commented on Mar 15, 2024

    @nextgenthemes

    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 the bs- prefix.

    		"custom": {
    			"bs-body-bg": "green",
    		}
    

    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-root section to theme.json where 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 :root can 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:

    :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);
    }
    

    The order is wrong for the typical case where you enqueue your theme's stylesheet on wp_enqueue_scripts the styles are actually put out after the GB inline styles. The result is the same, but it's this:

    /* WordPress Core definitions */
    body {
        --wp--preset--color--red: #f00;
    }
    
    /* Also WordPress Core definition right? */
    h1 {
        /* This value is noted as undefined*/
        color: var(--my-theme--primary--color);
    }
    
    /* Theme author's definitions */
    :root {
        --my-theme--primary--color: var(--wp--preset--color--red);
    }
    

    But why not do it like this:

    		"custom": {
    			"primary-color": "var(--wp--preset--color--red)",
    		}
    

    And then use --wp--custom-primary in your theme directly instead of --my-theme--primary--color ?

  16. markhowellsmead commented on Mar 15, 2024

    @markhowellsmead
    Author

    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--color on the :root cannot inherit a definition from body. I've updated the example at https://codepen.io/permanenttourist/pen/oNeYgVP?editors=1100 to show this.

    But why not […] use --wp--custom-primary in your theme directly instead of --my-theme--primary--color?

    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 body element and not the html element.

  17. markhowellsmead commented on Mar 15, 2024

    @markhowellsmead
    Author

    The pull request #42084 was working towards a solution for this, but was closed in 2023 because it was “old”. 🤔

  18. markhowellsmead commented on Mar 15, 2024

    @markhowellsmead
    Author

    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.

  19. nextgenthemes commented on Mar 15, 2024

    @nextgenthemes

    @markhowellsmead I said

    The result is the same,

    So you do not need to tell me

    but the order of this specific CSS is irrelevant.

    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.

    by insisting that the top-level element is the body.

    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.

    This PR is old. If the idea is still valid we can open a new issue

    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 ...

  20. fabiankaegy commented on Mar 15, 2024

    @fabiankaegy
    Member

    @tellthemachines I'm curious whether you have any insights here with your recent work or lowering the specificity/ looking at css layers :)

  21. markhowellsmead commented on Mar 15, 2024

    @markhowellsmead
    Author

    @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 body as 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.)

  22. colorful-tones commented on Mar 15, 2024

    @colorful-tones
    Member

    @colorful-tones already told you that you are wrong.

    @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. 🤔

  23. markhowellsmead commented on Mar 15, 2024

    @markhowellsmead
    Author

    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).

  24. nextgenthemes commented on Mar 17, 2024

    @nextgenthemes

    TLTR:

    • Not a WTC standard
    • Good and common practice
    • I am all for it

    @markhowellsmead

    I'm sorry to read that you're so negative about the potential for improvement.

    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

    @nextgenthemes, there is no wrong or correct, and we’re discussing this communally to reach a consensus by stating and understanding the nuances.

    Based on what I read above, he claimed that putting it on :root is 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:

    These documents MUST NOT be cited as W3C standards and may or may not become W3C standards.

    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_replace this in unofficially at least??

  25. annezazu commented on Mar 17, 2024

    @annezazu
    Contributor

    Hey folks. Let's please center the discussion on the issue at hand. A lot of folks were tagged in this when the @outreach handle 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.

  26. ramonjd commented on Mar 17, 2024

    @ramonjd
    Member

    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 :root or html.  #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?

    The pull request #42084 was working towards a solution for this, but was closed in 2023 because it was “old”.
    @ramonjd with the PR self closed in a kind of hilarious way.
    He killed his own work on it because it's "old". This looks like the end of this to me.

    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.

    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).

    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!

  27. tellthemachines commented on Mar 18, 2024

    @tellthemachines
    Contributor

    @tellthemachines I'm curious whether you have any insights here with your recent work or lowering the specificity/ looking at css layers :)

    There shouldn't be any specificity-related consequences to this change either way. :root does have higher specificity than html, 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.

  28. markhowellsmead commented on Mar 18, 2024

    @markhowellsmead
    Author

    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 body instead of the :root/html element 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 the body margins.

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CSS StylingGlobal Styles[Feature] Themes[Type] Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions