← Back to HTML & CSS, page 8

html-css-0154 · Easy fresher

What are void elements, and why does a self-closing div break the page?

Void elements such as br, img and input have no end tag and no content. A trailing slash is ignored in HTML, so a self-closing div is an opening tag that swallows the next elements. Check the real DOM in the developer tools.

Full answer

A void element has no content and no end tag. The list is short: area, base, br, col, embed, hr, img, input, link, meta, source, track and wbr. The parser closes them at once, so <br> is a complete element and </br> is an error.

In HTML, a slash at the end of a tag, as in <br/>, is allowed on void elements but means nothing. The parser ignores it. This is different from XML and from JSX, where every element must be closed. The habit of writing <div /> comes from those languages, and it is a bug in HTML. For a non-void element, the slash is ignored, so <div/> is an opening tag. Everything after it becomes the content of that div until a closing tag appears or the parent ends.

Results:

  • A "self-closed" div or span swallows the following elements into its box. Styles and layout then look wrong, and the cause is hard to see in the source.
  • Void elements cannot have children. Setting innerHTML on an img does nothing, and a script cannot append into an input.
  • <script src="a.js" /> is not closed either. The browser waits for </script>, and the following markup can be treated as script text.

Use the developer tools to check the real DOM. It shows what the parser built, which can differ from the source.

The first section has three void elements, and each has no child nodes. In the second section, the div is never closed, so the paragraph ends up inside it. A script can confirm this by reading p.parentElement.className, which gives x.

When a linter or a code formatter warns about this, fix the source and do not rely on the parser's recovery.

html
<section id="a"><img src="/a.png" alt="" id="img"><br id="br"><input id="inp"></section>
<section id="b"><div class="x"/><p id="p">I am inside the div</p></section>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Void elements have no content and no end tag.
  • The trailing slash is ignored in HTML.
  • A self-closed div or script stays open.
  • Void elements cannot have children.
  • Inspect the parsed DOM, not only the source.

Common mistakes

  • Writing <div /> or <span /> and expecting an empty element.
  • Writing </br> or </img> end tags.
  • Self-closing a script tag with a src attribute.

Follow-ups

  • Why does JSX need the slash and HTML does not?
  • How can you see the DOM the parser really built?

Tips

  • Give the list of common void elements from memory: br, hr, img, input, meta, link.
  • Say that the trailing slash is ignored in HTML and explain the div consequence.