Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

image

xhtml

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

like image 426
ѺȐeallү Avatar asked May 27 '14 13:05

ѺȐeallү


People also ask

Why is IMG not closing tag?

Create an image tag using the abbreviation img. This is considered a self closing tag, since it doesn't need to wrap text as many other tags do. The / right before the ending > is optional, but helps remind us that this tag doesn't need a closing tag. The src attribute is short for source.

Can you close an IMG tag?

The <img /> Tag This tag is different than other tags, in that it has no closing tag. It is called a self-closing tag, which means that there is just a slash at the end of the opening tag (ex. <img />). There are very few self-closing tags in HTML.


1 Answers

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.

like image 136
Quentin Avatar answered Sep 21 '22 16:09

Quentin