-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Theme.json: Margin for elements is overridden by layout margin #64785
Comments
As I understand it, the CSS specificity of element-level selectors was |
Thanks for writing up the issue @t-hamano 👍
The example theme.json provided adds heading element styles not styles for the Heading block as referred to here and in the following expected behaviour list. This doesn't mean there isn't a problem but it is an important distinction to make for others coming to this issue.
Agreed. That's the only workaround that springs to mind for me also. I'm not a layout support expert so perhaps @andrewserong could correct me here. My understanding is that global block type styles should be able to override layout margins. It is less clear, to me at least, whether elements should be able to do the same and whether it was intended behaviour previously. Backwards compatibility is key, so if there is a way to restore that while keeping all expected behaviour, count me in. In case it helps there is some extra history and context here.
Unfortunately, the issue in #63345 could be any top level element not just |
That's my understanding, too. As far as I understand, the work that went in to adjusting layout and global styles rules in order to support margin rules overriding layout was specifically about that happening at the block level rather than the element level. While it might have worked previously to do so at the element level, I don't believe that was the original intention. Arguably, depending on the theme, folks might expect that a layout container's default block spacing should override any element margins that might be set. I think this might make it tricky to pursue a fix, as it doesn't seem clear to me that changing it here would always be perceived as a fix 🤔
Yes, I'd argue that defining margins on the heading block instead of elements is probably the more correct way to achieve this kind of styling in That's just my 2 cents, though. Thanks for the ping! |
Thank you everyone for the detailed explanations. One idea: what do you think about an approach where only layout-related styles have a specificity of As an example, let me provide the following theme.json. theme.json{
"version": 3,
"settings": {
"appearanceTools": true,
"layout": {
"contentSize": "840px",
"wideSize": "1100px"
}
},
"styles": {
"elements": {
"heading": {
"spacing": {
"margin": {
"top": "3em"
}
}
},
"link": {
"typography": {
"textDecoration": "underline"
}
}
}
}
} This definition will output the following styles: // 0-0-1
h1,
h2,
h3,
h4,
h5,
h6 {
margin-top: 3em;
}
// 0-0-1
a:where(:not(.wp-element-button)) {
text-decoration: underline;
} I'm wondering if we could change these styles to the following:
Are there any potential problems with this approach? |
Good question. 🤔 From my perspective two potential problems might be:
I might be overly cautious here, but I don't feel certain that upping the specificity of margin rules for elements is necessarily the right way to go. Conceptually, do we think layout rules should win out over element styles, or should element styles win out over layout? To put it another way — why would a theme prefer to use margin rules on the heading element instead of the heading block? I don't mean to be a blocker here, but just want to understand what the overall problem is that we're trying to solve, and whether it's a bug we need to address, or how we guide theme developers to the appropriate rules for the use case 🙂 |
I think Andrew captures all my initial thoughts nicely 👍
+1 |
I have the same problem (hybrid theme), and it freezes the margins of all elements (titles, paragraphs, etc...) at the value of blockGap. When you've got a few pages to correct, it's not a problem, but when you've got hundreds, it's very problematic. I don't understand why blockGap controls the vertical alignment of all page elements. In my opinion, this should only apply to level 1 groups (not elements of a group or elements of a group within a group). "blockGap": null, with a schema https://schemas.wp.org/wp/6.5/theme.json in version 2, even though it tells me it expects a string value, this has the effect of completely disabling CSS writing of .main-container {
> .wp-block-group {
padding-top: var(--wp--custom--block-gap);
padding-bottom: var(--wp--custom--block-gap);
}
} As a web agency, we can't afford to take the risk of editing hundreds of pages to correct the margins with each update. So now I'm going to work without a default blockGap, hoping that the css rules won't come back on the next update with the null value declared in the theme.json... |
When developing a theme, I might define a style like this to allow more space above the Heading
blockelement, for example:What I expect from this definition is the following behavior:
1.5em
.3em
.This worked as expected up until WP6.5 because the margin for the Heading block has a higher CSS specificity than the margin for the constrained layout:
However, in WP 6.6.1, the CSS selectors generated from the element level are less specific, so the margin of Heading block is no longer applied:
The only solution I know is to define margin on blocks instead of elements:
The text was updated successfully, but these errors were encountered: