📖 Scenario: You are building a simple Next.js app that shows a product image. You want the image to load quickly and look good on all screen sizes by using responsive images.
🎯 Goal: Create a Next.js component that displays a responsive product image using the built-in next/image component with different sizes for mobile, tablet, and desktop screens.
📋 What You'll Learn
Use the next/image component to display the image
Set the src to '/images/product.jpg'
Add an alt text 'Product Image' for accessibility
Use the sizes attribute to specify image widths for different screen sizes
Set the width and height props to 800 and 600 respectively
💡 Why This Matters
🌍 Real World
Responsive images improve website speed and user experience on phones, tablets, and desktops by loading the right image size.
💼 Career
Knowing how to use Next.js Image component for responsive images is essential for frontend developers working on modern React and Next.js projects.
Progress0 / 4 steps
1
Setup the Next.js Image import and basic component
Import the Image component from next/image and create a functional component called ProductImage that returns an empty fragment <></>.
NextJS
Hint
Use import Image from 'next/image' at the top. Then create a function ProductImage that returns an empty React fragment.
2
Add the Image component with src, alt, width, and height
Inside the ProductImage component, add the <Image> component with src='/images/product.jpg', alt='Product Image', width={800}, and height={600}.
NextJS
Hint
Use the Image component with the exact props: src='/images/product.jpg', alt='Product Image', width={800}, and height={600}.
3
Add the sizes attribute for responsive image widths
Add the sizes attribute to the <Image> component with the value '(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 33vw' to specify image widths for mobile, tablet, and desktop screens.
NextJS
Hint
The sizes attribute helps the browser pick the right image size for different screen widths. Use the exact string given.
4
Wrap the Image in a responsive container with semantic HTML
Wrap the <Image> component inside a <section> element with an aria-label of 'Product image section' to improve accessibility and semantic structure.
NextJS
Hint
Use a <section> tag with aria-label='Product image section' wrapping the <Image> component.
Practice
(1/5)
1. What is the main benefit of using responsive images in a Next.js app?
easy
A. They load the right image size for different screen widths, improving speed.
B. They automatically add captions to images.
C. They convert images to black and white on small screens.
D. They disable images on mobile devices to save data.
Solution
Step 1: Understand responsive images purpose
Responsive images adjust to screen size to load faster and look better.
Step 2: Identify Next.js Image component benefit
Next.js Image component helps load the right image size automatically.
Final Answer:
They load the right image size for different screen widths, improving speed. -> Option A
Quick Check:
Responsive images = load right size fast [OK]
Hint: Responsive images adapt size to screen for speed [OK]
4. Identify the error in this Next.js Image usage:
import Image from 'next/image';
export default function Banner() {
return (
<Image
src='/banner.png'
alt='Banner'
width='100%'
height='300'
/>
);
}
medium
A. alt text is missing.
B. src path must be an external URL.
C. width should be a number, not a string with '%'.
D. height cannot be set with a number.
Solution
Step 1: Check width prop type
Next.js Image expects width as a number (pixels), not a string with '%'.
Step 2: Verify other props
alt is present, src can be local path, height as number is valid.
Final Answer:
width should be a number, not a string with '%'. -> Option C
Quick Check:
Width must be numeric pixels [OK]
Hint: Width and height props must be numbers, not strings [OK]
Common Mistakes:
Using percentage strings for width or height
Thinking alt text is optional
Believing src must be external URL
5. You want to optimize images for a blog with different screen sizes. Which approach correctly uses Next.js Image to load responsive images with accessibility and performance in mind?
hard
A.
B.
C.
D.
Solution
Step 1: Check alt text for accessibility
includes meaningful alt text 'Blog post', improving accessibility.
Step 2: Verify sizes and priority usage
Sizes prop correctly sets image width for small and larger screens. Priority loads important images faster.
Step 3: Identify issues in other options
A lacks alt text and reverses sizes logic, B has empty alt (bad for accessibility), D uses invalid string props for width/height.