Automatic srcset generation with width and height also overwrites sizes attribute
Hi, I am currently optimizing image loading for our site. For that I am providing the width and height, so that Hubspot can automatically generate the srcset value. But I noticed that it also overwrites the sizes value I set, which makes me unable to provide the right size for the right source to load on different screen sizes. Did I understand something wrong or is this an issue with Hubspot?
Here the image is using the my-image class to for example be full width on mobile and then half width on tablets. I would like to reflect this in the sizes attribute for loading the right source on startup. Hubspot generates the right srcset options but also overwrites my sizes and says the image is always full width up to its native size:
sizes="(min-width: 1500px) 1500px, 100vw"
(This is the overwritten output if image.width is 1500)
Automatic srcset generation with width and height also overwrites sizes attribute
Hey there!
You’re on the right track—HubSpot does automatically generate a srcset (and a default sizes) whenever you use its built-in image module or the image_tag/img_url filters with explicit width and height. By design, it assumes “full-width up to the image’s native size” unless you tell it otherwise. That’s why you’re seeing:
sizes="(min-width: 1500px) 1500px, 100vw"
even though you’d prefer:
sizes="(min-width: 768px) 50vw, 100vw"
Here are two ways to regain control over your sizes attribute:
1. Use the HubL image_tag (or img_url + img_srcset) with a manual sizes override
Instead of dropping a plain <img> in your template, leverage HubL’s image filters and pass your own sizes. For example, if you have an image object in a module or a context variable called image, do this:
img_url(...) generates the URL at the specified dimensions.
img_srcset(...) builds a full srcset up to your max width.
sizes="…" stays exactly what you type in.
This way, HubSpot won’t stomp your sizes because you’re fully in charge of the HTML it spits out.
2. Build a tiny custom module with a “Custom Sizes” toggle
If you’re using the drag-and-drop Image module in the Design Manager:
Clone the core Image module.
In its module.json, add a field:
"name":"custom_sizes","label":"Sizes attribute","type":"text","help_text":"Enter your sizes, e.g. “(min-width:768px) 50vw, 100vw”.","default":"(min-width:768px) 50vw, 100vw"}