Backend vs frontend discrepency using alignment (left/right) if theme.json is active #33385
Comments
@onetrev thanks for reporting this! I'm having a hard time following the exact problem you're describing. I just tested with WP 5.8 and the latest of Gutenberg. I can see that alignment works correctly. Can you share a video or an additional set of screenshots to clarify? |
Hey @annezazu thanks for the follow up. I realise now I forgot to include a key detail. This appears as soon as you add layout -> contentSize to your NOTE: in the video I'm now running WP 5.8, with no Gutenberg plugin, as since my original post WP 5.8 has been released. |
@annezazu This is a wider issue with how Gutenberg handles alignment styles, and it's not only present in hybrid (PHP themes with Traditionally, themes have had a container ( If themes want to use the new way of adding controlling the block width, the related dev 5.8 note recommends this approach: .entry-content > * {
max-width: 800px;
margin-left: auto !important;
margin-right: auto !important;
}
.entry-content > .alignwide {
max-width: 1000px;
}
.entry-content > .alignfull {
max-width: none;
}
.entry-content > .alignleft {
float: left;
margin-right: 2em;
}
.entry-content > .alignright {
float: right;
margin-right: 2em;
} So the content container doesn't have a fixed width. Instead it is a 100% and the blocks inside are individually restricted in width. And this approach also works. Let's look at why. This is a floated image on the frontend. This is the markup: <div class="wp-block-image">
<figure class="alignleft">
<img src="http://example.com/wp-content/uploads/image-alignment-150x150.jpg" alt="Image Alignment 150x150" class="wp-image-904">
</figure>
</div> This image stays inside of the content width because the However in the editor, the markup is the following: <div class="wp-block" data-align="left">
<figure>
<div>
<img />
</div>
</figure>
</div> So the Therefore the block floats left to its container, which is the editor. And therefore "sticks out" of the content area. This is also an issue in block-based themes, as shown with TT1: This bug is present in the post editor and template editor for hybrid themes, and the site editor for block-based themes. For me this is worth getting into 5.8.1. Why? Because there is no workaround here. You can fix the post editor. Assuming a content size of 520 pixels, you can use calculated margins in the editor stylesheet: .block-editor-block-list__layout.is-root-container > [data-align="left"] {
margin-left: calc( ( 100% - 520px ) / 2 ) !important;
} To explain: you take the full width, subtract the content size, and divide by two. That gives the margin to fit the floated element back in. All in all, this isn't great. However it gets worse. If you open the Template Editor, this is what you see as generated CSS: .editor-styles-wrapper .wp-container-39 > [data-align="left"] {
float: left;
margin-right: 2em;
}
.editor-styles-wrapper .wp-container-39 > * {
max-width: 520px;
margin-left: auto !important;
margin-right: auto !important;
} The Therefore you can't really override these styles at all currently, leaving the template editor broken. |
Description
If you have a valid
theme.json
file and then try to use align left or align right on an image, it goes outside of the content container to the far edge of the editor. This happen in WP 5.8 RC2, regardless of whether you have the Gutenberg plugin activated. I've tested in my own base theme, but also with TT1 Blocks and Blockbase. The same thing also happens if I add a basic theme.json file to Twenty Twenty One.Step-by-step reproduction instructions
Expected behaviour
As it was before in WP 5.7, the frontend and backend output should be the same, as detailed in the next section.
Actual behaviour
From what I can see the problem is if you have
theme.json
setup, then in the backend<div class="wp-block" data-align="left">
now containsfloat: left
whereas before the float would be added to the child<figure>
element in the backend. On the frontend the float is added to the child<figure>
element both when you have yourtheme.json
active and when it's not. Consequently, it's properly aligned on the frontend in all cases.Screenshots or screen recording (optional)
WordPress information
Device information
The text was updated successfully, but these errors were encountered: