Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need a "/" at the end of an <img> or <br> tag, etc.? [duplicate]

Tags:

html

Do you need to have a / at the end of an img tag? I saw an example on W3schools.com without a /:

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

I know it isn't necessary to self-close the tag, at least in my browser, but should I do so?

like image 733
MarJamRob Avatar asked Mar 01 '13 02:03

MarJamRob


People also ask

Does IMG require a closing tag?

The <img> tag is used to insert an image into a document. This element must not contain any content, and does not need a closing tag.

Do all tags need a closing tag?

No, not all the tags are ending tags. Tags may be either paired or unpaired (or single). Paired tags need to be closed, i.e. they contains both ending and non-ending tags, whereas unpaired tags need not be closed, i.e. they only contain opening tags.

Which tags do not need a closing tag?

The void elements or singleton tags in HTML don't require a closing tag to be valid. These elements are usually ones that either stand alone on the page ​or where the end of their contents is obvious from the context of the page itself.


1 Answers

The / is only required for XHTML & XML.

If you're using a HTML5 doctype, then there's no need to terminate self-closing tags in this way.

This applies to <img src="img.png" />, <br />, <hr /> etc.

I.e. Just use <img src="img.png">, <br> and <hr>.

If you need an empty element (like a div), don't use <div />, instead use <div></div>. This is important since in HTML5, the slash is ignored and <div /> is interpreted as <div> without a closing tag.

like image 186
Danny Beckett Avatar answered Sep 30 '22 17:09

Danny Beckett