Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.JS Image `layout='fill'` is broken

When using the Next.js image component, the docs claim that: "When fill, the image will stretch both width and height to the dimensions of the parent element, usually paired with object-fit."

However, this is not what's happening. What it actually does is fill the image to take up the entire screen in a fixed position that doesn't respect scrolling. I've tried each object-fit value possible for the img and none has worked.

To reproduce, make a new next project and put an image in your public folder. Do this:

export default function Home() {
  return (
    <div>
      <div style={{width: '100px', height: '100px'}}>
        <Image src={"/i.png"} layout='fill'/>
      </div>
    </div>
  )
}

Image will take up the entire screen. You can try styling the Image component but I haven't found a way to make it work.

Does anyone know how to fix this, or why it's happening?

like image 218
ICW Avatar asked May 06 '21 15:05

ICW


People also ask

Can we use IMG tag in next JS?

There are two ways you can display images in Next. js, you either use the conventional <img> tag or a specialized <Image/> component that is unique to Next. The differences between these tags are quite much, but they pretty much do the same thing; which is to display images to the browser.

Does Next JS lazy load images?

Lazy loading is enabled by default in the Next. js Image component. It is not only useful to the end-user, but it is also beneficial to the server because it does not have to execute image generation for images that may or may not be required.

How do I center the next image in Javascript?

object-fit: cover; object-position: center; this will make you image always 100% width and height of the <img/> box size and also center it.

Does next's image component stretch the width of the parent element?

When using the Next.js image component, the docs claim that: "When fill, the image will stretch both width and height to the dimensions of the parent element, usually paired with object-fit." However, this is not what it does at all.

What are image widths used for in next/image component?

These widths are used when the next/image component uses layout="responsive" or layout="fill" to ensure the correct image is served for user's device. If no configuration is provided, the default below is used. You can specify a list of image widths using the images.imageSizes property in your next.config.js file.

What is the difference between responsive image and fill in CSS?

When responsive, the image will scale the dimensions down for smaller viewports and scale up for larger viewports. When fill, the image will stretch both width and height to the dimensions of the parent element, usually paired with object-fit.

How to specify device width breakpoints in next/image component?

If you know the expected device widths of your users, you can specify a list of device width breakpoints using the deviceSizes property in next.config.js. These widths are used when the next/image component uses layout="responsive" or layout="fill" to ensure the correct image is served for user's device.


2 Answers

The wrapping div should have position: relative:

export default function Home() {
  return (
    <div>
      <div style={{width: '100px', height: '100px', position: 'relative'}}>
        <Image src={"/i.png"} layout='fill'/>
      </div>
    </div>
  )
}

This is a consequence of how position: absolute works. It's containing block will be the nearest ancestor element that has any position value but static (the initial value).

like image 120
Andrew Gillis Avatar answered Oct 27 '22 05:10

Andrew Gillis


You need to use position: relative for parent element.

like image 20
Dinesh Kumar Avatar answered Oct 27 '22 04:10

Dinesh Kumar