Bird
Raised Fist0
NextJSframework~5 mins

Image optimization with next/image in NextJS

Choose your learning style10 modes available
Introduction

Using next/image helps your website load images faster and look better on all devices. It automatically adjusts image size and quality to save data and improve speed.

When you want your website images to load quickly on phones and computers.
When you want to save visitors' data by sending smaller images.
When you want images to look sharp on high-resolution screens.
When you want to easily add lazy loading so images load only when visible.
When you want to support modern image formats without extra work.
Syntax
NextJS
import Image from 'next/image'

export default function Example() {
  return (
    <Image
      src="/path/to/image.jpg"
      alt="Description of image"
      width={500}
      height={300}
      quality={75}
      priority={true}
      placeholder="blur"
      blurDataURL="data:image/jpeg;base64,..."
    />
  )
}

The src can be a local file path or an external URL if configured.

Always provide alt text for accessibility.

Examples
Basic usage with local image and fixed size.
NextJS
import Image from 'next/image'

export default function Logo() {
  return <Image src="/logo.png" alt="Company logo" width={100} height={100} />
}
Using an external image URL with custom quality.
NextJS
import Image from 'next/image'

export default function ExternalPhoto() {
  return <Image src="https://example.com/photo.jpg" alt="Sample photo" width={800} height={600} quality={80} />
}
Shows a blurred placeholder while the image loads.
NextJS
import Image from 'next/image'

export default function BlurredImage() {
  return <Image src="/landscape.jpg" alt="Beautiful landscape" width={700} height={400} placeholder="blur" blurDataURL="data:image/jpeg;base64,..." />
}
Marks image as high priority to load faster on page load.
NextJS
import Image from 'next/image'

export default function HeroImage() {
  return <Image src="/hero.jpg" alt="Hero image" width={1200} height={800} priority />
}
Sample Program

This component shows a main page with a heading, an optimized image using next/image, and a paragraph. The image uses a blurred placeholder and is marked as priority to load fast.

NextJS
import Image from 'next/image'

export default function HomePage() {
  return (
    <main style={{ maxWidth: '600px', margin: 'auto', padding: '1rem' }}>
      <h1>Welcome to Our Site</h1>
      <Image
        src="/nature.jpg"
        alt="A scenic view of mountains and lake"
        width={600}
        height={400}
        placeholder="blur"
        blurDataURL="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD..."
        priority
      />
      <p>This image loads quickly and looks great on all devices.</p>
    </main>
  )
}
OutputSuccess
Important Notes

Always specify width and height to prevent layout shifts.

Use priority only for important images to avoid slowing down other content.

Configure next.config.js to allow external image domains if needed.

Summary

next/image automatically optimizes images for faster loading and better quality.

Use it to improve user experience on all devices and save bandwidth.

Remember to provide alt text and set image dimensions for best results.

Practice

(1/5)
1. What is the main benefit of using next/image in a Next.js project?
easy
A. It only works with SVG images.
B. It converts images to text for SEO purposes.
C. It disables image loading to save bandwidth.
D. It automatically optimizes images for faster loading and better quality.

Solution

  1. Step 1: Understand the purpose of next/image

    The next/image component is designed to optimize images automatically, improving loading speed and quality.
  2. Step 2: Compare options with the purpose

    Only It automatically optimizes images for faster loading and better quality. correctly describes this benefit. Other options describe unrelated or incorrect features.
  3. Final Answer:

    It automatically optimizes images for faster loading and better quality. -> Option D
  4. Quick Check:

    Image optimization = D [OK]
Hint: Remember next/image optimizes images automatically [OK]
Common Mistakes:
  • Thinking next/image converts images to text
  • Believing it disables image loading
  • Assuming it only supports SVG
2. Which of the following is the correct way to import and use the Image component from Next.js for image optimization?
easy
A. import { Image } from 'react';
B. import Image from 'next/image';
C. import Img from 'next/image';
D. import Image from 'next/image';

