After exploring various resources on responsive images, I have come across a challenge that has me stumped. It seems like it should be simple, but I can't quite figure it out: How can I serve the appropriate image size based on container width rather than screen width?
For example, let's say I have a desktop screen that is 1980px wide, but a flex container that occupies 1/3 of the screen. In this case, the maximum image size required would only be 660px wide, which means it should display the 800w image. However, srcset typically relies on screen size, so even when displaying thumbnails, it will load the 1200w image. Is there a way to handle this dynamically? Can I still use flexbox and dynamic widths while serving the correct image size based on the container width instead of the screen width? Any assistance would be immensely appreciated, thank you!
<div class="flex justify-center w-1/3 mx-auto">
<img
srcset="size-1.jpg 400w,
size-2.jpg 800w,
size-3.jpg 1200w,"
sizes="(min-width: 1200px) 1200px, (min-width: 800px) 800px, 400px"
/></div>