Skip to content
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

Center aligned image isn't centered in 0.90 (either in editor or on front of site) #2548

Closed
wpfangirl opened this issue Aug 27, 2017 · 5 comments
Labels
[Feature] Blocks Overall functionality of blocks [Type] Bug An existing feature does not function as intended
Milestone

Comments

@wpfangirl
Copy link

Issue Overview

I inserted an image and selected center align, but the image didn't center either in the editor or in the post previw.

Steps to Reproduce (for bugs)

  1. Add an image block.
  2. Click the button to center-align it.

Expected Behavior

The image should center, the same way it right-aligns and left-aligns (that is, the whole block resizes to fit the image, and centers).

Current Behavior

The block stays full-width, though the image resizes. The image shows inline inline even though the aligncenter class is added to the figure.
image-not-centering

@aduth
Copy link
Member

aduth commented Aug 28, 2017

This is potentially theme-dependant, caused by the alignment class aligncenter being applied to the figure wrapping the img, not the img itself:

return (
<figure className={ align ? `align${ align }` : null }>
{ href ? <a href={ href }>{ image }</a> : image }
{ caption && caption.length > 0 && <figcaption>{ caption }</figcaption> }
</figure>
);

The recommended styles for .aligncenter perform centering by auto left-and-right margins:

https://codex.wordpress.org/CSS#WordPress_Generated_Classes

In the case of a figure, this will not work because as a block-level element, it already occupies the full width of its container:

Figure full width center

When applied to the image though, since the image assigns its own width, it will center itself within:

Image cneter

Possible solutions:

  • Avoid using the default aligncenter (and other align) class and instead assign custom classes with styles prescribed by the image block
  • Assign aligncenter (and other align) to the img element
    • This may have repercussions on alignment of a caption, if applicable
  • Assign width style to figure equal to that of the image

@wpfangirl
Copy link
Author

wpfangirl commented Aug 28, 2017 via email

@aduth
Copy link
Member

aduth commented Aug 29, 2017

Okay, that applies to the way it looks on the front of the site, but not, one presumes, to the way it looks in the editor.

The reason it's not aligned in the editor is because there appears to be nothing operating on the center align style. There are styles to handle the left/right (float) and wide/full (escape bounds) cases, but not center:

getEditWrapperProps( attributes ) {
const { align, width } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align || 'full' === align ) {
return { 'data-align': align, 'data-resized': !! width };
}
},

Also, the alignleft and alignright work on the front of the site (if you resize the image), but not the aligncenter. Yet presumably these classes are being applied to the figure rather than the image in all cases.

Yes, but left and right alignments use float, not the margins of auto like with center alignment. The floated figure will assume the width of the image.

This doesn't seem predictable though:

You should always set a width on floated items [...] If no width is set, the results can be unpredictable. Theoretically, a floated element with an undefined width should shrink to the widest element within it. This could be a word, a sentence or even a single character - and results can vary from browser to browser.

http://css.maxdesign.com.au/floatutorial/introduction.htm

@karmatosed karmatosed added the [Type] Bug An existing feature does not function as intended label Sep 7, 2017
@mtias mtias added the [Feature] Blocks Overall functionality of blocks label Nov 20, 2017
@karmatosed karmatosed added this to the Merge Proposal milestone Jan 25, 2018
@youknowriad
Copy link
Contributor

With recent changes, this seems to work as intended in the editor. In the frontend it depends on the theme styles.

@aduth
Copy link
Member

aduth commented Feb 15, 2018

Fixed by #4801

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Blocks Overall functionality of blocks [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

5 participants