Skip to main content

HTML Guide

The “name” attribute on the “img” element is obsolete. Use the “id” attribute instead.

The warning regarding the use of the name attribute on the img element arises because the name attribute is considered obsolete in modern HTML. Historically, name was used to identify form controls and some other elements, but now it’s replaced by more standardized attributes like id.

To resolve this issue, replace the name attribute with the id attribute. The id attribute provides a unique identifier for the element within the document, which can be utilized for styling, scripting, or linking using fragment identifiers.

Here is an example of how to make this change:

HTML with obsolete name attribute:

<img src="example.jpg" name="myImage" alt="Descriptive text">

Updated HTML using the id attribute:

<img src="example.jpg" id="myImage" alt="Descriptive text">

In this modification:

  • The name="myImage" is replaced with id="myImage".
  • The remaining attributes like src and alt are retained for specifying the image source and providing alternative text, respectively.

The id attribute should be unique within the document, which ensures that JavaScript or CSS can target the element efficiently.

Learn more:

Related W3C validator issues