-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Layout: layout styles overriding block axial margin values in theme.json #43404
Comments
Thanks for writing this up! For a little more context, back in #42518 I had a go at removing the So, if memory serves me correctly, that kind of suggests to potential (and different) directions forward to come up with a solution for it.
I think there's probably a bit of a balancing act in ensuring that Layout rules "just work", while also allowing all other cases of blocks (either in theme.json or at the individual level in post content) to override things where appropriate, and margin appears to be a pretty tricky area 😅 Curious to hear if anyone has thoughts about the ideal relationship between all these styles / how Layout could play nicely with its style neighbours 🤔 |
Just an update to say I haven't forgotten about this one! I've opened up a fresh draft PR (#45927 — it currently does not work) to try to dig into the issue of specificity of layout styles. Even with the reduction in specificity, it still doesn't work properly, so I'm wondering if there might be more to it than just specificity (for example, if the order in which global styles is output from base layout versus block-level margin rules is also a factor). I might not have much time immediately to dig into this further (I'm currently focusing on other Phase 2 tasks), but hopefully at least having a PR up will help nudge along the investigation. |
Maybe I am missing something, but what about using :where(body .is-layout-constrained > * + *) {
margin-block-start: 1.5rem;
margin-block-end: 0;
}
:where(body .is-layout-constrained > *) {
margin-block-start: 0;
margin-block-end: 0;
} |
Since this is a known issue and the control basically does nothing it's super misleading to a user, surely the global margin control should have been disabled until it's fixed? @t-hamano your |
Interesting idea! Thanks for the suggestion — it'd be worth trying out, but we'd need to be careful to look at blocks' default margin rules, as the layout rules need to override any block default margins, but not override block margins set at the block level in global styles, so it can be a bit tricky to work with there. |
I tried the
The following is the code that was changed in the test. In practice, we will need to consider all layouts and various cases, but this may be one solution. Diffdiff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php
index 9acef6006d..ac12447f1b 100644
--- a/lib/class-wp-theme-json-gutenberg.php
+++ b/lib/class-wp-theme-json-gutenberg.php
@@ -1249,7 +1249,7 @@ class WP_Theme_JSON_Gutenberg {
$spacing_rule['selector']
);
} else {
- $format = static::ROOT_BLOCK_SELECTOR === $selector ? '%s .%s%s' : '%s.%s%s';
+ $format = static::ROOT_BLOCK_SELECTOR === $selector ? ':where(%s .%s%s)' : '%s.%s%s';
$layout_selector = sprintf(
$format,
$selector,
diff --git a/lib/theme.json b/lib/theme.json
index 6221873a03..a16346945d 100644
--- a/lib/theme.json
+++ b/lib/theme.json
@@ -299,12 +299,24 @@
"margin-block-end": "0"
}
},
+ {
+ "selector": " > *:first-child",
+ "rules": {
+ "margin-block-start": "0!important"
+ }
+ },
{
"selector": " > * + *",
"rules": {
"margin-block-start": null,
"margin-block-end": "0"
}
+ },
+ {
+ "selector": " > *:last-child",
+ "rules": {
+ "margin-block-end": "0!important"
+ }
}
]
}, |
Thanks for diving in further @t-hamano! Building on your idea there, I'm wondering if there's potentially another path forward that could avoid having to add the I haven't tried this yet, but I'm imagining something like:
What do you think of that kind of direction as an idea? |
This may be a good approach! This could be achieved by simply adding new output rules without changing the existing output logic. |
I've had a go at this and opened up a draft PR over in #47399 — happy for any feedback or ideas if anyone has time to take a look 🙂 |
I have found two topics related to this issue, both of which are relevant to this issue. #47399 may solve these problems as well. |
Related discussion about |
Closing this issue now that #47858 is merged. |
Possibly related to:
Description
Blocks with top or bottom margin values specified in theme.json will have those values overwritten by flow layout CSS.
Layouts can specify CSS in theme.json.
These layouts are printed to a stylesheet here: https://github.com/WordPress/gutenberg/blob/c76c87e9f1ec22578436d8e24969a29c568c9cd0/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php#L1369-L1368
The flow layout styles
body .is-layout-flow > * + *
andbody .is-layout-flow > *
containmargin-block-start
andmargin-block-end
declarations and have a specificity score of 0,1,1.These rules are overwriting the
.wp-block-{ $blockName }
rule.Which should have precedence in this case?
Context: #43365 (comment)
Step-by-step reproduction instructions
In a block theme create a post with the following HTML containing heading blocks:
In your theme.json give heading blocks a top and bottom margin:
What I expected
That the heading, both the heading in the group block and the heading outside, had a top and bottom margin equal to that defined in theme.json.
What happened
The wrapper's layout spacing rules take precedence.
Local expert @andrewserong offered the following advice:
Screenshots, screen recording, code snippet
No response
Environment info
Gutenberg 13.9
WP 6.0
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
The text was updated successfully, but these errors were encountered: