-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Responsive images #1042
Comments
In order to automatically generate the correct attributes for an image, we need to know how it should behave when resized. I propose introducing a The most important attributes to set for best practices are shown below. These will all have appropriate values set, according to the specified layout. These can be overridden, but should not need to be in most cases.
New
|
Would this be one way to ensure images are the correct size for the current screen/container size? https://developer.chrome.com/docs/lighthouse/performance/uses-responsive-images
|
@lloydjatkinson yes. It would generate a |
This has now moved to stage 3. Continue the discussion in the PR |
Summary
The current Astro image component offers a lot of flexibility for displaying images. It supports
densities
andwidths
props to help generate the correctimg
attributes, and the default image service supports modern formats such as AVIF and WebP. While this gives users the tools to create performant and responsive images, it does not give guidance in how to use them - and requires that they are set on all images. This proposal is for a more opinionated image component. It would offer all of the tools from the current component, and also introduce new props and config options that follow best practices by default.Background & Motivation
Displaying images on the web is difficult, even for the most experienced developers. Users suffer slower page loads, and poor experience as the page layout jumps around. Meanwhile sites experience poor core web vitals scores for performance, cumulative layout shift (CLS) and largest contentful paint (LCP).
The most common
img
tag attributes are well known:src
,alt
,width
andheight
, there are several lesser-known attributes that are needed if an image is to have the best performance. All of these are optional according to the spec, but best practices require most of them. The most important aresrcset
,sizes
,loading
,decoding
andfetchpriority
.These are a lot of attributes to remember and understand, though the final three have values that are usually safe to think of as dependent on just whether the image is onscreen when the page loads. Astro Image already sets
loading
anddecoding
tolazy
andasync
by default. Howeversrcset
andsizes
have no simple rules because they depend on how the image will be displayed, and can be very hard to do correctly. Images also need to be styled correctly if they are to be responsive and avoid CLS.This proposal is inspired by the attributes generated by
@unpic/astro
, which I created, but with some changes to make it closer to the existing component behavior, and less focussed on image CDNs.Goals
layout
prop that, when combined with existing props, sets the attributes that will make an image responsive and following best practices.Non-goals
Example
Responsive images will be enabled by setting the
layout
prop toresponsive
,fixed
orfull-width
.A new
layout
option for theimage
config will default all images to that layout. This can be overridden on each image.References
These served as inspiration, and/or are useful for understanding best practices:
The text was updated successfully, but these errors were encountered: