Problem/Motivation
Media embedded with CKEditor 5 defaults to the "break text" alignment option which should follow the default left alignment. But inside the editor, these items are centered horizontally, whereas when saved and viewed outside the editor, the embedded media item is left aligned as expected.
Steps to reproduce
1. Using Drupal version 9.5.5 install and enable CKEditor 5 and the Embed Media plugin.
2. Go to /admin/config/content/formats/manage/basic_html and configure the WYSIWYG editor to use the CKEditor 5 and the media embed plugin. See Embedding media with CKEditor documentation.
3. Create/edit a node and embed media within CKEditor by clicking the "Insert Media" button in the CKEditor toolbar. This will open the media library, where you can select the media file you want to embed.
4. Note that inside the editor, the embedded media item is horizontally centered.

5. Save the node and now note that the embedded media item is left aligned.

Proposed resolution
Inside the editor, embedded media is wrapped inside of a <figure> element whose CSS (from drupalmedia.css) is set to
display: table;
margin: 0.9em auto;
which results in a center-aligned media item. But when viewed outside of the editor, the media item is not wrapped in a <figure> element and therefore falls back to the default left alignment.
The CSS applied to editor-specific HTML elements should not introduce additional CSS formatting that will not be applied outside of the editor because it results in a discrepancy between the element's appearance inside and outside the editor.
Remaining tasks
User interface changes
After patch

API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | ScreenRecording2024-05-24at8.37.01AM-ezgif.com-video-to-gif-converter.gif | 1.92 MB | gauravvvv |
| Media outside editor.png | 291.3 KB | richarddavies | |
| Media in editor.jpeg | 47.05 KB | richarddavies |
Issue fork drupal-3349483
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
wim leers… this is highly dependent on the CSS of the front-end theme and back-end theme!
I remember that @lauriii spent a lot of time on this in #3264727: [drupalMedia|drupalImage] Allow removing data-align in the UI, and making an image inline. I think he'll be the best person to answer this.
Comment #6
gauravvvv commentedComment #7
gauravvvv commentedComment #8
smustgrave commented@Gauravvvv did you see comment #2?
Moving back to NW for the answer @Wim Leers was mentioning then determine approach
Comment #9
mabho commentedThe fact that the
<figure />tag is showing within CKEditor5, and not outside it (when the image is rendered on the page) seems to be the problem to me. My take on this is: when the image is rendered, no matter what, the<figure />wrapper should be applied, so the behavior within CKEditor is consistent with the behavior outside CKEditor. I wonder, though, what would be the unintended consequences of such a change on legacy websites...