How to close image tag in HTML?
<img src="#"></img>
or
<img src="#" />
or
<img src="#">
How to close image tag in HTML?
<img src="#"></img>
or
<img src="#" />
or
<img src="#">
in xhtml:
<img src="#" alt="some explaining" />
the closing / is for backwards compatibility
in html5
<img src="#" alt="some explaining">
no need anymore to close the <img>
tag, (the browser knows that it is self-closing)
more info here: http://www.w3.org/TR/html5/embedded-content-0.html#the-img-element (thanks to Chris)
As per this HTML5 standard: http://www.w3.org/TR/html-markup/syntax.html#void-element
Start tags consist of the following parts, in exactly the following order:
- A "<" character.
- The element’s tag name.
- Optionally, one or more attributes, each of which must be preceded by one or more space characters.
- Optionally, one or more space characters.
- Optionally, a "/" character, which may be present only if the element is a void element.
- A ">" character.
The img
is a void element and hence the part #5 would apply with a caveat that "a '/' character, which may be present..."
<img id="..." src="..." />
And so, you may omit the part #5 i.e. the closing "/", and hence this is also valid:
<img id="..." src="..." >
Further down the spec says:
Void elements only have a start tag; end tags must not be specified for void elements.
So, no end tag is required.
HTML 4.01 and earlier:
<img> <!-- end tag forbidden -->
<img/> <!-- self closing tag syntax -->
<img></img>
<img /> <!-- self closing tag syntax with a space -->
<img> <!-- end tag forbidden -->
<img /> <!-- end tag forbidden but a / allowed for people addicted to XML -->
Like this:
<img src="#" />
An image tag can't have any content, but you do need to close it in HTML4. In HTML5 this is not required any more.
So then this is also allowed:
<img src="#">
It is an empty element so you can close the start tag itself like this
<img src="YOUR_FILE"/>
or you can even not closing it like this
<img src="YOUR_FILE">