I have a Next.js app, and I need an image that fills the full height of its container while automatically deciding its width based on its aspect ratio.
I have tried the following:
<Image
src="/deco.svg"
alt=""
layout="fill"
/>
This snippet compiles successfully, but on the frontend, I see the following error:
Error: Image with src "/deco.svg" must use "width" and "height" properties or "unsized" property.
This confuses me because according to the docs, these properties are not required when using layout="fill"
.
The height and width of an image can be set using height and width attribute. The height and width can be set in terms of pixels. The <img> height attribute is used to set the height of the image in pixels. The <img> width attribute is used to set the width of the image in pixels.
Tip: Always specify both the height and width attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded. However, without these attributes, the browser does not know the size of the image, and cannot reserve the appropriate space to it.
Setting it to responsive sets the css max-/min-width to 100%. Maybe this works for your usecase. I think your question overlaps with the one I recently made. There's a link to where the styling is added in the next/image component. In Version 10.0.1 they added the layout prop to the image component.
When you use next Image with layout='fill', then the image will fill the space of first parent element with position relative. Your div, which contains image, has position relative, but doesn't have fixed width and height. Its width is determined by parent 'blocks' grid element, because of rule grid-template-columns.
Columns are about width, not height. Its height is determined by height of child div with h2.title and p.about. That's why it is so small and wide. The best solution in your case would be to just set up fixed height and width on div with position relative, and adjust its values with media-queries on various resolutions.
Its width is determined by parent 'blocks' grid element, because of rule grid-template-columns. Columns are about width, not height. Its height is determined by height of child div with h2.title and p.about.
This is what worked for me.
with next/image
:
<div style={{width: '100%', height: '100%', position: 'relative'}}>
<Image
alt='Mountains'
src='/mountains.jpg'
layout='fill'
objectFit='contain'
/>
</div>
and with next/future/image
:
<Image
fill
alt='Mountains'
src='/mountains.jpg'
sizes='100vw'/>
next/future/image
<img src="/path/to/image.jpg" alt="" title="" />
In NextJS
<Image src="/path/to/image.jpg" alt="" title="" width="100%" height="100%" layout="responsive" objectFit="contain"/>
Here is a way: For example I want to have an image that covers the whole width & height of its container which is a div.
<div className={'image-container'}>
<Image src={path} layout="fill" className={'image'} />
</div>
And here is the style: (There is a container div that occupies half width & height of viewport & my image will cover it.)
// Nested Styling
.image-container {
width: 50vw;
height: 50vh;
position: relative;
.image {
width: 100%;
height: 100%;
position: relative !important;
object-fit: cover; // Optional
}
}
// Or Normal Styling
.image-container {
width: 50vw;
height: 50vh;
position: relative;
}
.image-container .image {
width: 100%;
height: 100%;
position: relative !important;
object-fit: cover; // Optional
}
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