next/image ignores layout, objectFit and layout="fill"
Next.JS Image `layout='fill'` is brokenShort answer
Since Next.js 13 the next/image component no longer accepts layout, objectFit or objectPosition. Use the boolean fill prop, give the parent element position relative, and set object-fit through style or a class. If you need the old behaviour unchanged, import the legacy component instead.
Fix it fast
1Find out which cause you have
Search the code for the old props and for the import path. They tell you which of the causes below applies.
grep -rn "layout=\|objectFit=\|objectPosition=\|next/legacy/image" app components pages src 2>/dev/null2Match what you see, then jump to its fix
- 1
The image is the wrong size or does not cover its box, and the JSX still has layout="fill" objectFit="cover".The old layout and objectFit props are still on the componentFix - 2
With fill the image overflows the page, covers other content or has zero height.The parent element is not positionedFix - 3
Many files use layout="responsive", "fixed" or "intrinsic" and a rewrite is not possible right now.A project still depends on the pre-13 componentFix
- 1
What does the error look like?
Next.JS Image `layout='fill'` is brokenlayout, objectFit, objectPosition, lazyBoundary, lazyRoot props removed
Cause 1 of 3
The old layout and objectFit props are still on the component
1Check
The Next.js 13 release removed the layout, objectFit, objectPosition, lazyBoundary and lazyRoot props from next/image.
grep -rn "layout=" app components pages 2>/dev/null2Fix
Replace layout="fill" with the fill prop and move objectFit into style. A component that uses fill must also have alt.
// before
<Image src="/hero.jpg" layout="fill" objectFit="cover" alt="" />
// after
<Image src="/hero.jpg" fill style={{ objectFit: 'cover' }} alt="Hero" />3Verify
Reload the page and check that the image fills its box and is cropped, not stretched.
npm run devConfirmed by nextjs.org
Cause 2 of 3
The parent element is not positioned
1Check
An image with fill is position absolute. It sizes against the nearest positioned ancestor, so that ancestor needs position relative, fixed or absolute. The parent also needs a size.
grep -rn -B2 "fill" app components | grep -n "position\|relative"2Fix
Wrap the image in an element with position relative and a width and height (or an aspect ratio).
<div style={{ position: 'relative', width: '400px', height: '300px' }}>
<Image src="/hero.jpg" alt="Hero" fill style={{ objectFit: 'cover' }} />
</div>3Verify
Inspect the page: the img element stays inside the wrapper box.
npm run devConfirmed by nextjs.org
Cause 3 of 3
A project still depends on the pre-13 component
1Check
In v13.0.0 the old component was renamed to next/legacy/image, and a codemod renames imports.
grep -rln "from 'next/image'" app components pages 2>/dev/null | head2Fix
Import the legacy component where you still need the old props, and plan the move to fill, or to width and height, file by file.
import Image from 'next/legacy/image'3Verify
The page renders as it did before the upgrade.
npm run buildConfirmed by nextjs.org
How do I stop it coming back?
- Give every fill image a positioned wrapper with a defined size, in the same commit.
- Pass the sizes prop with fill so the browser does not download an image sized for the whole viewport.
- Set width and height on images whose size is known; use fill only when it is not.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
next/image shows nothing for an image from another domain
next/image shows a broken image only for external src values
next/image only optimizes remote images that match an entry in images.remotePatterns in next.config.
3 causes, each with a fixWeb apps
AuthUserPoolException: Auth UserPool not configured in a Next.js app
Auth UserPool not configured
The error means an Amplify Auth call ran before Amplify.configure did.
3 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fix