HTML Void Elements and Optional Tags
Void is an element category in syntax
Void HTML elements such as br and input cannot have children in conforming markup and must not have an end tag. A trailing slash on a void start tag is permitted but has no closing effect in HTML.
<output id="result"></output><script> const parsed = new DOMParser().parseFromString("<p>A<br />B<input type=\"text\"></p>", "text/html"); const result = parsed.querySelector("p").innerHTML; document.querySelector("#result").textContent = String(result);</script>Expected output: A<br>B<input type="text">
A slash does not close a normal HTML element
This intentionally invalid HTML uses a slash on a div start tag. The HTML tree builder leaves the div open. In XML the equivalent empty-element syntax really closes it. Use an explicit end tag for a non-void div.
<output id="result"></output><script> const parsed = new DOMParser().parseFromString("<div/><p>Inside</p>", "text/html"); const result = parsed.querySelector("p").parentElement.localName; document.querySelector("#result").textContent = String(result);</script>Expected output: div
Optional tags have conditions
The end tag of an li may be omitted when immediately followed by another li or when there is no more content in its parent. Optional tags are not a universal shortcut. The DOM still contains the elements, and serialization can emit explicit tags.
<output id="result"></output><script> const parsed = new DOMParser().parseFromString("<ul><li>One<li>Two</ul>", "text/html"); const result = parsed.querySelector("ul").innerHTML; document.querySelector("#result").textContent = String(result);</script>Expected output: <li>One</li><li>Two</li>
Common mistakes and practice
Prefer explicit optional end tags in tutorials for readability. Quote attribute values: in an unquoted value a slash directly following the value can become part of that value. Never add an end tag to a void element.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Void Elements and Optional Tags — Knowledge Check
Apply the internals concepts taught in this lesson.
Which element is void?
Does <div/> close a div in HTML?
When may an li end tag be omitted?
Does omitting a permitted end tag remove the DOM element?
What can a slash adjacent to an unquoted attribute value become?