gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Image Block Alignments broken on themes using theme.json

Open philhoyt opened this issue 4 years ago • 14 comments

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 Screen Shot 2022-04-13 at 11 46 14 AM 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 Screen Shot 2022-04-13 at 11 49 07 AM

This was tested using WordPress 5.9.3 with and without Gutenberg 12.9.0

Step-by-step reproduction instructions

  1. Activate a theme using theme.json (Twenty Twenty-Two or Other)
  2. Create a page or post with a paragraph of text
  3. Place an image block above the paragraph and align left or right
  4. 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

philhoyt avatar Apr 13 '22 15:04 philhoyt

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.

ndiego avatar Apr 15 '22 14:04 ndiego

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.

philhoyt avatar Apr 25 '22 13:04 philhoyt

Looks to be reverted... weird

philhoyt avatar May 25 '22 13:05 philhoyt

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

sergiu-radu avatar Sep 29 '22 11:09 sergiu-radu

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?

bridgetwes avatar Oct 07 '22 20:10 bridgetwes

So this is bug that won't be fixed, which will break thousands of websites if they update to an FSE theme.

audunmb avatar Jun 23 '23 11:06 audunmb

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

audunmb avatar Jun 23 '23 12:06 audunmb

This is clearly unacceptable behavior from the image block. Please fix asap.

delorimier avatar Oct 16 '23 17:10 delorimier

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.

delorimier avatar Oct 16 '23 20:10 delorimier

@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

djcowan avatar Apr 16 '24 04:04 djcowan

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.

Image

gmoz22 avatar Mar 14 '25 20:03 gmoz22

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.

gregbob13 avatar Mar 21 '25 16:03 gregbob13

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 avatar Mar 24 '25 21:03 gmoz22

@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',
  ],
]);

gregbob13 avatar Mar 25 '25 16:03 gregbob13