1

I have a component with dynamic content like so : <Image width="680" height="400" src={post.imgsrc} alt={post.title} />

I keep having the same error even if I have added the width/height :

Image with src "/imgs/paintings/2022/2.jpg" must use "width" and "height" properties or "layout='fill'" property.

I don't know why, and I wonder why I have to specify width and height since my pictures have different sizes...

2
  • 3
    You need to provide numeric values: <Image width={680} height={400} .... And the reason for requiring them is to prevent cumulative layout shift. Commented Feb 8, 2022 at 12:45
  • Thank you very much, didn't know about this ! Commented Feb 9, 2022 at 13:14

1 Answer 1

1

One thing to mention is that you can import the images if they are local, if you want that you do not need to use width, height, or fill. They get provided automatically

Example from the docs:

import profilePic from '../public/me.png'

import Image from 'next/image'
import profilePic from '../public/me.png'

function Home() {
  return (
    <>
      <h1>My Homepage</h1>
      <Image
        src={profilePic}
        alt="Picture of the author"
        // width={500} automatically provided
        // height={500} automatically provided
        // blurDataURL="data:..." automatically provided
        // placeholder="blur" // Optional blur-up while loading
      />
      <p>Welcome to my homepage!</p>
    </>
  )
}

This also explains why remote images do need these properties.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, I get it now, I used the import method and it's working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.