These additional elements are critical to allow the component to prevent layout shifts. For some modes, it may also have a sibling for spacing. Target the image with className, not based on DOM structureįor most layout modes, the Image component will have a DOM structure of one tag wrapped by exactly one. If the styling of your image isn't turning out the way you want, consider experimenting with other layout modes. The image component has several different layout modes that define how it is sized on the page. Styling the Image component is not that different from styling a normal element, but there are a few guidelines to keep in mind: Note: Many of the styling issues listed below can be solved with next/future/image 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 elements. If your application is retrieving image URLs using an API call (such as to a CMS), you may be able to modify the API call to return the image dimensions along with the URL. If you're serving images from a source that you control, consider modifying your image pipeline to normalize the images to a specific size. Consider using CSS to give the image's parent element space on the page, then using the objectFit property with fill, contain, or cover, along with the objectPosition property to define how the image should occupy that space. The fill layout mode allows your image to be sized by its parent element. If you are accessing images from a source without knowledge of the images' sizes, there are several things you can do: What if I don't know the size of my images? Implicitly, by using layout="fill" which causes the image to expand to fill its parent element.Explicitly, by including a width and height property.This allows the browser to reserve precisely enough space for the image before it loads.īecause 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: The way to avoid image-based layout shifts is to always size your images. This performance problem is so annoying to users that it has its own Core Web Vital, called Cumulative Layout Shift. 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. See more about priority in the next/image component documentation. import Image from 'next/image' import profilePic from './public/me.png' function Home ( ) These values are used to prevent Cumulative Layout Shift while your image is loading. Next.js will automatically determine the width and height of your image based on the imported file. The import must be static so it can be analyzed at build time. webp files: import profilePic from './public/me.png'ĭynamic await import() or require() are not supported. Now, you can define the src for your image (either local or remote). To add an image to your application, import the next/image component: import Image from 'next/image'Īlternatively, you can import next/future/image if you need a component much closer to the native element: import Image from 'next/future/image' Asset Flexibility: On-demand image resizing, even for images stored on remote servers.Faster Page Loads: Images are only loaded when they enter the viewport, with optional blur-up placeholders.Visual Stability: Prevent Cumulative Layout Shift automatically.Improved Performance: Always serve correctly sized image for each device, using modern image formats.Some of the optimizations built into the Image component include: These scores are an important measurement of user experience on your website, and are factored into Google's search rankings. It includes a variety of built-in performance optimizations to help you achieve good Core Web Vitals. The Next.js Image component, next/image, is an extension of the HTML element, evolved for the modern web. Image Component and Image Optimization Examples