diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md
index 08a299bc17b71..b16bc7137b58f 100644
--- a/docs/reference-guides/core-blocks.md
+++ b/docs/reference-guides/core-blocks.md
@@ -387,7 +387,7 @@ Insert an image to make a visual statement. ([Source](https://github.com/WordPre
- **Name:** core/image
- **Category:** media
-- **Supports:** align (center, full, left, right, wide), anchor, color (~~background~~, ~~text~~), filter (duotone), interactivity, shadow ()
+- **Supports:** align (center, full, left, right, wide), anchor, color (~~background~~, ~~text~~), filter (duotone), interactivity, shadow (), spacing (margin)
- **Attributes:** alt, aspectRatio, blob, caption, height, href, id, lightbox, linkClass, linkDestination, linkTarget, rel, scale, sizeSlug, title, url, width
## Latest Comments
diff --git a/packages/block-library/src/image/block.json b/packages/block-library/src/image/block.json
index b9d269db67462..6417879164a22 100644
--- a/packages/block-library/src/image/block.json
+++ b/packages/block-library/src/image/block.json
@@ -106,6 +106,9 @@
"filter": {
"duotone": true
},
+ "spacing": {
+ "margin": true
+ },
"__experimentalBorder": {
"color": true,
"radius": true,
diff --git a/packages/block-library/src/image/index.php b/packages/block-library/src/image/index.php
index ace6a36546901..ce9c958c19b6c 100644
--- a/packages/block-library/src/image/index.php
+++ b/packages/block-library/src/image/index.php
@@ -306,12 +306,12 @@ class="wp-lightbox-overlay zoom"
-
-
+
diff --git a/packages/block-library/src/image/view.js b/packages/block-library/src/image/view.js
index 99cc84d79f0f4..5ff97cccf1834 100644
--- a/packages/block-library/src/image/view.js
+++ b/packages/block-library/src/image/view.js
@@ -42,6 +42,15 @@ const { state, actions, callbacks } = store(
'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='
);
},
+ get figureStyles() {
+ return (
+ state.overlayOpened &&
+ `${ state.currentImage.figureStyles?.replace(
+ /margin[^;]*;?/g,
+ ''
+ ) };`
+ );
+ },
get imgStyles() {
return (
state.overlayOpened &&
diff --git a/test/e2e/specs/editor/blocks/image.spec.js b/test/e2e/specs/editor/blocks/image.spec.js
index 3cb1565c8d416..c4c725e083625 100644
--- a/test/e2e/specs/editor/blocks/image.spec.js
+++ b/test/e2e/specs/editor/blocks/image.spec.js
@@ -937,6 +937,37 @@ test.describe( 'Image - lightbox', () => {
} );
} );
} );
+
+ test.describe( 'should render as expected on front end', () => {
+ test( "Overlay image should not inherit content image's margins", async ( {
+ editor,
+ page,
+ } ) => {
+ await editor.setContent( `
+
+
+ ` );
+
+ const postId = await editor.publishPost();
+ await page.goto( `/?p=${ postId }` );
+
+ const lightboxImage = page.locator( '.wp-lightbox-container img' );
+ await expect( lightboxImage ).toBeVisible();
+ await lightboxImage.click();
+
+ const figure = page
+ .locator( '.wp-lightbox-overlay .wp-block-image' )
+ .first();
+ await expect( figure ).toBeVisible();
+ const margin = await figure.evaluate( ( element ) => {
+ return window
+ .getComputedStyle( element )
+ .getPropertyValue( 'margin' );
+ } );
+ expect( margin ).toBe( '0px' );
+ } );
+ } );
} );
// Added to prevent regressions of https://github.com/WordPress/gutenberg/pull/57040.