2

Is it necessary to close an img tag?

<img src="" alt="" /> 

versus

<img src="" alt""> 

I'm purely wondering as someone learning HTML who has never learned the proper way, but has seen both methods used. I tend to use the first i.e. close all my tags.

azochz
  • 791
  • 3
  • 10
  • 18

3 Answers3

2

In XHTML, all elements must be closed. In HTML, some (like <img>) are optional.

(X)HTML5 has both syntaxes: HTML and XHTML. Then, it's your choice if you use HTML5 or XHTML5.

If you doubt, you can always use W3C Validator to check if your code is valid.

Oriol
  • 225,583
  • 46
  • 371
  • 457
  • since no distinction in html is it completely arbitrary when people choose to use one or the other? – azochz Mar 20 '14 at 22:06
  • @azochz Well, if some web page is required to be valid XML, it must be coded in XHTML, with closing slashes. But if it doesn't matter, yes, choose the one you like the most. – Oriol Mar 20 '14 at 22:13
0

You don't need the "/" at the end . This is W3 standard for tags in HTML5. If using XHTML, you should close it.

0

I don't see why you would ever intentionally exclude the closing slash. It makes the tag more readable and your HTML will pass a lot more validators. However, if you're 4.01 strict, it doesn't look to be required: http://www.w3.org/TR/REC-html40/struct/objects#h-13.2

Andreas
  • 4,410
  • 2
  • 19
  • 28