Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

ESLint warns that img elements must have an alt prop in React

Errorimg elements must have an alt prop, either with meaningful text, or an empty string for decorative images

Short answer

The jsx-a11y/alt-text rule found an img without a usable alt. Give meaningful text when the image carries information, and an empty string alt="" when it is decorative. A role of presentation does not replace alt, and props spread onto an img must pass alt explicitly.

  • Applies to React 19, eslint-plugin-jsx-a11y 6, ESLint
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run ESLint on the file to see the exact line, then find every img in the project that has no alt.

    npx eslint src
  2. 2Match what you see, then jump to its fix

    1. 1The warning points at a plain <img src=... /> lineThe img has no alt at allFix
    2. 2The img has role="presentation" or role="none" and still warnsrole="presentation" was used instead of altFix
    3. 3The line is <img {...props} /> and the alt is passed by the callerProps are spread onto the img and alt is hidden inside themFix
    4. 4The warning is on <Image /> although there is no img in that fileA wrapper component such as Image is checked through the rule's img optionFix

What does the error look like?

  • img elements must have an alt prop, either with meaningful text, or an empty string for decorative images
  • jsx-a11y/alt-text

Cause 1 of 4

The img has no alt at all

1Check

Look at the tag. The rule fails an alt that is missing, written without a value, or set to undefined.

grep -rn "<img" src/

2Fix

Describe the image if it adds information. If it is decoration, or an icon next to a text label, use an empty string.

<img src={logo} alt="Acme logo" />
<img src={divider} alt="" />

3Verify

Run ESLint again on the file.

npx eslint src/App.jsx

Confirmed by github.com

Did this fix it?

Cause 2 of 4

role="presentation" was used instead of alt

1Check

The rule documentation lists these roles on an image as a failing case; plain HTML already has the answer.

grep -rnE "role=.(presentation|none)" src/

2Fix

Remove the role and use alt="".

// before
<img src={bg} role="presentation" />
// after
<img src={bg} alt="" />

3Verify

ESLint reports no alt-text error for that file.

npx eslint src/Hero.jsx

Confirmed by github.com

Did this fix it?

Cause 3 of 4

Props are spread onto the img and alt is hidden inside them

1Check

The rule treats a spread without an explicit alt as failing, even if the caller does pass one.

grep -rn "<img {" src/

2Fix

Destructure alt and pass it on explicitly.

function Photo({ alt, ...props }) {
  return <img alt={alt} {...props} />;
}

3Verify

ESLint passes, and callers still set alt on Photo.

npx eslint src/Photo.jsx

Confirmed by github.com

Did this fix it?

Cause 4 of 4

A wrapper component such as Image is checked through the rule's img option

1Check

The rule can map the img element to custom component names, so a wrapper listed in the config is checked like an img.

grep -rn "alt-text" eslint.config.* .eslintrc* package.json 2>/dev/null

2Fix

Give the wrapper an alt prop the same way. If a component should not be checked, remove it from the option list in your config.

<Image src={hero} alt="Product dashboard" />

3Verify

Run ESLint on the file.

npx eslint src

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Treat alt as part of the image component's required props in TypeScript.
  • Keep jsx-a11y in the recommended config so new images are checked at save time.
  • Write alt text for what the image says, not 'image of'.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.