Nextjs Fundamentals · Optimization & Metadata

Optimizing images

`next/image`

Optimizing images

next/image extends the HTML <img> element with automatic optimization: correctly sized images per device, modern formats like WebP, lazy loading, and protection against layout shift.

Import Image from next/image and render it:

import Image from 'next/image';

export default function Page() {
  return <Image src="/profile.png" alt="Picture of the author" width={500} height={500} />;
}

A local image lives under public/ and is referenced by a path. If you import it statically, Next.js infers width, height, and a blur placeholder for you. A remote image needs width and height — to reserve the right aspect ratio — or the fill prop, and its host must be allowed in next.config.ts under images.remotePatterns.

For responsive images, pass sizes so the browser downloads the smallest image that fits, for example sizes="(max-width: 768px) 100vw, 33vw". fill makes the image stretch to its positioned parent.

In short: use next/image for local and remote images, supply dimensions or fill, and set sizes to avoid oversized downloads.

You are about to answer a short quiz on next/image.

  1. next/image automatically optimizes images, serving modern formats and preventing layout shift.
  2. A remote image needs width and height, or the ___ prop, to fill its parent.
  3. For a statically imported local image, Next.js infers width and height automatically.
  4. The ___ prop tells the browser which image size to download for a responsive image.
  5. next/image lazy-loads images by default.