Very confused about why the <Image/> component works the way it does #25564
Replies: 4 comments 3 replies
-
|
I would add to the point list: #19914 (comment) |
Beta Was this translation helpful? Give feedback.
-
|
I agree 100% with all the points you've made, and I have also scoured the internet in search of answers, but my conclusion so far is that as of today it's impossible to preserve aspect ratio by just supplying either width OR height. I hope someone can solve this problem in the future because it seems like a pretty common use case to me. Did you end up just using <img> ? Or the band-aid solution of a relative parent div? The latter is not great because as you mentioned you have to make sure the parent has the right size for the image. For instance for something like this: We would still have to find the right size for the parent div. Giving either width OR height to the div won't solve the problem of getting a responsive image that preserves the aspect ratio. |
Beta Was this translation helpful? Give feedback.
-
|
I agree with you and wanted to share this discussion which might help you find a solution. |
Beta Was this translation helpful? Give feedback.
-
|
For any Tailwind user still struggling with this, I built a library called // ❌ Before:
<Image
src="/avatar.webp"
sizes="(min-width: 1024px) 120px, 100px"
className="size-25 lg:size-30"
/>
// ✅ After:
<SmartImage src="/avatar.webp" className="size-25 lg:size-30" />
// → sizes="(min-width: 1024px) 120px, 100px"It wraps
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I've been working with next.js for a few months now and I love it. However, I have a few gripes about the
Imagecomponent system that don't really make sense to me:layout="fill". This really doesn't make any sense to me. In what world would someone want to explicitly set the width AND height of an image? Images should only be sized using width OR height to maintain aspect ratio. Why am I not allowed to do this?layout="fill"andobjectFit="cover", with a relative wrapper div around every single one to size them without warping. This is only a band-aid solution though because it is most likely going to introduce some clipping on the edges of the image if the parent is not exactly the right size.next.config.js. I'm just struggling to make it work at all using the default settings. If I understand correctly, next.js uses a maximum image size of100vw. So why is it that on my 1920px wide screen, I see images 3000px or larger being loaded in the Chrome inspector Network tab?Sorry if this sounds like a rant but I've honestly been scouring the internet almost every day since I started with next.js and I've still never been able to find any straightforward, concise, and detailed documentation on how next.js images work. The official documentation for the
next/imagepackage seems oversimplified and shallow to me. I've honestly learned more about the image system from other people's posts online than from the official documentation.Beta Was this translation helpful? Give feedback.
All reactions