Responsive images help your website show the right image size for different screen sizes. This makes pages load faster and look good on phones, tablets, and desktops.
Responsive images in NextJS
import Image from 'next/image' <Image src="/path/to/image.jpg" alt="Description of image" width={width} height={height} sizes="(max-width: 600px) 100vw, 50vw" />
The Image component from Next.js automatically optimizes images.
The sizes attribute tells the browser how wide the image will be at different screen widths.
import Image from 'next/image' <Image src="/dog.jpg" alt="A cute dog" width={800} height={600} sizes="(max-width: 600px) 100vw, 50vw" />
priority to load it faster because it is important for the page.import Image from 'next/image' <Image src="/logo.png" alt="Company logo" width={400} height={100} priority />
fill prop to make the image fill its parent container and cover it nicely.import Image from 'next/image' <Image src="/landscape.jpg" alt="Beautiful landscape" fill style={{ objectFit: 'cover' }} />
This component shows a nature image that changes size depending on the screen width. It uses Next.js Image with sizes to load the best image size. The image has rounded corners for a nice look.
import Image from 'next/image' export default function ResponsiveImageExample() { return ( <main style={{ maxWidth: '800px', margin: 'auto' }}> <h1>Responsive Image Example</h1> <Image src="/nature.jpg" alt="Nature scene" width={800} height={500} sizes="(max-width: 600px) 100vw, 50vw" style={{ borderRadius: '8px' }} /> <p>This image adjusts size based on your screen width.</p> </main> ) }
Always provide a meaningful alt text for accessibility.
Use the sizes attribute to help browsers pick the right image size.
The Next.js Image component automatically optimizes images for performance.
Responsive images load the right size for different screens to improve speed and look.
Next.js Image component makes it easy to use responsive images.
Remember to use alt text and sizes for best results.
Practice
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 AQuick Check:
Responsive images = load right size fast [OK]
- Thinking responsive images add captions automatically
- Believing images turn black and white on small screens
- Assuming images are disabled on mobile devices
Solution
Step 1: Check import syntax
Next.js Image is imported as default from 'next/image'. So, import Image from 'next/image' is correct.Step 2: Check usage with required props
Image requires src, width, height, and alt for accessibility and layout.Final Answer:
import Image from 'next/image'; <Image src='/pic.jpg' width={500} height={300} alt='pic' /> -> Option AQuick Check:
Correct import + width/height + alt = import Image from 'next/image'; [OK]
- Importing Image from 'react' instead of 'next/image'
- Omitting width and height props
- Using wrong component name like Img
import Image from 'next/image';
export default function Photo() {
return (
<Image
src='/photo.jpg'
alt='A photo'
sizes='(max-width: 600px) 100vw, 50vw'
width={800}
height={600}
/>
);
}Solution
Step 1: Understand sizes prop usage
The sizes prop defines image display width based on screen width media queries.Step 2: Interpret sizes value
For screens max 600px wide, image uses 100vw (full viewport width). Otherwise, 50vw (half viewport width).Final Answer:
Loads 100% viewport width image on screens 600px or smaller, else 50% viewport width. -> Option DQuick Check:
Sizes prop controls image width by screen size [OK]
- Thinking sizes sets image pixel dimensions directly
- Assuming sizes prop causes error
- Believing image loads only on wide screens
import Image from 'next/image';
export default function Banner() {
return (
<Image
src='/banner.png'
alt='Banner'
width='100%'
height='300'
/>
);
}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 CQuick Check:
Width must be numeric pixels [OK]
- Using percentage strings for width or height
- Thinking alt text is optional
- Believing src must be external URL
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.Final Answer:
<Image src='/post.jpg' alt='Blog post' width={1200} height={800} sizes='(max-width: 768px) 100vw, 50vw' priority /> -> Option BQuick Check:
Alt + sizes + priority = best practice [OK]
- Leaving alt text empty
- Using invalid width/height values
- Incorrect sizes media query order
Ready to prove it?
Pass the test and this pattern moves unknown
