next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Image warning issue - If you use CSS to change the size of your image, also include the styles 'width: "auto" or 'height: "auto"' to maintain the aspect ratio

Open nicitaacom opened this issue 1 year ago • 31 comments

Link to the code that reproduces this issue

demo - https://ominous-space-broccoli-9qgwx9pqq6p2pxvq-3000.app.github.dev/

mvp - https://ominous-space-broccoli-9qgwx9pqq6p2pxvq.github.dev/ mvp codesandbox - https://codesandbox.io/p/devbox/mvp-warn-img-aspect-ratio-md683h

github - https://github.com/nicitaacom/acc2-image-warning-to-maintain-the-aspect-ratio

To Reproduce

  1. Paste this code in page.tsx
        <Image
      className="cursor-pointer max-h-[32px] w-auto h-auto"
      src={"/23_store-dark.png"}
      alt="logo"
      width={300}
      height={32}
      priority
    />

See this

image

I already use width auto and height auto

Current vs. Expected behavior

I don't want to see useless warnings in my console

Now I see useless warning that impossible to fix (I tried add some tailwind classes - issue persists)

Verify canary release

  • [X] I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
    Binaries:
      Node: 16.17.0
      npm: 8.15.0
      Yarn: 1.22.19
      pnpm: 7.1.0
    Relevant Packages:
      next: 13.5.1
      eslint-config-next: 13.5.1
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.2.2
    Next.js Config:
      output: N/A

Which area(s) are affected? (Select all that apply)

Image optimization (next/image, next/legacy/image)

Additional context

I tried it with next 13.5.3 - the same result

For search engines

Image with src "/success-checkmark.gif" has either width or height modified, but not the other. If you use CSS to change the size of your image, also include the styles 'width: "auto"' or 'height: "auto"' to maintain the aspect ratio.

nicitaacom avatar Sep 26 '23 15:09 nicitaacom

Just add this, depending on what you need

        style={{ width: "300px", height: "32px" }}

or style={{ width: "auto", height: "auto" }}

Paget96 avatar Sep 26 '23 17:09 Paget96

Having auto width and height doesn’t make sense. It’s one or the other. What’re you trying to achieve by setting both properties to auto?

terrymun avatar Sep 26 '23 23:09 terrymun

Just add this, depending on what you need

        style={{ width: "300px", height: "32px" }}

or style={{ width: "auto", height: "auto" }}

I'd like to use tailwind to keep consistent Now I do w-auto and h-auto and it doesn't work

nicitaacom avatar Sep 27 '23 08:09 nicitaacom

Having auto width and height doesn’t make sense. It’s one or the other. What’re you trying to achieve by setting both properties to auto?

I wnat max-h-[32] and w-full

I mean image should be max-h-[32px]

If I set max-h-[32px] and in atributes width={300} my image has 300px width - I don't want it I want image to be with original aspect ratio (I think I bad described what I want - I decided to add picture to describe what I mean)

image

nicitaacom avatar Sep 27 '23 08:09 nicitaacom

Is any updates about this issue?

nicitaacom avatar Oct 06 '23 09:10 nicitaacom

This issue still open - somebody know how to fix this?

nicitaacom avatar Nov 06 '23 10:11 nicitaacom

I'm still see this issue Somebody know how to fix it?

warn-once.js:16 Image with src "/success-checkmark.gif" has either width or height modified, but not the other. If you use CSS to change the size of your image, also include the styles 'width: "auto"' or 'height: "auto"' to maintain the aspect ratio.

nicitaacom avatar Jan 17 '24 11:01 nicitaacom

I'm running into this issue as well.

console.js:213 Image with src "/logos/test.png" has either width or height modified, but not the other. If you use CSS to change the size of your image, also include the styles 'width: "auto"' or 'height: "auto"' to maintain the aspect ratio.

With this:

<Image src={"/logos/test.png"}
       width={42.625}
       height={40}
       alt="test"
       as={NextImage}
       radius="none"
/>

And still have the issue with this:

<Image src={"/logos/test.png"}
       width={42.625}
       height={40}
       alt="test"
       as={NextImage}
       radius="none"
       className="h-[40px] w-auto"
/>

RyanPaiva56 avatar Jan 20 '24 17:01 RyanPaiva56

@CitizenBeta It is strange you have the as prop in your template. Is Image from next/image?

terrymun avatar Jan 20 '24 22:01 terrymun

Update

Hey I updated MVP Please check it and help if you want Because I'm still see this issue

nicitaacom avatar Jan 24 '24 07:01 nicitaacom

@CitizenBeta It is strange you have the as prop in your template. Is Image from next/image?

