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.
Image optimization with next/image in 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.
import Image from 'next/image' export default function Logo() { return <Image src="/logo.png" alt="Company logo" width={100} height={100} /> }
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} /> }
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,..." /> }
import Image from 'next/image' export default function HeroImage() { return <Image src="/hero.jpg" alt="Hero image" width={1200} height={800} priority /> }
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.
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> ) }
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.
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
next/image in a Next.js project?Solution
Step 1: Understand the purpose of next/image
Thenext/imagecomponent is designed to optimize images automatically, improving loading speed and quality.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.Final Answer:
It automatically optimizes images for faster loading and better quality. -> Option DQuick Check:
Image optimization = D [OK]
- Thinking next/image converts images to text
- Believing it disables image loading
- Assuming it only supports SVG
Image component from Next.js for image optimization?Solution
Step 1: Check import syntax
The correct import for Next.js image optimization isimport Image from 'next/image';.Step 2: Verify usage with required props
TheImagecomponent requiressrc,alt, and explicitwidthandheightfor optimization. import Image from 'next/image';
includes all correctly.Final Answer:
import Image from 'next/image'; with src, alt, width, and height props -> Option BQuick Check:
Correct import and props = B [OK]
- Importing Image from 'react' instead of 'next/image'
- Omitting width and height props
- Using wrong import names
import Image from 'next/image';
export default function Logo() {
return (
<Image src='/logo.png' alt='Company Logo' width={200} height={100} priority />
);
}Solution
Step 1: Understand the priority prop effect
Thepriorityprop forces the image to load immediately instead of lazy loading.Step 2: Check width and height usage
Width and height props set the image size to 200x100 pixels, which will be respected.Final Answer:
The image loads immediately with 200x100 size due to priority prop. -> Option AQuick Check:
priority = immediate load = C [OK]
- Assuming priority disables image rendering
- Thinking width and height are ignored
- Confusing lazy loading default behavior
import Image from 'next/image';
export default function Profile() {
return (
<Image src='/profile.jpg' alt='User Profile' width='150' height='150' />
);
}Solution
Step 1: Check width and height prop types
Next.jsImagerequireswidthandheightas numbers, not strings. Here they are strings ('150').Step 2: Correct the prop types
Remove quotes to pass numbers:width={150}andheight={150}.Final Answer:
width and height should be numbers, not strings. -> Option CQuick Check:
width/height as numbers = A [OK]
- Passing width and height as strings
- Ignoring alt attribute importance
- Thinking local src paths are invalid
next/image. Which approach correctly uses the sizes attribute to serve appropriate image sizes on different screen widths?Solution
Step 1: Understand the sizes attribute usage
Thesizesattribute defines image display sizes for different viewport widths using media queries.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, 50vwmeaning full viewport width on small screens, half on larger screens. This is a common responsive pattern.Final Answer:
Use sizes='(max-width: 600px) 100vw, 50vw' with numeric width and height. -> Option AQuick Check:
Responsive sizes attribute = A [OK]
- Passing width and height as strings
- Using min-width instead of max-width incorrectly
- Omitting sizes attribute for responsiveness
Ready to prove it?
Pass the test and this pattern moves unknown
