11

I'm trying to add the Next Image component to my project.

And I have a problem, the image disappears when I add layout='responsive'.

code:

<Box>
    <Link href='/' >
        <Image src='/images/logoDark.svg'
            alt='navbar-logo'
            width={260}
            height={56}
            layout='responsive'
        />
    </Link>
</Box>

Is there a solution? or any other way to optimize images?

juliomalves
  • 42,130
  • 20
  • 150
  • 146
Marko B.
  • 535
  • 3
  • 8
  • 18

5 Answers5

5

Based on what is found here (https://github.com/vercel/next.js/issues/19314), it seems layout='responsive' has no intrinsic size. So you will have to add a width to the containing element.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jac Wynn
  • 96
  • 2
4

You need to wrap the next/image into a container with display:block, so that the image will be displayed.

kyun
  • 9,710
  • 9
  • 31
  • 66
Sorin
  • 111
  • 1
  • 6
2

set height and width for the parent of the Image component :

<Box width={263} height={56}>
    <Link href='/' >
        <Image src='/images/logoDark.svg'
            alt='navbar-logo'
            width={260}
            height={56}
            layout='responsive'
        />
    </Link>
</Box>

ensure the parent element uses display: block in their stylesheet like Box component or div

Ramin eghbalian
  • 2,348
  • 1
  • 16
  • 36
1

layout = responsive is no longer valid it is deprecated. This How used Image in Nextjs responsively

    <Box
        position={'relative'}
        width={526}
        height={526}
      >
        <Image
          src={Myimage}
          fill
          alt=''
        ></Image>
      </Box>

Make sure you wrap the Image with Box and give it a width and a height and also important to give the position={'relative'} if you are using 'fill' prop inside Image.

Image is Responsive like a charm!!!

0

Component

<div className="photo">
    <Image
    width={300}
    height={300}
    src="https:/images.unsplash.com/photo-1501432377862-3d0432b87a14?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80"
    alt="photo"
    layout="raw"
    />
</div>
<style jsx>{`
    @media (max-width: 768px) {
        .photo {
        margin: 0 auto;
        justify-content: center;
        display: flex;
        width: 200px;
        height: 200px;
        }
    }
`}</style>

next.config.js

experimental: {
 images: {
  layoutRaw: true,
 },
},

This worked for me, the image doesn't disappear and is responsive,

csgeek
  • 711
  • 6
  • 15