htmlimagexhtml

Why is the <img> tag not closed in HTML?


For curiosities sake, why is the <img> tag not closed in HTML?

<img src="smiley.gif" alt="Smiley face" height="42" width="42">

I also noticed that <img> tags are explicitly closed in XHTML...

<img src="smiley.gif" alt="Smiley face" height="42" width="42"/>

W3Schools: Image Tag


Solution

  • Historically, HTML has been based on SGML which allows tags to be omitted under certain conditions.

    Since the <img> element cannot have any child nodes, it is defined as EMPTY and the end tag is forbidden (as it would serve no purpose).

    XHTML is HTML expressed in XML, and XML does not support optional or forbidden tags (although it allows a self-closing tag to substitute for a start+end tag pair), so it has to be explicitly closed there.

    HTML 5 is backwards compatible with versions of HTML that were SGML based.