Image Component and Image Optimization

    The Next.js Image component, , is an extension of the HTML element, evolved for the modern web. It includes a variety of built-in performance optimizations to help you achieve good Core Web Vitals. These scores are an important measurement of user experience on your website, and are .

    Some of the optimizations built into the Image component include:

    • Improved Performance: Always serve correctly sized image for each device, using modern image formats
    • Visual Stability: Prevent Cumulative Layout Shift automatically
    • Faster Page Loads: Images are only loaded when they enter the viewport, with optional blur-up placeholders
    • Asset Flexibility: On-demand image resizing, even for images stored on remote servers

    To add an image to your application, import the next/image component:

    Alternatively, you can import if you need a component much closer to the native <img> element:

      Now, you can define the src for your image (either local or remote).

      To use a local image, import your .jpg, .png, or .webp files:

      Dynamic await import() or require() are not supported. The import must be static so it can be analyzed at build time.

      Next.js will automatically determine the width and height of your image based on the imported file. These values are used to prevent while your image is loading.

      1. import Image from 'next/image'
      2. import profilePic from '../public/me.png'
      3. function Home() {
      4. return (
      5. <>
      6. <h1>My Homepage</h1>
      7. <Image
      8. src={profilePic}
      9. alt="Picture of the author"
      10. // width={500} automatically provided
      11. // height={500} automatically provided
      12. // blurDataURL="data:..." automatically provided
      13. // placeholder="blur" // Optional blur-up while loading
      14. />
      15. </>
      16. )

      To use a remote image, the src property should be a URL string, which can be relative or . Because Next.js does not have access to remote files during the build process, you’ll need to provide the width, and optional blurDataURL props manually:

      Sometimes you may want to optimize a remote image, but still use the built-in Next.js Image Optimization API. To do this, leave the loader at its default setting and enter an absolute URL for the Image src prop.

      To protect your application from malicious users, you must define a list of remote hostnames you intend to use with the next/image component.

      Note that in the example earlier, a partial URL ("/me.png") is provided for a remote image. This is possible because of the next/image architecture.

      A loader is a function that generates the URLs for your image. It modifies the provided src, and generates multiple URLs to request the image at different sizes. These multiple URLs are used in the automatic srcset generation, so that visitors to your site will be served an image that is the right size for their viewport.

      The default loader for Next.js applications uses the built-in Image Optimization API, which optimizes images from anywhere on the web, and then serves them directly from the Next.js web server. If you would like to serve your images directly from a CDN or image server, you can use one of the or write your own with a few lines of JavaScript.

      You should add the priority property to the image that will be the for each page. Doing so allows Next.js to specially prioritize the image for loading (e.g. through preload tags or priority hints), leading to a meaningful boost in LCP.

      The LCP element is typically the largest image or text block visible within the viewport of the page. When you run next dev, you’ll see a console warning if the LCP element is an <Image> without the priority property.

      Once you’ve identified the LCP image, you can add the property like this:

      1. import Image from 'next/image'
      2. export default function Home() {
      3. return (
      4. <>
      5. <h1>My Homepage</h1>
      6. <Image
      7. src="/me.png"
      8. alt="Picture of the author"
      9. width={500}
      10. height={500}
      11. priority
      12. <p>Welcome to my homepage!</p>
      13. </>
      14. )
      15. }

      See more about priority in the next/image component documentation.

      Image Sizing

      One of the ways that images most commonly hurt performance is through layout shift, where the image pushes other elements around on the page as it loads in. This performance problem is so annoying to users that it has its own Core Web Vital, called . The way to avoid image-based layout shifts is to always size your images. This allows the browser to reserve precisely enough space for the image before it loads.

      Because next/image is designed to guarantee good performance results, it cannot be used in a way that will contribute to layout shift, and must be sized in one of three ways:

      1. Automatically, using a
      2. Implicitly, by using layout=”fill” which causes the image to expand to fill its parent element.

      If none of the suggested methods works for sizing your images, the next/image component is designed to work well on a page alongside standard <img> elements.

      Styling the Image component is not that different from styling a normal <img> element, but there are a few guidelines to keep in mind:

      The image component has several different layout modes that define how it is sized on the page. If the styling of your image isn’t turning out the way you want, consider experimenting with other layout modes.

      Target the image with className, not based on DOM structure

      For most layout modes, the Image component will have a DOM structure of one <img> tag wrapped by exactly one <span>. For some modes, it may also have a sibling <span> for spacing. These additional <span> elements are critical to allow the component to prevent layout shifts.

      The recommended way to style the inner <img> is to set the className prop on the Image component to the value of an imported . The value of className will be automatically applied to the underlying <img> element.

      Alternatively, you can import a global stylesheet and manually set the className prop to the same name used in the global stylesheet.

      You cannot use because it’s scoped to the current component.

      When using layout='fill', the parent element must have position: relative

      This is necessary for the proper rendering of the image element in that layout mode.

      When using layout='responsive', the parent element must have display: block

      This is the default for <div> elements but should be specified otherwise.

      View all properties available to the next/image component.

      For examples of the Image component used with the various fill modes, see the Image component example app.

      The component and Next.js Image Optimization API can be configured in the next.config.js file. These configurations allow you to , define custom image breakpoints, and more.

      Read the full image configuration documentation for more information.

      For more information on what to do next, we recommend the following sections: