Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stray end tag img

While validating markup through W3C validator service got this below error

Stray end tag img

Code is like below

<a title="text" href="url">
<img class="text" src="imgSrc" alt="Text"></img>
</a>

What does it means ? How we can avoid it ?

like image 635
Midhun Murali Avatar asked Jun 09 '14 08:06

Midhun Murali


People also ask

What is a stray end tag?

As such, “Stray end tag...” means just that an end tag is not allowed in the context where it appears. As the validator's explanation says: “The Validator found an end tag for the above element, but that element is not currently open.

What is a stray tag in HTML?

A stray start tag <html> has been found in the document. As this tag defines the start of the whole HTML document, it should appear only once.

What is a stray start tag footer?

The validator says “Stray start tag footer” because the start tag appears in a context where no elements can be started – after the </body> tag, where only the optional </html> tag may appear. Follow this answer to receive notifications.

What does start tag body seen but an element of the same type was already open mean?

An opening <body> tag has been found in an incorrect place. Check that it appears only once in the document, right after the closing </head> tag, and that the whole document content is contained within <body> and </body> tags.


2 Answers

As such, “Stray end tag...” means just that an end tag is not allowed in the context where it appears. As the validator’s explanation says: “The Validator found an end tag for the above element, but that element is not currently open. This is often caused by a leftover end tag from an element that was removed during editing, or by an implicitly closed element (if you have an error related to an element being used where it is not allowed, this is almost certainly the case). In the latter case this error will disappear as soon as you fix the original problem.”

From the symptoms (the error message string), we can infer that you are validating against HTML5 in HTML serialization. This means that no end tag is allowed for an img element, since the start tag is treated as also closing the element (“implicitly closed element”).

Thus, the solution is to either remove the </img> tag or to validate against HTML5 in XHTML serialization. The latter is not practical for web pages, but if you are using HTML for something else, you should validate by URL referring to a resource that is served with an XML content type.

like image 103
Jukka K. Korpela Avatar answered Sep 22 '22 02:09

Jukka K. Korpela


If your document is XHTML compliant then you have to close img tag with <img src="image.jpg"/>, not with <img>...</img>.

If your document is HTML5 compliant, then you do not need the /> part, only use <img src="image.jpg">

And if you wonder what means that document should be XHTML or HTML5 compliant - this is the very first line of your HTML page, the so called document type definition:

<!DOCTYPE HTML> for HTML5 and

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> for XHTML 1.0 Transitional

NOTE: The <!DOCTYPE> declaration is mandatory (if you want your pages to validate with an HTML validator) and should always be the first thing in an HTML document.

NOTE: Although a document type definition is technically not required for a functioning web page, it is good practice to always include it in your code. As you learn to build web pages, get into the habit of always including the document type definition in your code.

More reading:

  • HTML 4.01
  • XHTML 1.1
  • HTML5
like image 26
Bud Damyanov Avatar answered Sep 21 '22 02:09

Bud Damyanov