0
0
NextJSframework~10 mins

Responsive images in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Responsive images
Start: Define Image Component
Set src, width, height, sizes
Next.js Image optimizes image
Browser loads image
Browser checks viewport size
Browser selects best image size
Image displays responsively
End
This flow shows how Next.js Image component uses provided sizes and dimensions to serve the best image size for the user's screen, making images responsive.
Execution Sample
NextJS
import Image from 'next/image'

export default function Example() {
  return <Image src="/photo.jpg" width={800} height={600} sizes="(max-width: 600px) 100vw, 800px" alt="Sample" />
}
This code renders a responsive image that adjusts size based on screen width using Next.js Image component.
Execution Table
StepActionInput/StateResult/Output
1Component renderssrc='/photo.jpg', width=800, height=600, sizes='(max-width: 600px) 100vw, 800px'Next.js prepares image with multiple sizes
2Browser loads pageViewport width unknown yetImage placeholder reserved with correct aspect ratio
3Browser evaluates viewport widthViewport width = 500px (example)Matches '(max-width: 600px) 100vw' condition
4Browser selects image sizesizes='100vw' means 100% viewport widthLoads image sized to 500px width
5Image displaysImage loaded at 500px widthImage fits screen responsively
6Resize browser to 700pxViewport width = 700pxMatches fallback '800px' size
7Browser selects image sizesizes='800px'Loads image sized to 800px width
8Image displaysImage loaded at 800px widthImage fits larger screen
9EndAll steps completeResponsive image behavior achieved
💡 Execution stops after image loads and displays responsively for current viewport size.
Variable Tracker
VariableStartAfter Step 3After Step 6Final
viewportWidthunknown500700700
selectedImageWidthnone500800800
imageDisplayednoyes (500px wide)yes (800px wide)yes (800px wide)
Key Moments - 3 Insights
Why does the image load at 500px width when the viewport is 500px?
Because the sizes attribute says for max-width 600px use 100vw (100% viewport width), so the browser picks an image sized to 500px to fit exactly.
What happens when the viewport width grows larger than 600px?
The browser uses the fallback size '800px' from sizes attribute and loads a larger image to fit the bigger screen.
Why do we provide width and height props if the image size changes?
Width and height define the original image's aspect ratio so Next.js can reserve space and avoid layout shifts while loading responsive sizes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the selectedImageWidth at step 4?
A600
B500
C800
D1000
💡 Hint
Check the 'selectedImageWidth' value after step 3 and step 4 in the execution_table.
At which step does the viewport width change to 700px?
AStep 6
BStep 3
CStep 2
DStep 8
💡 Hint
Look for the step where viewportWidth changes from 500 to 700 in variable_tracker and execution_table.
If the sizes attribute was '(max-width: 400px) 100vw, 600px', what image width would load at viewport 500px?
A500px
B400px
C600px
D800px
💡 Hint
Check how sizes fallback works when viewport is larger than max-width condition in execution_table.
Concept Snapshot
Next.js Responsive Images
- Use <Image> with src, width, height, sizes props
- sizes tells browser which image width to pick based on viewport
- Browser picks best image size for screen
- Width/height reserve space to prevent layout shift
- Responsive images improve load speed and look good on all devices
Full Transcript
This visual execution shows how Next.js Image component handles responsive images. The component is given a source image, fixed width and height, and a sizes attribute that tells the browser how wide the image should be at different screen widths. When the page loads, the browser checks the viewport width and uses the sizes attribute to pick the best image size to load. For example, if the viewport is 500 pixels wide and sizes says to use 100vw for max-width 600px, the browser loads a 500 pixel wide image. If the viewport grows to 700 pixels, the browser uses the fallback size of 800 pixels. The width and height props help Next.js reserve space to avoid layout shifts while the image loads. This process ensures images look sharp and load efficiently on all screen sizes.