JS Guide
HomeQuestionsSearchResources
Search

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsnextjs
PrevNext
nextjs
junior
optimization

What is next/image and why should you use it over the regular img tag?

nextjs
image
optimization
performance
lazy-loading
webp
Quick Answer

The `next/image` component automatically optimizes images with lazy loading, responsive sizing, modern formats (WebP/AVIF), and prevents Cumulative Layout Shift (CLS), improving both performance and Core Web Vitals.

Detailed Explanation

Benefits of next/image:

  1. Automatic Optimization

    • Converts to modern formats (WebP, AVIF)
    • Resizes based on device
    • Compresses images
  2. Lazy Loading

    • Images load only when entering viewport
    • Reduces initial page load time
  3. Prevents Layout Shift

    • Reserves space for images before they load
    • Improves CLS (Core Web Vital)
  4. Responsive Images

    • Automatically serves correct size
    • sizes prop for responsive behavior
  5. Blur Placeholder

    • Shows blurred version while loading
    • Better user experience

Required Props:

  • src - Image source (local or remote)
  • alt - Accessibility text
  • width & height - Required for remote images (or use fill)

Code Examples

Basic Image usage
import Image from 'next/image';

// Local image (auto width/height detection)
import profilePic from '../public/profile.jpg';

export default function Profile() {
  return (
    <div>
      {/* Local image - width/height inferred */}
      <Image
        src={profilePic}
        alt="Profile picture"
        placeholder="blur" // Built-in blur for local images
      />
      
      {/* Remote image - must specify dimensions */}
      <Image
        src="https://example.com/photo.jpg"
        alt="Remote photo"
        width={500}
        height={300}
      />
    </div>
  );
}

Resources

Next.js Image Component

docs

Image Optimization

docs

Related Questions

What performance optimization techniques does Next.js provide?

senior
performance

What is Next.js and how does it differ from React?

junior
basics
Previous
How do you navigate between pages in Next.js?
Next
What's the difference between SSR and SSG in Next.js?