Removing that solved it for me. I honestly don't know how that got there, maybe it was in a code sample. Thank you!

RyanPaiva56 avatar Jan 24 '24 17:01 RyanPaiva56

Any updates on this? I'm still see this issue

I write this comment to keep this issue open

Edit by maintainer bot: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!

nicitaacom avatar Feb 16 '24 07:02 nicitaacom

Yeah this is still an issue. I set both properties, follow the directions, and still randomly get this warning.

mcfry avatar Mar 26 '24 15:03 mcfry

Hello. In the face of the same problem, I made several estimates, and in my project, it was a symptom due to the default setting of tailwindcss. If you are using tailwindcss, you can resolve warning with the following settings.

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
    img {
        width: auto;
        height: auto;
    }
}

If you are not using tailwindcss, it would be good to check if there is a css file that modifies width, height of img tag with the same principle.

Jungho-Cheon avatar Apr 19 '24 01:04 Jungho-Cheon

Before suggested fix: image

After suggested fix: (console has been cleared and page has been refreshed) image

MVP - https://codesandbox.io/p/devbox/mvp-warn-img-aspect-ratio-md683h

@Jungho-Cheon - please provide mvp where fix work for you So others can use your fix

nicitaacom avatar Apr 19 '24 08:04 nicitaacom

This issue is because the tailwind base css:

@tailwind base;
@tailwind components;
@tailwind utilities;

which include following:

img, video {
    max-width: 100%;
    height: auto;
}
image

I don't know how to property fix it, but this warning seems not cause any actual issue.

tjx666 avatar May 29 '24 19:05 tjx666

Hello. In the face of the same problem, I made several estimates, and in my project, it was a symptom due to the default setting of tailwindcss. If you are using tailwindcss, you can resolve warning with the following settings.

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
    img {
        width: auto;
        height: auto;
    }
}

If you are not using tailwindcss, it would be good to check if there is a css file that modifies width, height of img tag with the same principle.

Work for me, thanks!

nalfin avatar Jun 30 '24 12:06 nalfin

Work for me, thanks!

Provide mvp please

nicitaacom avatar Jun 30 '24 19:06 nicitaacom

Can confirm this warning is unfixable. It is a bug.

poznianski avatar Jul 04 '24 16:07 poznianski

import Image from "next/image";
import Link from "next/link";

const Footer = () => {
  return (
    <footer className="border-t">
      <div className="flex-center wrapper flex-between flex flex-col gap-4 p-5 text-center sm:flex-row">
        <Link href="/">
          <div>
            <Image
              src="/assets/images/logo.svg"
              alt="logo"
              style={{ width: "128px", height: "38px" }}
              priority
              width={128}
              height={38}
            />
          </div>
        </Link>

        <p>2024 Evently. All Rights reserved.</p>
      </div>
    </footer>
  );
};

export default Footer;

alok-38 avatar Jul 14 '24 05:07 alok-38

import Image from "next/image";
import Link from "next/link";

const Footer = () => {
  return (
    <footer className="border-t">
      <div className="flex-center wrapper flex-between flex flex-col gap-4 p-5 text-center sm:flex-row">
        <Link href="/">
          <div>
            <Image
              src="/assets/images/logo.svg"
              alt="logo"
              style={{ width: "128px", height: "38px" }}
              priority
              width={128}
              height={38}
            />
          </div>
        </Link>

        <p>2024 Evently. All Rights reserved.</p>
      </div>
    </footer>
  );
};

export default Footer;

image

I need max width that fit original aspect ratio e.g 200x100 h-[50px] w-full Then should be image 100x50

nicitaacom avatar Jul 14 '24 06:07 nicitaacom

I keep seeing comments suggesting fixes and possible solution, can we all agree that:

  1. if styled properly (see screenshot below for inspiration) the images display fine with one side having a fixed length and the other one adapting according to the original aspect ratio
  2. the warning is going to appear in the browser console, regardless of the image displaying properly

Screenshot 2024-07-30 122141 Screenshot 2024-07-30 122153

lorenzo-dallamuta avatar Jul 30 '24 10:07 lorenzo-dallamuta

I keep seeing comments suggesting fixes and possible solution, can we all agree that:

Same with next.js Image warning here

nicitaacom avatar Jul 30 '24 11:07 nicitaacom

I was able to fix the error with the globals.css option: @layer base { img { width: auto; height: auto; } }

But after that i got a new error:

The resource <URL> was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate as value and it is preloaded intentionally.

Never had this issue before with NextJS must be some sore of bug.

byron-rod avatar Aug 11 '24 01:08 byron-rod