-1

How to close image tag in HTML?

<img src="#"></img>

or

<img src="#" />

or

 <img src="#">
5
  • What version of HTML are you after?
    – Chris
    May 19, 2014 at 13:38
  • 2nd and 3rd is correct.it if wont close also it working
    – Ramki
    May 19, 2014 at 13:39
  • What is version? I use new HTML. May 19, 2014 at 13:40
  • There is HTML 5 as the newest but HTML 4 is still quite prevalent.
    – Chris
    May 19, 2014 at 13:42
  • HTML6? I don't believe such a thing exists...
    – Chris
    May 19, 2014 at 14:28

5 Answers 5

3

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)

2
  • Might be nice to include spec reference. The HTML 5 one is w3.org/TR/html5/embedded-content-0.html#the-img-element. Also the xhtml is not for backwards compatibility, it is so that it is valid XML markup.
    – Chris
    May 19, 2014 at 13:41
  • @Chris: thanks user: always do some alt attribute, it is for when the image can not be loaded, or screen-readers, then the alt will be visible
    – Mark
    May 19, 2014 at 13:44
2

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:

  1. A "<" character.
  2. The element’s tag name.
  3. Optionally, one or more attributes, each of which must be preceded by one or more space characters.
  4. Optionally, one or more space characters.
  5. Optionally, a "/" character, which may be present only if the element is a void element.
  6. 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.

1

HTML 4.01 and earlier:

<img> <!-- end tag forbidden -->

XHTML

<img/> <!-- self closing tag syntax -->
<img></img>

HTML Compatible XHTML

<img /> <!-- self closing tag syntax with a space -->

HTML 5

<img> <!-- end tag forbidden -->
<img /> <!-- end tag forbidden but a / allowed for people addicted to XML -->
0

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="#">
0
0

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">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.