HTML Guides for unclosed tag
Learn how to identify and fix common HTML validation errors flagged by the W3C Validator — so your pages are standards-compliant and render correctly across every browser. Also check our Accessibility Guides.
The <span> element is an inline container used to mark up a portion of text or a group of inline elements, typically for styling or scripting purposes. Like most HTML elements, it requires both an opening <span> tag and a closing </span> tag. When the closing tag is missing, the browser’s HTML parser must determine where the element ends on its own. Different browsers may handle this differently, potentially wrapping more content inside the <span> than intended.
This is problematic for several reasons:
- Unexpected styling: If the <span> has CSS applied to it (via a class, id, or inline style), the styles may bleed into sibling or subsequent elements that were never meant to be affected.
- Broken document structure: An unclosed <span> inside a <p> element can cause the parser to implicitly close and reopen elements in unexpected ways, distorting the DOM tree.
- Accessibility concerns: Screen readers and assistive technologies rely on a well-formed DOM. An unclosed element can cause content to be grouped or announced incorrectly.
- Maintenance difficulty: Unclosed elements make the markup harder to read, debug, and maintain over time.
A common scenario is forgetting the closing tag when the <span> wraps only part of a paragraph’s text. Another frequent cause is mismatched or misordered nesting, where the closing tags appear in the wrong sequence.
Examples
Unclosed <span> inside a paragraph
The closing </span> is missing entirely, so the browser has to guess where the span ends:
<!-- ❌ Bad: unclosed <span> -->
<p><span class="highlight">I'm forgetting something</p>
<p>Life goes on</p>
Add the missing </span> before the content that shouldn’t be wrapped:
<!-- ✅ Good: <span> is properly closed -->
<p><span class="highlight">I'm forgetting something</span></p>
<p>Life goes on</p>
Incorrectly nested closing tags
Sometimes the closing tags are present but in the wrong order. Tags must be closed in the reverse order they were opened:
<!-- ❌ Bad: closing tags are misordered -->
<p><em><span class="note">Important text</em></span></p>
<!-- ✅ Good: tags closed in correct (reverse) order -->
<p><em><span class="note">Important text</span></em></p>
Multiple <span> elements with a missing close
When using several <span> elements in sequence, it’s easy to lose track:
<!-- ❌ Bad: second <span> is never closed -->
<p>
<span class="first-name">Jane</span>
<span class="last-name">Doe
</p>
<!-- ✅ Good: all <span> elements are closed -->
<p>
<span class="first-name">Jane</span>
<span class="last-name">Doe</span>
</p>
Tips for avoiding unclosed elements
- Indent consistently. Proper indentation makes it much easier to spot a missing closing tag.
- Write both tags at once. When you type <span>, immediately type </span> and then fill in the content between them.
- Use an editor with auto-closing and bracket matching. Most modern code editors will highlight unmatched tags.
- Validate regularly. Run your markup through the W3C HTML Validator often, especially after larger edits.
When the validator reports “Unclosed element ‘X’”, it means an opening tag like <div> or <p> was never terminated with its required closing tag </div> or </p>, or it was closed out of order. HTML parsing then tries to auto-close elements, which can reshape the DOM in ways you didn’t intend. This often happens with nested structures, copy‑paste edits, or when mixing void elements (which never have closing tags) with normal elements.
Leaving elements unclosed can break layout and styles, confuse assistive technologies that rely on a well-formed structure, and cause scripts or CSS selectors to behave unpredictably. It also harms portability between browsers and tools, and makes maintenance harder because the rendered tree differs from what the source suggests.
To fix it, ensure that:
- Every non-void element has a matching end tag: open <section>, close with </section>.
- Nesting is properly balanced: the last opened element is the first one you close (LIFO).
- You do not add closing tags to void elements like br, img, input, meta, link, or hr. These must not have end tags in HTML, and you don’t need a trailing slash.
- Self-closing syntax (<br />) is permitted but treated as <br> in HTML; do not rely on it to close non-void elements.
- Use your editor’s bracket/tag matching or formatter to spot mismatches, and validate early.
Examples
Example that triggers the issue (unclosed element)
<!-- Problem: <div> is never closed, and <p> is closed after a new <div> starts -->
<div class="card">
<h2>Title</h2>
<p>Some text
<div class="footer">
<p>Footer text</p>
</div>
Fixed: properly closed and correctly nested
<div class="card">
<h2>Title</h2>
<p>Some text</p>
<div class="footer">
<p>Footer text</p>
</div>
</div>
Example that triggers the issue (out-of-order closing)
<!-- Problem: <em> is opened inside <strong> but closed after </strong> -->
<p><strong><em>Important</strong></em> notice</p>
Fixed: close in reverse order of opening
<p><strong><em>Important</em></strong> notice</p>
Example that triggers the issue (incorrectly “closing” a void element)
<!-- Problem: void elements must not have end tags -->
<p>Line one</p>
<br></br>
<p>Line two</p>
Fixed: use void elements without end tags
<p>Line one</p>
<br>
<p>Line two</p>
Full document example with a common pitfall (lists and paragraphs)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Unclosed Element Fix</title>
</head>
<body>
<!-- Problem avoided: do not nest <li> across paragraph boundaries -->
<ul>
<li>
<p>Item one title</p>
<p>Item one details</p>
</li>
<li>
<p>Item two</p>
</li>
</ul>
</body>
</html>
Tips to prevent “Unclosed element” errors
- Keep indentation consistent; it visually reflects nesting.
- Close tags immediately after opening, then fill content.
- Use linters/formatters (e.g., HTMLHint, Prettier) and run the W3C validator regularly.
- Be cautious when mixing templating logic; ensure conditionals don’t skip required end tags.
Ready to validate your sites?
Start your free trial today.