#Guys Why next image cannot take height
1 messages ยท Page 1 of 1 (latest)
maybe layout="responsive" is what you're looking for? that would make it scale to fit the width of the container (like height auto) and the height and width attributes are only used for calculating the aspect ratio of the image. https://nextjs.org/docs/api-reference/next/image#height
I have tried layout responsive, maybe I am using it wrong.
Send a screenshot of your usecase
Sure!
Next/Image approach.
<section className="flex flex-col">
{images.map((img) => (
<div className="mx-auto" key={img}>
<Image alt={img} src={img} width={'768px'} height={'1000'} />
</div>
))}
</section>
Output:
2.Plain html img tag with fixed width and height auto which is by default.
<section className="flex flex-col">
{images.map((img) => (
<div className="mx-auto" key={img}>
<img alt={img} src={img} width={'768px'} loading={'lazy'} />
</div>
))}
</section>
This is what I want, the images look not distorted as the previous image
what is the original width and height of the image?
looks like 1000px is not enough
Here is the list of image if you want to try it.
If I use the images like this:
<Image alt={img} src={img} width={'768px'} height={18000} />
They are distorted again
use the layout responsive prop
or try layout="fill" with objectFit="cover"
<Image alt={img} src={img} width={'768px'} height={18000} layout={'responsive'} />
I tried with layout responsive, they are not showin in screen
I have tried layout=fill and object fit cover.
<Image
alt={img}
src={img}
width={'768px'}
height={18000}
layout={'fill'}
objectFit={'cover'}
/>
There is only one image that shows on the screen, its not scrollable
so you want them to all the same width but different heights?
Yes, yes
style prop could also work, to overwrite the height with auto
also just a sidenote but you're using a string for the width and int for the height, it probably doesn't matter but I would use the same type just to be sure ๐
Yeah, I am using ts, it showed it can accept string and number as types
So I thought it could take auto height as well.
Can you pls help me with that?
<Image alt={img} src={img} width={'768px'} height={'14000'} style={'height':'auto'}/>
Oh!
Well! I did as the doc says!
<div className="mx-auto" key={img}>
<Image alt={img} src={img} layout={'fill'} objectFit={'cover'} style={imageStyle} />
</div>
const imageStyle = {
height: 'auto',
width: '768px',
};
Am I missing something?
I don't think you need to layout and objectFit anymore when height auto works ๐ค
So, I need to pass height and width and let the custom style ovrride it.
Okay, let me try that
I am able to see the image as expected now with fixed width. But it doesnt seem to be taking the custom height.
<div className="mx-auto" key={img}>
<Image alt={img} src={img} width={'768px'} height={'14000'} style={imageStyle} />
</div>
can you see if it's there in devtools? maybe it's just getting overwritten
Yes sure
Looks like the second image has different size but the first span tag, is overriting it
there's also layout="raw" but it's still experimental
but I guess that's pretty much just a img tag then
Yeah, then I wouldnt be able to use the benefits of the Next js image performance
๐ญ
maybe you can get the size of each image with js somehow and insert it into the Image as props?
or put a width and height attribute into your image object
At this point I am ready to take the risk of experimental features. But ig, 12.2 doesnt have height=auto
what if you do your image array like this?
const images = [
{
url: 'https://firebasest...',
width: 1280,
height: 15311
},
{
url: 'https://firebasest...',
width: 1280,
height: 15311
}
]
and then insert the height and width for each one like
<Image alt={img} src={img.url} width={img.width} height={img.height} style={imageStyle} />
I could do that.
Let me try this one
But then my question is shouldnt be height auto allowed in next js.
const url = require('url')
const http = require('http')
const sizeOf = require('image-size')
const imgUrl = 'http://my-amazing-website.com/image.jpeg'
const options = url.parse(imgUrl)
http.get(options, function (response) {
const chunks = []
response.on('data', function (chunk) {
chunks.push(chunk)
}).on('end', function() {
const buffer = Buffer.concat(chunks)
console.log(sizeOf(buffer))
})
})
I am using this package
I think the devs agree
Which is why they got 12.2!
Yeah! 

error - ./node_modules/image-size/dist/index.js:13:0
Module not found: Can't resolve 'fs'
Import trace for requested module:
Let me fix this, oh boy!
just to add height=auto in nextjs!
Error: The "next/future/image" component is experimental and may be subject to breaking changes. To enable this experiment, please include `experimental: { images: { allowFutureImage: true } }` in your next.config.js file.
/** @type {import('next').NextConfig} */
const nextConfig = {
strictMode: true,
experimental: {
images: {
allowFutureImage: true,
},
},
images: {
domains: ['firebasestorage.googleapis.com',],
},
};
module.exports = nextConfig;
OMG! @quartz bluff It worked it worked
<Image alt={img} src={img} />

I guess I gotta start using future image now aswell ๐ @cobalt tiger
Yeah I didnt pass any height and width
Do you know how to have the equivalent of layout=full in the future/image component.
I am not fure! height=100% width=100%?