#Image component densities property
7 messages · Page 1 of 1 (latest)
Hi @wary pollen. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
The densities property in Astro's Image component is used to generate a srcset attribute for the <img> tag. This attribute is a list of pixel densities for which the image should be generated. The browser then uses this srcset to decide which image to display based on the pixel density of the user's screen.
For example, if you provide densities={[1.5, 2]} as a property to the Image component, it will generate a srcset attribute with images for 1.5x and 2x pixel densities. If the user's screen has a pixel density of 2x, the browser will choose the 2x image from the srcset.
Here's an example of how you might use the densities property:
import { Image } from 'astro:assets';
import myImage from "../assets/my_image.png";
<Image src={myImage} width={myImage.width / 2} densities={[1.5, 2]} alt="A description of my image." />
This will generate an <img> tag similar to the following:
<img src="/_astro/my_image.hash.webp" srcset=" /_astro/my_image.hash.webp 1.5x /_astro/my_image.hash.webp 2x " alt="A description of my image." width="800" height="450" loading="lazy" decoding="async"/>
In this example, the srcset attribute contains two images: one for 1.5x pixel density and one for 2x pixel density. The browser will choose the appropriate image based on the pixel density of the user's screen.
Sources:
- Astro Documentation: Images (https://docs.astro.build/en/guides/images#densities)
- Astro Blog: Astro 3.3: Picture component (https://astro.build/blog/astro-330#srcset-support)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@wary pollen kapa.ai is still learning and improving, please let us know how it did by reacting below
@whole wolf if I want to generate a srcset containing higher quality images for desktop and lower quality images for mobile, which combination of Image component properties should I use
Let me check the knowledge base for you. One moment! :books:
To generate a srcset containing higher quality images for desktop and lower quality images for mobile, you can use the widths property of the Image component in Astro. This property allows you to specify the widths of the images you want to generate for different viewport sizes.
Here's an example:
import { Image } from 'astro:assets';
import myImage from "../assets/my_image.png";
<Image src={myImage} widths={[300, 600, 900]} alt="A description of my image." />
In this example, the widths property is set to [300, 600, 900], which means Astro will generate three versions of the image: one with a width of 300 pixels (for smaller screens), one with a width of 600 pixels (for medium-sized screens), and one with a width of 900 pixels (for larger screens).
The browser will then choose the appropriate image based on the current viewport size and the srcset attribute generated by Astro.
Please note that the widths property generates images based on absolute widths in pixels, not pixel density. If you want to generate images based on pixel density (for example, for retina displays), you should use the densities property instead.
Sources:
- Astro Documentation: Images (https://docs.astro.build/en/guides/images#widths)
- Astro Blog: Astro 3.3: Picture component (https://astro.build/blog/astro-330#srcset-support)