Image (Legacy)
Starting with Next.js 13, the next/image
component was rewritten to improve both the performance and developer experience. In order to provide a backwards compatible upgrade solution, the old next/image
was renamed to next/legacy/image
.
View the new next/image
API Reference
Comparison
Compared to next/legacy/image
, the new next/image
component has the following changes:
- Removes
<span>
wrapper around<img>
in favor of native computed aspect ratio - Adds support for canonical
style
prop- Removes
layout
prop in favor ofstyle
orclassName
- Removes
objectFit
prop in favor ofstyle
orclassName
- Removes
objectPosition
prop in favor ofstyle
orclassName
- Removes
- Removes
IntersectionObserver
implementation in favor of native lazy loading- Removes
lazyBoundary
prop since there is no native equivalent - Removes
lazyRoot
prop since there is no native equivalent
- Removes
- Removes
loader
config in favor ofloader
prop - Changed
alt
prop from optional to required - Changed
onLoadingComplete
callback to receive reference to<img>
element
Required Props
The <Image />
component requires the following properties.
src
Must be one of the following:
- A statically imported image file
- A path string. This can be either an absolute external URL, or an internal path depending on the loader prop or loader configuration.
When using the default loader, also consider the following for source images:
- When src is an external URL, you must also configure remotePatterns
- When src is animated or not a known format (JPEG, PNG, WebP, AVIF, GIF, TIFF) the image will be served as-is
- When src is SVG format, it will be blocked unless
unoptimized
ordangerouslyAllowSVG
is enabled
width
The width
property can represent either the rendered width or original width in pixels, depending on the layout
and sizes
properties.
When using layout="intrinsic"
or layout="fixed"
the width
property represents the rendered width in pixels, so it will affect how large the image appears.
When using layout="responsive"
, layout="fill"
, the width
property represents the original width in pixels, so it will only affect the aspect ratio.
The width
property is required, except for statically imported images, or those with layout="fill"
.
height
The height
property can represent either the rendered height or original height in pixels, depending on the layout
and sizes
properties.
When using layout="intrinsic"
or layout="fixed"
the height
property represents the rendered height in pixels, so it will affect how large the image appears.
When using layout="responsive"
, layout="fill"
, the height
property represents the original height in pixels, so it will only affect the aspect ratio.
The height
property is required, except for statically imported images, or those with layout="fill"
.
Optional Props
The <Image />
component accepts a number of additional properties beyond those which are required. This section describes the most commonly-used properties of the Image component. Find details about more rarely-used properties in the Advanced Props section.
layout
The layout behavior of the image as the viewport changes size.
layout | Behavior | srcSet | sizes | Has wrapper and sizer |
---|---|---|---|---|
intrinsic (default) | Scale down to fit width of container, up to image size | 1x , 2x (based on imageSizes) | N/A | yes |
fixed | Sized to width and height exactly | 1x , 2x (based on imageSizes) | N/A | yes |
responsive | Scale to fit width of container | 640w , 750w , ... 2048w , 3840w (based on imageSizes and deviceSizes) | 100vw | yes |
fill | Grow in both X and Y axes to fill container | 640w , 750w , ... 2048w , 3840w (based on imageSizes and deviceSizes) | 100vw | yes |
- Demo the
intrinsic
layout (default)- When
intrinsic
, the image will scale the dimensions down for smaller viewports, but maintain the original dimensions for larger viewports.
- When
- Demo the
fixed
layout- When
fixed
, the image dimensions will not change as the viewport changes (no responsiveness) similar to the nativeimg
element.
- When
- Demo the
responsive
layout- When
responsive
, the image will scale the dimensions down for smaller viewports and scale up for larger viewports. - Ensure the parent element uses
display: block
in their stylesheet.
- When
- Demo the
fill
layout- When
fill
, the image will stretch both width and height to the dimensions of the parent element, provided the parent element is relative. - This is usually paired with the
objectFit
property. - Ensure the parent element has
position: relative
in their stylesheet.
- When
- Demo background image
loader
A custom function used to resolve URLs. Setting the loader as a prop on the Image component overrides the default loader defined in the images
section of next.config.js
.
A loader
is a function returning a URL string for the image, given the following parameters:
Here is an example of using a custom loader:
import Image from 'next/legacy/image'
const myLoader = ({ src, width, quality }) => {
return `https://example.com/${src}?w=${width}&q=${quality || 75}`
}
const MyImage = (props) => {
return (
<Image
loader={myLoader}
src="me.png"
alt="Picture of the author"
width={500}
height={500}
/>
)
}
sizes
A string that provides information about how wide the image will be at different breakpoints. The value of sizes
will greatly affect performance for images using layout="responsive"
or layout="fill"
. It will be ignored for images using layout="intrinsic"
or layout="fixed"
.
The sizes
property serves two important purposes related to image performance:
First, the value of sizes
is used by the browser to determine which size of the image to download, from next/legacy/image
's automatically-generated source set. When the browser chooses, it does not yet know the size of the image on the page, so it selects an image that is the same size or larger than the viewport. The sizes
property allows you to tell the browser that the image will actually be smaller than full screen. If you don't specify a sizes
value, a default value of 100vw
(full screen width) is used.
Second, the sizes
value is parsed and used to trim the values in the automatically-created source set. If the sizes
property includes sizes such as 50vw
, which represent a percentage of the viewport width, then the source set is trimmed to not include any values which are too small to ever be necessary.
For example, if you know your styling will cause an image to be full-width on mobile devices, in a 2-column layout on tablets, and a 3-column layout on desktop displays, you should include a sizes property such as the following:
import Image from 'next/legacy/image'
const Example = () => (
<div className="grid-element">
<Image
src="/example.png"
layout="fill"
sizes="(max-width: 768px) 100vw,
(max-width: 1200px) 50vw,
33vw"
/>
</div>
)
This example sizes
could have a dramatic effect on performance metrics. Without the 33vw
sizes, the image selected from the server would be 3 times as wide as it needs to be. Because file size is proportional to the square of the width, without sizes
the user would download an image that's 9 times larger than necessary.
Learn more about srcset
and sizes
:
quality
The quality of the optimized image, an integer between 1
and 100
where 100
is the best quality. Defaults to 75
.
priority
When true, the image will be considered high priority and
preload. Lazy loading is automatically disabled for images using priority
.
You should use the priority
property on any image detected as the Largest Contentful Paint (LCP) element. It may be appropriate to have multiple priority images, as different images may be the LCP element for different viewport sizes.
Should only be used when the image is visible above the fold. Defaults to false
.
placeholder
A placeholder to use while the image is loading. Possible values are blur
or empty
. Defaults to empty
.
When blur
, the blurDataURL
property will be used as the placeholder. If src
is an object from a static import and the imported image is .jpg
, .png
, .webp
, or .avif
, then blurDataURL
will be automatically populated.
For dynamic images, you must provide the blurDataURL
property. Solutions such as Plaiceholder can help with base64
generation.
When empty
, there will be no placeholder while the image is loading, only empty space.
Try it out:
- Demo the
blur
placeholder - Demo the shimmer effect with
blurDataURL
prop - Demo the color effect with
blurDataURL
prop