Cover block: add explicit sizes attribute to prevent blurry background image#77672
Open
adithya-naik wants to merge 2 commits intoWordPress:trunkfrom
Open
Cover block: add explicit sizes attribute to prevent blurry background image#77672adithya-naik wants to merge 2 commits intoWordPress:trunkfrom
adithya-naik wants to merge 2 commits intoWordPress:trunkfrom
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
…ect-fit cover (fixes WordPress#77387)
4beb527 to
8944f38
Compare
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
Solves : #77387
Added
sizes="100vw"to the<img>element in the Cover block'ssave.js, and added a correspondingv15deprecation entry indeprecated.jsto maintain backward compatibility.Why?
The Cover block uses
object-fit: coveron its background image, which means the browser crops the image based on both width and height. However, the browser selects which image to load from thesrcsetusing thesizesattribute, which by default was being set tosizes="auto"by WordPress'swp_filter_content_tags().sizes="auto"causes the browser to use only the rendered width of the element to pick an image — completely ignoring height. This leads to the wrong (too small) image being selected when the Cover block is tall but narrow, resulting in a blurry or pixelated background.Example scenario that triggers the bug:
min-height: 600pxset on the blockWhat happens:
300 × 600768 × 192← wrong, too small2400 × 600The image appears blurry because
object-fit: coverzooms in on the small image to fill the tall container.Root cause:
The browser picks image size based on width only, but
object-fit: coverrequires a large enough image to cover the full height too. Since nosizesattribute was set in the block's saved output, WordPress automatically addedsizes="auto", which doesn't account for height-based cropping.How?
By explicitly setting
sizes="100vw"on the<img>tag insave.js, WordPress no longer overrides it withsizes="auto". The browser then assumes the image could be as wide as the full viewport and loads an appropriately large image.A
v15deprecation was added indeprecated.jsthat exactly mirrors the previoussave.jsoutput (withoutsizes="100vw"), ensuring existing blocks saved without this attribute continue to work without validation errors.Testing Instructions
min-height: 600pxon the Cover blockImgBefore fix: A small image (e.g. 768px wide) is loaded, image appears blurry
After fix: A large image (e.g. 2400px wide) is loaded, image appears sharp
Changes
packages/block-library/src/cover/save.jssizes="100vw"to the<img>elementpackages/block-library/src/cover/deprecated.jsv15deprecation representing the previous save output