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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With