vercel / nextjs-portfolio-starter

Easily create a portfolio with Next.js and Markdown.
https://demo.vercel.blog
664 stars 263 forks source link

How to auto-height an Image using only a width? #32

Open opendeluxe opened 1 year ago

opendeluxe commented 1 year ago

I want to put images into a blog post with a fixed width but an automatically calculated height. How to do that?

Using code like

<Image
  src="/photos/test.jpg"
  alt="Photo"
  width={200}
  priority
  className="next-image"
/>

will return the error message:

Error: Image with src "/photos/test.jpg" must use "width" and "height" properties or "layout='fill'" property.

The property "layout=fill" doesn't work either, as it strechtes the image over the full screen size.

any ideas how to do that?

bogdanrakocevic commented 10 months ago

Try with CSS in main.css file

img.next-image-your-class {
  margin: 0;
  width: auto;
  height: 90px;
}

In this example, I wanted fixed height, but width to remain scaled to that fixed height.