-1

In some places I see it's "just" <img src="URL"> whereas in others I see it's <img src="URL" />

Do I need to add the / and > or not?

Space or not is irrelevant?

Maybe the /> was in the past (not for HTML5)? I cannot find any information on it

Thanks!

Starting Out
  • 123
  • 1
  • 2
  • 6

2 Answers2

0

The self-closing element tag syntax (/>) was introduced to allow HTML to exist as an XML dialect as XHTML. It is not necessary in SGML (HTML and XML's superset) as the DTD declares what elements are self-closing or not.

In SGML, the trailing / character is interpreted as a flag attribute with the literal name / and is otherwise ignored, making it a polyglot syntax.

In the current HTML5 syntax revision there is an explanation:

A Self-closing tag is a special form of start tag with a slash immediately before the closing right angle bracket. These indicate that the element is to be closed immediately, and has no content. Where this syntax is permitted and used, the end tag must be omitted. In HTML, the use of this syntax is restricted to void elements and foreign elements. If it is used for other elements, it is treated as a start tag. In XHTML, it is possible for any element to use this syntax. But note that it is only conforming for elements with content models that permit them to be empty.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • What does this have to do with SGML? –  Sep 30 '15 at 17:19
  • 1
    @torazaburo I mentioned SGML to give some historical context and to explain why `>` is valid in HTML5 but why ` />` is also acceptable (and in the case of XHTML, required). – Dai Sep 30 '15 at 17:22
-1

img tag is a self-closing tag so it's a good practice to write it like <img /> (more XHTML style) although it's absolutely fine and valid to leave it without that slash.

Cube.
  • 483
  • 6
  • 15