Solution

  1. Step 1: Check import syntax

    The correct import for Next.js image optimization is import Image from 'next/image';.
  2. Step 2: Verify usage with required props

    The Image component requires src, alt, and explicit width and height for optimization. import Image from 'next/image';
    includes all correctly.
  3. Final Answer:

    import Image from 'next/image'; with src, alt, width, and height props -> Option B
  4. Quick Check:

    Correct import and props = B [OK]
Hint: Always import Image from 'next/image' with width and height [OK]
Common Mistakes:
  • Importing Image from 'react' instead of 'next/image'
  • Omitting width and height props
  • Using wrong import names
3. What will be the rendered output behavior of this Next.js code snippet?
import Image from 'next/image';

export default function Logo() {
  return (
    <Image src='/logo.png' alt='Company Logo' width={200} height={100} priority />
  );
}
medium
A. The image loads immediately with 200x100 size due to priority prop.
B. The image will not render because priority is invalid.
C. The image loads lazily by default with 200x100 size.
D. The image loads with default size ignoring width and height.

Solution

  1. Step 1: Understand the priority prop effect

    The priority prop forces the image to load immediately instead of lazy loading.
  2. Step 2: Check width and height usage

    Width and height props set the image size to 200x100 pixels, which will be respected.
  3. Final Answer:

    The image loads immediately with 200x100 size due to priority prop. -> Option A
  4. Quick Check:

    priority = immediate load = C [OK]
Hint: priority prop disables lazy loading for faster display [OK]
Common Mistakes:
  • Assuming priority disables image rendering
  • Thinking width and height are ignored
  • Confusing lazy loading default behavior
4. Identify the error in this Next.js image usage and how to fix it:
import Image from 'next/image';

export default function Profile() {
  return (
    <Image src='/profile.jpg' alt='User Profile' width='150' height='150' />
  );
}
medium
A. src path must be an external URL, not local path.
B. alt attribute is missing, causing accessibility issues.
C. width and height should be numbers, not strings.
D. Image component must be self-closing with a slash.

Solution

  1. Step 1: Check width and height prop types

    Next.js Image requires width and height as numbers, not strings. Here they are strings ('150').
  2. Step 2: Correct the prop types

    Remove quotes to pass numbers: width={150} and height={150}.
  3. Final Answer:

    width and height should be numbers, not strings. -> Option C
  4. Quick Check:

    width/height as numbers = A [OK]
Hint: Use curly braces for numeric width and height [OK]
Common Mistakes:
  • Passing width and height as strings
  • Ignoring alt attribute importance
  • Thinking local src paths are invalid
5. You want to optimize multiple images with different sizes responsively using next/image. Which approach correctly uses the sizes attribute to serve appropriate image sizes on different screen widths?
hard
A. <Image src='/photo.jpg' alt='Photo' width={800} height={600} sizes='(max-width: 600px) 100vw, 50vw' />
B. <Image src='/photo.jpg' alt='Photo' width='800' height='600' sizes='100vw' />
C. <Image src='/photo.jpg' alt='Photo' width={800} height={600} sizes='(min-width: 600px) 100vw, 50vw' />
D. <Image src='/photo.jpg' alt='Photo' width={800} height={600} />

Solution

  1. Step 1: Understand the sizes attribute usage

    The sizes attribute defines image display sizes for different viewport widths using media queries.
  2. Step 2: Analyze the options

    <Image src='/photo.jpg' alt='Photo' width={800} height={600} sizes='(max-width: 600px) 100vw, 50vw' /> correctly uses (max-width: 600px) 100vw, 50vw meaning full viewport width on small screens, half on larger screens. This is a common responsive pattern.
  3. Final Answer:

    Use sizes='(max-width: 600px) 100vw, 50vw' with numeric width and height. -> Option A
  4. Quick Check:

    Responsive sizes attribute = A [OK]
Hint: Use sizes with max-width media query for responsive images [OK]
Common Mistakes:
  • Passing width and height as strings
  • Using min-width instead of max-width incorrectly
  • Omitting sizes attribute for responsiveness
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard