Featured Image Block: Center alignment does not work
Description
When Center alignment for the Featured Image block is selected - it does not change the alingment.
I expected selecting center alignment to center the image - but it remained left aligned.
Step-by-step reproduction instructions
- Make sure your site has at least one post with a Featured Image uploading
- Open the index template in the site editor
- Add or select a Featured Image block inside the Query Loop > Post Object
- Use the 'Dimensions' settings to make the image smaller (200px or so)
- Use the alignment tool (in the toolbar) to select left alignment for the Featured Image
Screenshots, screen recording, code snippet
Environment info
- WP 5.8.3
- Gutenberg 12.3.2
- tested with Quadrat and TT1 Blocks themes
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 @jordesign! Thanks for reporting this. I'm able to reproduce the steps. To add a context, the featured image can't be centered when custom width is applied.
Noting that this is still an issue with WordPress 6.0.1 and Gutenberg 13.6. This was repeated during the fifteenth call for testing for the FSE Outreach Program when @paaljoachim was trying to left align a featured image. After dimensions are changed, it seems as though aligning the block doesn't work at all. I'm not sure how/if this might overlap with general block support tracking issues: https://github.com/WordPress/gutenberg/issues/41113
@draganescu and @ntsekouras would this be something for you to take a closer look at?
(edited) Tested this:
- the problem seems to only appear when the block is used inside a query loop
- stand alone featured image block gets properly aligned
- ~~featured image block in a query loop's post template does not get aligned~~ I think this is another issue. In the theme I tested the featured image was properly aligned in the query loop if it is in a group block in the post template. It does not work if it is directly in the post template.
Set the margin of the featured image in theme.json.
"blocks": { "core/post-featured-image": { "spacing": { "margin": { "bottom": "0px", "top": "0px", "left": "auto", "right": "auto" } } }
I think this may have been fixed in trunk.
Noting that this is still an issue in WordPress 6.1.1 and Gutenberg 14.7.3
This seems to be true for multiple blocks inside a query loop / post template block that has an alignment setting and not a text alignment. I quickly tested the following blocks in 6.2 RC and Twenty Twenty Three:
Cannot be right aligned or centered: Image, post featured image, calendar, table., search.
Except for the two image blocks, I can't think of any reasons to add these blocks to the query, but they are allowed and they should work.
Can be right aligned or centered: File block
I can see that the file block specifically adds the following CSS:
&.aligncenter {
text-align: center;
}
&.alignright {
/*rtl:ignore*/
text-align: right;
}
The custom width is applied to the inner <img>, while the alignment class is applied to the <figure> wrapper, which stays full width. So the wrapper is aligned, but the image inside it doesn't move. I'm planning a small CSS-only fix that aligns the image within the wrapper using text-align, as the File and Image blocks already do (thanks @carolinan for the pointer). I'll open a PR shortly.