Nextjs Image not using the height value - next.js

I am new to NextJS and was using its Image tag to have an image in my application. But the image size only changes if I change its width value, changing height's value does not impact it at all.
Here is the image:
<Image src="/Logo.png" alt="Logo" height={10} width={100} />
Here the image is taking the width's value and coming out to be big. If I replace the values in height and width, then also it takes the width's value and becomes small. I have even tried to put height property after width but nothing changes.
What am I doing wrong here?

You can wrap the Image component inside a div and style it like this:
<div style={{ position: "relative", width: `${100}px`, height: `${10}px` }}>
<Image
src="/Logo.png"
alt="Logo"
fill
style={{ objectFit: "contain" }}
/>
</div>
Note that:
The wrapper div has a relative position since fill Images are absolute positioned inside the DOM
fill is a prop in next 13 (older versions use layout="fill")
objectFit is passed inside the style object (with optional objectPosition) and those properties will be added to the rendered span element style properties

Wrap tag in a with the height and width you want your image to be
Add layout="fill" in the tag
<div style={{width: '100px', height: '100px'}}>
<Image
src="/Logo.png"
alt="Logo"
layout='fill'
objectFit='contain'
/>
</div>

Related

Fill the image and img error box to desire height and width

<>
<div
className={
"border-2 border-amber-400 flex-shrink-0 w-[200px] h-[300px]"
}
>
<img
src={img}
alt="Background Image"
className="object-cover h-250 w-200"
/>
Pictureeeee
</div>
</>
I want the container always be constants to h-250 and w-200 for the layout.
Even if I set it to h-250 and w-200, it does not seem to work that way. I want the image to fill or stretch to cover all the spots if necessary. Also, that ? box to be h-250 and w-200 as well.
You have missed a very small detail while using arbitary values, As there is no class named h-250 and w-200 you are not able to see the desired height and width
Change
className="object-cover h-250 w-200"
to
className="object-cover h-[250px] w-[200px]"

Image Component in nextjs with CSS styles for mobiles

I am using Image Nextjs Component to put my image in the center of the page i want the picture to be like this and remain its reel size but it has been pixelized. plusm when i reduce the the screen width the picture doesn't appear correctly for the small screen views a part of the picture disappears
<div
style={{
position: 'relative',
width: '1000px',
height: '500px',
alignItems: 'center',
}}
>
<Image src='/services.png' alt='' layout='fill' objectFitt='fill' />
</div>
I think the problem is from the div style. I tried so hard to fix the width and height of the div but i still get the deformed picture in the page. Also i changed the objectFitt to 'cover' but same problem :(
I appreciate any help and any explanation of how to put an image without being pixelized.
Thank you so much.
The picture size is: width=1454px and height=455px
here is the picture
You should change the div's width from 1000px to 100% to make it responsive and choose height according to different screen sizes. try out this-
<div
style={{
position: 'relative',
width: '100%',
height: '300px',
alignItems: 'center',
}}
>
<Image src={main_image} alt='' layout='fill' objectFitt='fill' />
</div>

Using Nextjs Image component with different image sizes

I am trying to use the Next.js Image component with images that all have unique sizes. Their example states the image will stretch to the parent div width and height. I've tried wrapping them but don't know how to accommodate dynamic image sizes. I want to have a set width and the height to adjust to each respective proportion. TIA.
Here is my code (the images are coming from an array) -- since I don't have a height set nothing renders on the page. If i use Tailwind's h-auto it still does not display:
<div className="w-screen h-screen">
{allImages.allImages.map((image, i) => {
return (
<div className="relative w-100 md:w-5/6 lg:w-7/12" key={image}>
<Image priority src={`/${image}`} layout="fill" objectFit="cover" />
</div>
)})}
</div>
I found a solution in this article which seems to work.
<div className="image-container">
<Image src={path} layout="fill" className="image" />
</div>
SCSS
.image-container {
width: 100%;
> div {
position: unset !important;
}
.image {
object-fit: contain;
width: 100% !important;
position: relative !important;
height: unset !important;
}
}
I wrapped this in a component named <ResponsiveImage />. You can try other rules on the wrapper, such as position: relative.
You can use these props on the Image tag,
<div className="div-class">
<Image src={imageLink} layout="fill" objectFit="cover" />
</div>
and wrap the Image tag in a div, just as above. Now you can give width and height to the div with media queries and the image will use that space.

TailwindCSS + Next.js Image take full height with fill layout

I'm trying to use the Next.js' <Image> component with the layout=fill option. Therefore I've created a relative div that contains the Image tag. But I'd like to determine the height by the Image height.
Found this example but height is statically given there. So I've tried this based on the example.
<div className="relative w-full h-full">
<Image
src={post.coverImage.url}
layout="fill"
alt={post.title}
/>
</div>
I've also tried h-full or max-h-full classes instead of h- tags but couldn't succeed.
How can I make its height automatically determined?
I had the same issue and figured it out after inspecting the element. The problem is how Image component takes place in DOM.
This the Image component on DOM, look there are 2 divs and inside "img" is placed. So you have to target the outer div that wraps img.
// I add img-wrapper class
<div className="relative w-full h-full img-wrapper">
<Image
src={post.coverImage.url}
layout="responsive"
alt={post.title}
/>
</div>
and in css:
// targeting the first div
.img-wrapper > div {
// this will take full-height, u can adjust it
height: 100%;
}

material design stretching image to take full browser width with auto height

the header image i have obviously wont fit to all browser width so i used following css to take auto adjust it to the browser width. however, if i dont specify the height attribute the div does not appear. specifying height as auto or % does not work either. only way it works is giving a fix px. but then that will make the whole design non responsive on small devices as the height will stay constant. please advise
<div fxLayout="column">
<app-nav-bar></app-nav-bar>
<div fxLayout="row" fxLayoutAlign="start stretch" style="background: url('../../assets/images/header1.png') no-repeat;background-size: 100%;height:300px">
</div>
</div>
Try using "background-size:cover" instead of 100%.
For those using React: the material-ui-image will automatically make the image full screen like so:
<Image style={{marginLeft: '-24px', marginRight: '-24px', marginBottom: '24px', marginTop: '-24px'}}
src={'https://images.unsplash.com/photo-1579037611768-298fcaad76e2?auto=format&fit=crop&w=1000&h=625&q=60'}
onClick={() => console.log('onClick')}
aspectRatio={(16 / 9)}
disableSpinner
/>

Resources