UPDATE:
For Drupal <= 11.2.10 use the patch in comment #17
For Drupal >= 11.3 use the patch in comment #37.
Starting with Drupal 11.3.0, the responsive image function in question has been replaced with methods on the class/service \Drupal\responsive_image\ResponsiveImageBuilder, and the original function was moved to a hook within the responsive_image module (ResponsiveImageThemeHooks.php). See issue 3548329.
Problem/Motivation
When configuring responsive image styles using the srcset and sizes attributes (Resolution switching), Drupal uses the fallback image’s width/height, rather than the dimensions of the image the browser selects, causing images to render with the wrong dimensions.
This does not seem to be an issue when using the <picture> element (Art direction).
Inside /core/modules/responsive_image/responsive_image.module the following snippet seems to cause the issue:
if (isset($variables['img_element']['#width'])) {
$variables['attributes']['width'] = $variables['img_element']['#width'];
}
if (isset($variables['img_element']['#height'])) {
$variables['attributes']['height'] = $variables['img_element']['#height'];
}

While inspecting the image above, dev tools show the correct image is loaded but the width and height attributes are altered to match the fallback image dimensions causing the image to not render as expected. The expected behavior is for that image to render full width, but because the fallback image is a smaller image, the issue above takes place.
Steps to reproduce
- Use Drupal 10.4+
- Create any image styles if necessary or use the existing image styles you may already have
- Configure a new Responsive Image Style using Responsive images from the Breakpoints group dropdown
- Choose "Select multiple image styles and use the sizes attribute." from the Type fieldset
- Select as many image styles as needed
- Type the desired sizes value in the Sizes field (i.e.
(min-width:768px) 720px, 100vw)) - Select a fallback image (preferably an image that is not the size of how you expect your image to render)
- Create page where the new responsive image style is used and add images to it. Save it
- Your image should render using your fallback image dimensions
See attached screenshot.

