-1

So I have been coding for a little bit now and have noticed something about the <img> tag. I have always closed this tag using <img src="image.jpg">, however, I have noticed that some programmers have used <img src="image.jpg"/>.

Can someone please clarify for me and maybe others who have the same question which is the correct syntax?

Thank you~!

Ash4741
  • 37
  • 4
  • I think you could find a proper answer here. https://stackoverflow.com/questions/3558119/are-non-void-self-closing-tags-valid-in-html5 – Leonardo Gasparini Aug 29 '18 at 13:19
  • Thank you everyone for redirecting the post to some very useful references. Because this post seems to be a duplicate, should I delete it? – Ash4741 Aug 29 '18 at 13:53

1 Answers1

1

The HTML5 standard defines a void element, like img (https://www.w3.org/TR/html5/syntax.html#void-elements and img https://www.w3.org/TR/html5/semantics-embedded-content.html#elementdef-img

<img src='my.png'>

Void elements only have a start tag; end tags must not be specified for void elements

Whereas XHTML needs it to be closed

<img src='my.png' /> <!-- Self Closing -->
<img src='my.png'></img> <!-- "normal" closing -->
Frederik Spang
  • 3,379
  • 1
  • 25
  • 43