As web-developers we code (or used to code) pretty frequently in HTML. Within HTML, you’ve seen some elements, that have a closing tag
<div></div>
But some, don’t
<br>
or
<br />
Why is that? Let’s jump right into it
Search in the best source - non-closing tags
Elements without closing tag are called void elements
We’ll seek knowledge in HTML Specifications
Void elements only have a start tag; end tags must not be specified for void elements.
[…]
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).
This answers your question: void elements don’t have any content. Technically, I could end it here but
Should we add /
in void elements
Which one should we choose
<br>
Or
<br />
There have been numerous discussions online about it - some people add slash, some don’t
What’s in the docs again?
[…] there may be a single U+002F SOLIDUS character (/), which on foreign elements marks the start tag as self-closing. On void elements, it does not mark the start tag as self-closing but instead is unnecessary and has no effect of any kind. For such void elements, it should be used only with caution — especially since, if directly preceded by an unquoted attribute value, it becomes part of the attribute value rather than being discarded by the parser.
(for context: foreign elements are ones from SVG or MathML namespaces)
According to specs: trialing slashes are fully optional in void tags and have no effect, but might cause issues later on.
What issues? As said, if you write HTML properties without quotes, then parser might interpret that slash as a part of an attribute rather than end of a tag.
Why is it even there in the first place? For backward compatibility
Currently used HTML version is HTML5 - but your documents can also be rendered in other modes
There used to be a standard called XHTML - a mashup of HTML and XML
In there, these slashes were obligatory. If slashes went missing, XHTML parser could not distinguish whether such tag closes or is it missing a closing one.
Final verdict
This might be disappointing but… use whatever you want to!
If you write HTML properties without quotes, then omiting that slash will be safer choice
Yet, for some people, not writing it can save time Others prefer the trailing slash, as it makes the code more readable. It clearly marks the end of the tag.
Personally, I’m a fan of a second option
Conclusion
Thanks for reading, I hope you’ve learned something new. I wrote this article pretty quickly, but had to find a lot of data
As a little fun fact, I can tell you that <p>
tag used to be void, as it indicaited space between the paragraphs - not paragraphs themselves.
Subscribe to my newsletter in the navbar, check out other articles too such as
And see you next time