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?
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.
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.
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.
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.
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.
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.
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.
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).
You need to use position: relative
for parent element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With