Proposed resolution
Restore original responsive image rendering behavior which allowed images to be rendered at the size specified in the Sizes field.
Related issues:
https://www.drupal.org/project/drupal/issues/3377420 which seems to be where the new behavior stems from. Perhaps rethink the approach for ensuring the img tag dimensions reflect the correct image's dimensions, not the fallback image.
https://www.drupal.org/project/drupal/issues/3359421
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3516726
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
mariohernandez commentedComment #3
mariohernandez commentedComment #4
mariohernandez commentedI did additional research and testing using vanilla installations of Drupal 10.x.
First test - Using fallback image width and height
Expected behavior, image should render large on desktop (max 1290px), and full width on mobile.
1. First I configured responsive images (resp-img.png)
2. While inspecting the image, visually it looks tiny and that's because it is using the fallback image dimensions (dimensions.png)
3. However, looking at the Network tab in the dev tools, we see the right image is loaded, but because Drupal is changing the width and height of the img tag based on the fallback image dimensions (network.png)
Second test - Not using fallback image
1. Same responsive image configuration as above
2. Inspecting the image this time, reveals the right width/height on the image tag which is provided by the Sizes configuration in item #1 above.
3. The Network tab in inspector's console displays the right image dimensions and visually the image also looks correct (dimensions2.png)
Conclusion
Drupal is able to provide the right width and height to the img tag without resourcing to the fallback image. Hoping this can be resolved to be able to effectively use responsive images.
Comment #5
mariohernandez commentedComment #6
mariohernandez commentedComment #7
quietone commentedChanges are made on the main development branch 11.x first, then backported.
Comment #8
mariohernandez commentedComment #9
agarzola commentedThe question is how to determine which image to use for these values. Which image is most correct to use? From a progressive enhancement perspective, the fallback image seems like the correct choice, even though it creates the issue described here for images that are not sized with CSS.
Comment #10
mariohernandez commented@agarzola, that a great question and that's the beauty of using
srcsetandsizesimage attributes. Thesrcsetlets you define a set of images each of which includes its width, allowing the browser to select the best image based on its width and the desired rendering size which is provided as part of thesizesattribute.Comment #11
agarzola commentedYes, that part is clear. What I am asking is what width and height attributes should be included in the img tag. Of all the inages included in the srcset, which one should Drupal use use to populate those attributes?
Comment #12
mariohernandez commented@agarzola, I see what you mean. I think the width/height should be the one from the image selected by the browser. The browser analyzes the srcset and sizes attributes values then picks the best image that meets the rendering criteria, among other things. Up to this point, this has been the default behavior until I started seeing issues with fallback image width/height being applied to the img tag (thus this issue). By the way, this only seems to be an issue with srcset/sizes and not
<picture>.Comment #13
mariohernandez commentedComment #14
mariohernandez commentedComment #15
mariohernandez commentedComment #16
mariohernandez commentedComment #17
mariohernandez commentedThis patch removes the fallback image dimensions so they are not forced into the width/height attributes of the img tag.
Comment #18
agarzola commentedI'm asking about the HTML attributes that are printed by Drupal in the HTML payload, not the rendered width and height in the browser. There needs to be some value in the width and height attributes. What values should Drupal use for those attributes if not the values from the fallback image?
Removing the width and height attributes as proposed in #17 will cause content layout shift issues that were resolved in 3359421. Is that really what we want?
Comment #19
mariohernandez commentedYes @agarzola, my answers remains the same. The width/height values for the HTML attributes should be those of the image selected by the browser. My argument is that what is the point of configuring responsive images with specific loading criteria, if they will be overriden by using fixed values from a fallback image?
As for issue 3359421, If thats where the breaking changes were introduce, then I would think they need to be implemented differently.
I appreciate your feedback and I hope with the help of the community we can come up with a solution that will work for everyone while adhering to the standards of responsive images.
Comment #20
smustgrave commentedPatches should be in MR, and most likely will need a test case showing the issue.
Comment #21
mariohernandez commentedHey there! I wanted to share some clarifications about this patch that might be helpful:
I hope this helps understand what's in play here.
Comment #22
damienmckennaCould you please clarify what the HTML looks like before and after this change? I think that would help everyone understand what is happening. Thank you.
Comment #23
mariohernandez commented@damienmckenna, certainly. The HTML does not look much different before/after the patch is applied. I've cleaned it up for better readability.
Before
After
The difference after the patch is applied is that the width and height of the img tag is set/forced to be that of the fallback image rather than using the dimensions of the image the browser has selected. As you can see, each image in the
srcsethas a width defined (i.e.768w, this value is to instruct the browser how big this image is and in combination with the query in thesizesattribute, the browser is able to select the best image to meet the developer's intended rendering goal.By using the fallback image dimensions as the img width/height, we are bypassing the concept of responsive images.
Here are before/after screenshots which show the issue when the fallback image is smaller than the size of the intended image. Some may say: "Why not use a larger fallback image?", true, but again, the whole goal of responsive images is for the browser to analyze the environment and select the best image for the job. In addition, what if I am dealing with an image that is, say, 2600px? this means my fallback image would need to be that large which I don't think is the best approach.
Before
After
Comment #24
mariohernandez commentedComment #25
damienmckennaI wanted to know what the recommended solution was for the "width" and "height" attributes when using responsive images.
I couldn't find any recommendations on Mozilla's dev article on the topic or their <img> docs page.
I did find this article:
The article says "Set width and height to the resolution that corresponds to your default src." That suggests that Drupal core's handling of these attributes is correct and that we should close this as "won't fix".
Comment #26
damienmckennaBTW it's not technically possible to set the image's width and height attributes to the specific image variation the browser will use as the HTML tag is generated at the server, and the server doesn't know what the browser is going to do with it.
Comment #27
mariohernandez commentedYes, this is not an easy issue due to your last point of the server not knowing what the browser will do.
This may be a silly question, but based on the code examples in my previous comment, how were the width/height set? Was it Drupal doing that after the page was rendered?
I feel like responsive images works well when we let the browser do its job, but I do understand that in some cases there might be content shift when images don't contain a width/height. My concern with forcing a static width/height on the img is that this defeats the purpose of responsive images. So the question is, is there another way to capture width and height that is based on the image actually being selected by the browser rather than a random fallback image which purpose is simply to be used when browsers don't support responsive images?
Comment #28
mariohernandez commentedOn smaller images, using a fixed image may work just fine and we wouldn't even need responsive images. This is the approach I take when images are intended to be rendered at a relatively small size across all breakpoints. However, when it comes to large images such as Hero images or full width images, this is when responsive images shine and using the fallback image approach would result in either upscaling images with CSS which is not recommended, or, using extremely large images as fallback which in turn will affect performance and maybe even UX.
Comment #29
damienmckennaIMHO a major part of this is the CSS that goes with the image, so in the examples in #23 there might be CSS that also affect how the image is being displayed.
Comment #30
mariohernandez commentedI think there may be some level of CSS involved in process. Here are the most common ones:
img { max-width: 100%; height: auto; }. This rule is pretty much best practice on most projects. It ensures the image never exceeds the width of the container it is in, and the image width will never be upscaled beyond its original image size.aspect-ratioproperty can help the browser predict what the image approx size or aspect ratio will be.It's not common practice to resize the actual
<img>tag only using CSS. The recommended approach is to manage layouts around the image and images should naturally adapt to the layout containers.The last point about using CSS for image dimensions, yes, it is true that you can actually resize images to the desired dimension with CSS, but just like images, CSS also need to be fully available during the page load process for those rules to kick in.
I feel like the issue is not so much the size of the image, the browser is already taking care of that for us, but just as important, is assigning the right width/height values to the img tag at load time (ideally the same dimensions the browser selected), to avoid affecting Largest Contentful Paint
Comment #31
josaku commented#17 works for me (Drupal core 10.5.0). Thanks.
Moreover, I think that width and height in img could be replaced with one "sizes".
Is this way a resolution for two issues?
For example, in my case, the following code was generated after upgrading to 10.5.0 from 10.3.x.
However, this resolves the issue. (Of course, #17 is enough for me though)
Comment #32
luke.leberI think that the root of this problem is that the original intent of #3377420 was to target the fallback image.
The
<img>element is only a fallback when a<picture>tag is wrapped around it. Drupal's responsive image handling wraps a picture tag around markup conditionally. #3377420 also affects images that are not used in a fallback capacity.Example configuration:
In this case, the responsive image style only has a single child image style -- which causes Drupal Core to omit the wrapping
<picture>tag.Example markup before #3377420
After
The result is a pretty significant B/C break that makes the image inaccessible to sighted users.
Comment #33
mariohernandez commentedUpdated requirements for patch.
Comment #34
mariohernandez commentedAdded issue reference about 11.3 change.
Comment #35
mariohernandez commentedAdded new patch for Drupal core 11.3 or higher.
For Drupal <= 11.2.10 use the patch in comment #17
For Drupal >= 11.3 use the patch in comment #37.
See 3548329 for details.
Comment #36
mariohernandez commentedComment #37
mariohernandez commentedComment #38
mariohernandez commentedComment #39
smustgrave commentedHave not reviewed
But patches needs to be in an MR now
Also will need test coverage, if not already there.
Comment #40
mariohernandez commentedAdded patch tests.
Comment #42
mariohernandez commentedCreated a MR from patch in comment #37. I'd appreciate your feedback: MR/14162
Comment #43
smustgrave commentedMR appears to be missing test coverage. Tests should be included in the fix. But this change broke some existing tests which means this probably is a BC, so backwards compatibility will have to be considered
Comment #44
smustgrave commentedPatches don’t run the tests, DrupalCi was shut down a few years back but the MRs are much much faster
Comment #45
mariohernandez commentedThanks for the feedback, @smustgrave. I'll add test to the MR.
Comment #48
mariohernandez commentedUpdate the Functional tests with backwards compatibility.
Comment #49
smustgrave commentedSorry this one appears to need a rebase.
If you are another contributor eager to jump in, please allow the previous poster(s) at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!