N
Nuxt2w ago
Boog

Does anyone know how NuxtImage and it's 'sizes' attribute works?

Let's say we want something that is full width as a simple example. But we don't want a 2500px wide image downloading on mobile. First, for testing purposes, we can set densities to just x1. To handle the sizes, one would think you could do something like this sizes attribute:
sizes="xs:400px sm:640px md:768px lg:1024px xl:1280px 2xl:1920px"
sizes="xs:400px sm:640px md:768px lg:1024px xl:1280px 2xl:1920px"
Full image snippet:
<NuxtImg
src="/images/heros/home-hero.jpg"
class="absolute w-full h-full object-cover -z-10"
sizes="xs:400px sm:640px md:768px lg:1024px xl:1280px 2xl:1920px"
format="webp"
densities="x1"
alt="My alt text"
preload
/>
<NuxtImg
src="/images/heros/home-hero.jpg"
class="absolute w-full h-full object-cover -z-10"
sizes="xs:400px sm:640px md:768px lg:1024px xl:1280px 2xl:1920px"
format="webp"
densities="x1"
alt="My alt text"
preload
/>
To test this, I emulate an iPhone. Which should be the size of XS. I check the network tab in devtools and low and behold, the size generated is 1280(xl). I have no idea why. If I emulate a browser in the small region (say 700px wide) and clear the network tab, refresh, the 1920 version comes in. The markup it produces is:
<img onerror="this.setAttribute('data-error', 1)" alt="My alt text" data-nuxt-img="" sizes="(max-width: 640px) 400px, (max-width: 768px) 640px, (max-width: 1024px) 768px, (max-width: 1280px) 1024px, (max-width: 1536px) 1280px, 1920px" srcset="/_ipx/w_400&amp;f_webp/images/heros/home-hero.jpg 400w, /_ipx/w_640&amp;f_webp/images/heros/home-hero.jpg 640w, /_ipx/w_768&amp;f_webp/images/heros/home-hero.jpg 768w, /_ipx/w_1024&amp;f_webp/images/heros/home-hero.jpg 1024w, /_ipx/w_1280&amp;f_webp/images/heros/home-hero.jpg 1280w, /_ipx/w_1920&amp;f_webp/images/heros/home-hero.jpg 1920w" class="absolute w-full h-full object-cover -z-10" src="/_ipx/w_1920&amp;f_webp/images/heros/home-hero.jpg">
<img onerror="this.setAttribute('data-error', 1)" alt="My alt text" data-nuxt-img="" sizes="(max-width: 640px) 400px, (max-width: 768px) 640px, (max-width: 1024px) 768px, (max-width: 1280px) 1024px, (max-width: 1536px) 1280px, 1920px" srcset="/_ipx/w_400&amp;f_webp/images/heros/home-hero.jpg 400w, /_ipx/w_640&amp;f_webp/images/heros/home-hero.jpg 640w, /_ipx/w_768&amp;f_webp/images/heros/home-hero.jpg 768w, /_ipx/w_1024&amp;f_webp/images/heros/home-hero.jpg 1024w, /_ipx/w_1280&amp;f_webp/images/heros/home-hero.jpg 1280w, /_ipx/w_1920&amp;f_webp/images/heros/home-hero.jpg 1920w" class="absolute w-full h-full object-cover -z-10" src="/_ipx/w_1920&amp;f_webp/images/heros/home-hero.jpg">
I must be doing something wrong to get this wonky of a result.
2 Replies
kapa.ai
kapa.ai2w ago
Hi @Boog. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I understand your confusion with the sizes attribute in NuxtImg. Let me explain how it works and what might be happening in your case. How the sizes attribute works in NuxtImg The sizes attribute in NuxtImg is a space-separated list of screen size/width pairs that follows a specific pattern. According to the documentation: - If you omit a screen size prefix (like sm:), that size becomes the 'default' size - Otherwise, Nuxt picks the smallest size as the default - Each specified size applies upward - so md:400px means the image will be 400px on medium screens and larger - The default size is used until the next specified screen width is reached NuxtImg documentation explains that this is a "responsive-first" sizing approach. The issue with your implementation In your example:
sizes="xs:400 sm:640 md:768 lg:1024 xl:1280 2xl:1920"
sizes="xs:400 sm:640 md:768 lg:1024 xl:1280 2xl:1920"
You're specifying widths in pixels, but you're missing the units (like px or vw). This is likely causing the unexpected behavior. Additionally, the way you've structured your sizes doesn't match how the browser interprets the sizes attribute. The browser needs to know when to use each size, not just the size values. Correct implementation For a full-width responsive image, try something like:
<NuxtImg
src="/images/heros/home-hero.jpg"
class="absolute w-full h-full object-cover -z-10"
sizes="100vw sm:50vw md:400px"
format="webp"
densities="x1"
alt="My alt text"
preload
/>
<NuxtImg
src="/images/heros/home-hero.jpg"
class="absolute w-full h-full object-cover -z-10"
sizes="100vw sm:50vw md:400px"
format="webp"
densities="x1"
alt="My alt text"
preload
/>
This tells the browser: - Default size is 100vw (100% of viewport width) - On small screens and up, use 50vw - On medium screens and up, use 400px For your specific case, you might want:
sizes="100vw"
sizes="100vw"
This simply tells the browser that the image will always be 100% of the viewport width, and NuxtImg will generate appropriate srcset values based on your configured screen sizes. The output you're seeing with all those different widths in the srcset is actually correct - NuxtImg is generating multiple image sizes to let the browser choose the most appropriate one. The issue is with how you're specifying which size should be used when. NuxtImg documentation provides more examples of how to properly use the sizes attribute. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@384545733061378060> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?