Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we still need forward slashes when closing void elements in HTML5?

Tags:

html

xhtml

People also ask

Which slash is included in a closing tag in HTML?

Standard HTML Elements Notice how there is both an opening and closing part of the tag. The closing tag includes a forward slash at the beginning.

Is it mandatory to close empty elements in HTML5?

No, it is not required.

Which tag should have forward slash?

In most cases, the tag is repeated at the end of the content, with the tag name preceded by a forward slash (e.g. </p>), to indicate that the action is no longer needed. In a few cases, like <br /> (line break), tags are not followed by any content, in which case the tag is followed by a trailing slash.

Do void elements have end tags?

Characteristics: Void elements do not have end tags. Void elements cannot have content inside it. Void elements have attributes.


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.


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.


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


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


According to Start Tags they are optional.