Image Block Alignments broken on themes using theme.json
Description
Image blocks when rendered on themes using theme.json output as followed
<figure class="wp-block-image alignright size-full"><img width="150" height="150" src="https://the-northeast-ohio-region.local/wp-content/uploads/2022/04/150.png" alt="" class="wp-image-16913"></figure>
Without a div container resulting in the following
This is easily tested with themes like Twenty Twenty-Two, but not limited to FSE themes.
Image blocks on non theme.json themes render as followed
<div class="wp-block-image">
<figure class="alignright size-full"><img width="150" height="150" src="https://the-northeast-ohio-region.local/wp-content/uploads/2022/04/150.png" alt="" class="wp-image-16913"></figure></div>
resulting in the desired alignment visually

This was tested using WordPress 5.9.3 with and without Gutenberg 12.9.0
Step-by-step reproduction instructions
- Activate a theme using theme.json (Twenty Twenty-Two or Other)
- Create a page or post with a paragraph of text
- Place an image block above the paragraph and align left or right
- View results
Screenshots, screen recording, code snippet
No response
Environment info
This was tested using WordPress 5.9.3 with and without Gutenberg 12.9.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
Hi @philhoyt, while not ideal, this is expected behavior in block themes. The image figure is no longer contained in a wrapper div, which is great for many use-cases but does present the drawback you highlighted in this issue. One easy fix is to wrap the Image block in a Group block. That will give you the design you are looking for. Please let me know if I can answer any additional questions.
This is not okay. To explain to a client they would need to wrap their image in a group to accommodate alignments is unacceptable. Are posts imported from previous themes not taken into account for backward compatibility? The block already wraps the image in a div as explained in previous iterations of the block editor. Your "expected behavior in block themes" results in "not ideal" behavior in the default FSE theme.
Looks to be reverted... weird
Hey guys, any updates on this issue?
Do you plan to bring back the wp-block-image container to left/right aligned images?
Without it everything breaks and adding wrap the image inside a group is not great at all because people will have to edit all their posts manually...
I spent hours trying to figure out why some of my client sites get the div wrapper around alignright and alignleft image blocks and others don't. Even Twenty Twenty-One has the wrapper, but switching same site to Twenty Twenty-Two removed the wrapping div. I finally figured out it was the presence of the theme.json file in the theme. The extra wrapping div allows me to align the floated images with the other margin-left, margin-right: auto content on pages. Otherwise, there is no container to add the margin: auto to, with a float on the figure so paragraphs can wrap around it. The Twenty Twenty-Two theme floats alignleft image blocks way off to the side. Is there any chance the wrapping div can be added back to image blocks aligned left or right?
So this is bug that won't be fixed, which will break thousands of websites if they update to an FSE theme.
Adding the following to a css-file (style.css) creates a workaround for this issue. It won't work in the editor, only in the frontend.
@media only screen and (min-width : 768px) {
figure.wp-block-image.alignleft {
margin-left: calc( ( 100% - var(--wp--style--global--content-size)) / 2 );
}
figure.wp-block-image.alignright {
margin-right: calc( ( 100% - var(--wp--style--global--content-size)) / 2 );
}
}
This is clearly unacceptable behavior from the image block. Please fix asap.
Here is my solution: wrapping the image block in a div. Works in my current theme.
Frontend:
function wrap_image_block( $block_content, $block ) {
if ( $block['blockName'] === 'core/image' ) {
$block_content = '<div class="image-block-wrapper">' . $block_content . '</div>';
return $block_content;
}
return $block_content;
}
add_filter( 'render_block', 'wrap_image_block', 10, 2 );
Backend (enqueue script in backend with admin_enqueue_scripts)
var createElement = wp.element.createElement;
var modifyEditHtml = function(BlockEdit) {
return function(props) {
if (props.name !== "core/image") {
return createElement( BlockEdit, props );
}
return createElement(
"div",
{ className: "image-block-wrapper" },
createElement(
BlockEdit,
props
)
);
};
};
wp.hooks.addFilter(
"editor.BlockEdit",
"my-plugin/modifyEditHtml",
modifyEditHtml
);
This took me a few hours to figure out and may not be optimized for your project,
I wish we didn't have to struggle as much with the new editor. Image blocks should never have behaved that way.
@audunmb 👍 thank you - solution works. but - might - need to consider .is-layout-constrained
_.is-layout-constrained_ figure.wp-block-image.alignright {
margin-right: calc( ( 100% - var(--wp--style--global--content-size)) / 2 );
}
@delorimier there's an render_block ticket regarding the priorty of calls to core\image #55407
The expected solution for an update which changes elements' alignments on websites is to manually edit every single post?
It's also breaking in the editor, not just the images (this one is supposed to be centered) but even the post title is now misaligned.
The expected solution for an update which changes elements' alignments on websites is to manually edit every single post?
It's also breaking in the editor, not just the images (this one is supposed to be centered) but even the post title is now misaligned.
@gmoz22 , I've opted to remove theme.json from non-block based themes and enqueue editor CSS & js for any editor styling / functionality tweaks instead (like I've been doing for many years). Removing theme.json from your theme effectively solves this issue.
Per @ndiego 's earlier comment "while not ideal, this is expected behavior in block themes" ... I believe this is where we need to decide whether we're going for the full on FSE experience or if we're still using WP primarily as a CMS with an arguably easier albeit less flexible experience for editors.
The expected solution for an update which changes elements' alignments on websites is to manually edit every single post? It's also breaking in the editor, not just the images (this one is supposed to be centered) but even the post title is now misaligned.
@gmoz22 , I've opted to remove theme.json from non-block based themes and enqueue editor CSS & js for any editor styling / functionality tweaks instead (like I've been doing for many years). Removing theme.json from your theme effectively solves this issue.
Per @ndiego 's earlier comment "while not ideal, this is expected behavior in block themes" ... I believe this is where we need to decide whether we're going for the full on FSE experience or if we're still using WP primarily as a CMS with an arguably easier albeit less flexible experience for editors.
I was only using theme.json to have a custom color palette. I can't seem to update the settings via JS, I'm running into this issue: https://github.com/WordPress/gutenberg/issues/15993.
@gmoz22 - Good old PHP:
// brand colors
add_theme_support( 'disable-custom-colors' );
add_theme_support( 'editor-color-palette', []);
add_theme_support( 'editor-color-palette', [
[
'name' => __( 'white', 'p13' ),
'slug' => 'white',
'color' => '#ffffff',
],
[
'name' => __( 'blue', 'p13' ),
'slug' => 'blue',
'color' => '#46578e',
],
[
'name' => __( 'red', 'p13' ),
'slug' => 'red',
'color' => '#ED5A4A',
],
[
'name' => __( 'charcoal', 'p13' ),
'slug' => 'charcoal',
'color' => '#616264',
],
[
'name' => __( 'brown', 'p13' ),
'slug' => 'brown',
'color' => '#47433b',
],
]);