160

In HTML5, do we still need the end slash like in XHTML?

<img src="some_image.png" />

validator.w3.org didn't complain if I dropped it, not even a warning. But some online documents seem to indicate the end slash is still required for tags such as img, link, meta, br, etc.

6
  • 7
    Still? Did any version of HTML require closing all tags?
    – Gabe
    Sep 9, 2011 at 19:11
  • 10
    @Gabe XHTML 1.0 Strict needs it. I mean, web pages will load fine usually, but it's considered invalid. Sep 9, 2011 at 19:15
  • 4
    Yes, but that X isn't just there because X is cool, there's quite a gap between HTML and XHTML.
    – user395760
    Sep 9, 2011 at 19:18
  • 1
    @Capt: You are correct, but that should only be an issue if your doctype indicates XHTML.
    – Gabe
    Sep 9, 2011 at 20:11
  • All the answers specify that you can either have the slash or no, but none of answers really explain why there are two alternative versions for defining void elements. I have also encountered term "self-closing" tag, which makes it even more confusing - are self-closing elements void elements? or they are similar to void, but with ending slash? anyways - this is quite inconsistent in HTML. Nov 11, 2022 at 15:14

5 Answers 5

120

In HTML 5, the closing slash is optional on void elements such img (I am adding this because the currently accepted answer only says: "end tags must not be specified for void elements", and does not address closing slashes in void elements).

Citing from http://www.w3.org/TR/html5/syntax.html#start-tags (number 6):

Then, if the element is one of the void elements, or if the element is a foreign element, then there may be a single "/" (U+002F) character. This character has no effect on void elements, but on foreign elements it marks the start tag as self-closing.

3
  • 1
    This is not an HTML5 thing. Actually, in earlier Versions of HTML <br/> or <img ... /> were errors.
    – j.j.
    Jun 30, 2020 at 15:05
  • 2
    @j.j. I quote the HTML5 docs, so I don't understand why do not think it is not HTML5. And indeed, in other versions, such slashed were errors. Jul 2, 2020 at 18:38
  • 1
    Sure, I meant to say "do we still need end slashes in HTML5" is the wrong question. May be I write a full answer with some hisorical links some day. IIRC it was a hard decision for Ian Hickson (the HTML editor) to allow trailing slashes in HTML5.
    – j.j.
    Jul 3, 2020 at 0:12
87

img tags are Void Elements so they do not need an end tag.

Void elements area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr

...

Void elements only have a start tag; end tags must not be specified for void elements.

W3C | WHATWG

That being said it's not strict parsing in HTML5 so it won't do any major harm.

6
  • Just curious, because my editor (Komodo) indents if I type '<img src="x">' and hit enter. It expects the trailing slash in HTML5 mode and I wanted to make sure this was correct behavior. Sep 9, 2011 at 19:14
  • 6
    This is HTML, not XHTML, so it's not required. Fortunately, parsers still understand the XHTML-style end-slashes just fine, so there's no harm in leaving it there. Means easier converting backwards to XHTML, if necessary for whatever reason. Sep 9, 2011 at 19:19
  • 5
    @FreeRadical: A trailing slash is optional for a void element, but an end tag would be invalid.
    – Ry-
    Apr 24, 2014 at 18:24
  • 37
    While the question is about end slashes (e.g. <br/), this answer only refers to end tags (e.g. <br></br>). Apr 25, 2014 at 3:27
  • 17
    @FreeRadical is correct. This answer is technically incorrect, because it confuses an ending slash with an end tag, and thus misunderstands the spec paragraphs that it quotes. This leads to @YannisDran's confusion. @minitech correctly states what is true. Aug 7, 2014 at 21:27
14

The end slash for void elements is optional.

Both are valid HTML5:

<img src="some_image.png" />

and

<img src="some_image.png">

Void Elements are:

  • area
  • base
  • br
  • col
  • embed
  • hr
  • img 👍
  • input
  • link
  • meta
  • param
  • source
  • track
  • wbr

Besides interesting:

Void elements can't have any contents (since there's no end tag, no content can be put between the start tag and the end tag).

More details at: https://html.spec.whatwg.org/multipage/syntax.html#elements-2


See also the SO-question here:
Self-closing tags (void elements) in HTML5

2
  • thanks, I made a regex to remove or add end slash; find: <(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)([^>]*?)\s*?/?> ; replace: <$1$2 /> if you're a parser, end slash is nicer because you'll know it's a self closing tag, and not an opening tag (you won't be looking: where's the closing tag?)
    – Mr. Doge
    Oct 7, 2021 at 15:38
  • Trailing slash on HTML 4.01 Transitional is the same as in HTML 5
    – MeSo2
    Mar 1, 2023 at 17:22
5

Nope. HTML has never required it, even before HTML5. If you plan to use XHTML with HTML features, then yes, it is necessary.

2
  • 1
    XHTML is not the same as HTML5, correct? If my DOCTYPE is just "html", I don't need the trailing slashes, correct? Sep 9, 2011 at 19:12
  • 1
    @CaptSaltyJack - HTML5 covers both ordinary HTML and XHTML. XHTML requires all elements to be closed, but for browsers, the doctype has no effect on the situation. For a fuller explanation, see stackoverflow.com/questions/2662508/…
    – Alohci
    Sep 9, 2011 at 20:56
2

According to Start Tags they are optional.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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