Search code examples
htmlmarkup

Is not setting an attribute to equal a value valid HTML?


Is this line of code technically valid?

<img src="img.png" alt>

Within the CMS we are using, the devs said that they cannot make alt attributes appear on the page empty (e.g.: alt=""), they can either display them as the example above or they can add a space as a value (e.g.: alt=" ").

From what I understand, adding a space as a value could still cause some screen readers to not ignore decorative images creating accessibility issues. When I preview the above example in browsers, it does appear correct in dev tools and passes an HTML validator, but it just seems like it may be wrong or sloppy markup.

Is this valid?


Solution

  • Yes, it's valid. alt="" or just alt is recommended if you have purely decorative images, so screenreaders won't read irrelevant info like "red dot".

    See also the "alt"-Paragraph on https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img