Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the next/image component to 100% height

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".

like image 745
Jake Avatar asked Dec 06 '20 14:12

Jake


People also ask

How do you manage the height and width of an image?

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.

Why is it important to assign height and width attributes to an image?

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.

What does it mean to set the image component to responsive?

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.

How to use next image with layout='fill' in HTML?

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.

How to increase the height of column in a Div?

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.

How to determine the width and height of a block column?

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.


Video Answer


3 Answers

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

like image 147
francis Avatar answered Oct 13 '22 19:10

francis


<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"/>
like image 31
Muhammet Can TONBUL Avatar answered Oct 13 '22 18:10

Muhammet Can TONBUL


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
}
like image 14
Peyman Baseri Avatar answered Oct 13 '22 19:10

Peyman Baseri