Learn how to use images on your website without sacrificing performance.
Deco provides Image, Picture, and Source components to work with images. These components add responsive patterns to speed up your site. To add an image to your site:
- Add the deco image components (
<Image/>
,<Source>
,<Picture>
) to your code- Style the components with CSS until you achieve the desired appearance
- Set the width and height attributes of the component.
The deco components already provide:
- Responsive images for all screen sizes
- Preloading tags to improve LCP
- Image processing to optimize image size
<Image>
and <Picture>
. The
appropriate component depends on your use case:
<Image>
displays an image (internally using <img>
) and is useful for
images that always have the same aspect ratio (width vs height) regardless of
screen size.<Picture>
defines images that can vary in size according to the screen
resolution.Note that you should use theThe example above renders the<Frame><img>
tag inside Picture, not the<Image/></Frame>
component. Also note that thesrc
attribute in the<img>
tag is REQUIRED and should receive the larger image, in this case, the desktop image.
/image-mobile.png
image for screen sizes up to
768px wide. For larger screen sizes, the /image-desktop.png
image will be
rendered.
To style this image, add classes to the <img>
tag. For example, to make the
image fill the available space, as shown in the example above.
width
and height
attributes of the image can be confusing, even for
experienced developers. This confusion arises from the fact that these
attributes DO NOT change the final size of the rendered image on the screen.
Instead, they affect the image that the browser will download in a responsive
image scenario. Choosing appropriate width and height values is key to
downloading a small image for good LCP results.
To find a good width and height value:
width
is 270px and the height
is 377px.<Image>
and <Picture>
components from Deco help us achieve
this behavior. Start by identifying the largest element on the screen (LCP).
Then, open the component’s code and check if:
preload
attribute is set.loading
attribute is set to ‘eager’.fetchPriority
attribute is set to ‘high’.<Image/>
component:
<Picture/>
:
preload
attribute and have the
loading="lazy"
and fetchPriority="low"
attributes. By adding these
configurations, you will optimize the image loading to improve LCP.
Tip: The final page should have only one preloaded image.
Preloading multiple images worsens the LCP score. To verify that only one
image is being preloaded, check that there is only one <link rel="preload"/>
tag.