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 aclass,id, or inlinestyle), 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><spanclass="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><spanclass="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><spanclass="note">Important text</em></span></p>
<!-- ✅ Good: tags closed in correct (reverse) order -->
<p><em><spanclass="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>
<spanclass="first-name">Jane</span>
<spanclass="last-name">Doe
</p>
<!-- ✅ Good: all <span> elements are closed -->
<p>
<spanclass="first-name">Jane</span>
<spanclass="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.
Every non-void HTML element needs a matching end tag. When one is missing, the browser's error recovery kicks in and decides where the element ends. The resulting DOM tree can differ from what the source code suggests, breaking layout, CSS selectors, and JavaScript that depends on a specific structure. Screen readers and other assistive technologies also rely on correct nesting to convey document structure to users.
This error commonly appears when tags are closed out of order. HTML requires last-in-first-out nesting: the most recently opened element must be closed first. Another frequent cause is copy-paste editing that removes a closing tag while leaving the opener behind. Note that void elements (<br>, <img>, <input>, <meta>, <hr>, <link>) never have closing tags. Writing <br></br> is invalid in HTML and can trigger a related error.
Examples
Missing closing tag
<!-- Bad: the outer <div> is never closed -->
<divclass="card">
<h2>Title</h2>
<p>Some text</p>
<divclass="footer">
<p>Footer text</p>
</div>
Fixed
<divclass="card">
<h2>Title</h2>
<p>Some text</p>
<divclass="footer">
<p>Footer text</p>
</div>
</div>
Out-of-order closing tags
<!-- Bad: <em> is opened inside <strong> but closed after </strong> -->
<p><strong><em>Important</strong></em> notice</p>
Fixed
<p><strong><em>Important</em></strong> notice</p>
Consistent indentation makes nesting mismatches easier to spot by eye. Editor features like bracket matching, and tools like Prettier or HTMLHint, can catch these problems before you run the validator. If you use a templating language, make sure conditional blocks do not skip required end tags.
Validate at scale.
Ship accessible websites, faster.
Automated HTML & accessibility validation for large sites. Check thousands of pages against WCAG guidelines and W3C standards in minutes, not days.
Pro Trial
Full Pro access. Cancel anytime.
Start Pro Trial →Join teams across 40+ countries