Skip to main content
HTML Validation

Stray end tag “X”.

About This HTML Issue

The HTML parser processes tags sequentially, maintaining a stack of open elements. When it encounters an end tag like </div>, it looks for a matching open <div> in the current scope. If no matching open element exists, the end tag is “stray” — it has nowhere to go and violates the HTML specification. Browsers will silently ignore stray end tags in most cases, but their presence signals structural problems in your markup that can lead to unpredictable rendering and layout issues.

This error matters for several reasons. First, stray end tags often indicate a deeper structural problem — if you have an extra </div>, your layout nesting may be wrong even if the browser renders it acceptably today. Second, assistive technologies like screen readers rely on correct document structure to convey meaning, and malformed HTML can confuse them. Third, clean, valid markup is easier to maintain and debug, especially in collaborative projects.

There are several common causes of this error:

Duplicate closing tags — The most frequent cause. You accidentally close the same element twice, so the second closing tag is stray.

Copy-paste remnants — When moving or deleting blocks of HTML, a closing tag gets left behind without its corresponding opening tag.

Mismatched nesting — Elements are improperly nested, causing the parser to close tags in an unexpected order and leaving orphaned end tags.

Closing void elements — Void elements like <br>, <img>, <hr>, and <input> never have closing tags. Writing </br> or </img> will produce this error.

To fix the issue, search your document for the stray end tag mentioned in the error, then either remove it if it’s a duplicate or add/move the corresponding opening tag if one is missing.

Examples

Duplicate closing tag

This is the most common scenario — an element is closed twice:

<!-- ❌ Bad: duplicate </ul> -->

<ul>
  <li>Item one</li>
  <li>Item two</li>
</ul>
</ul>

Remove the extra closing tag:

<!-- ✅ Good -->

<ul>
  <li>Item one</li>
  <li>Item two</li>
</ul>

Copy-paste leftover

After editing, a closing tag remains without its opening counterpart:

<!-- ❌ Bad: stray </p> with no matching <p> -->

<div>
  <h2>Title</h2>
  </p>
  <span>Some text</span>
</div>

Remove the orphaned </p>:

<!-- ✅ Good -->

<div>
  <h2>Title</h2>
  <span>Some text</span>
</div>

Mismatched nesting

Incorrectly nested elements can produce stray end tags:

<!-- ❌ Bad: </section> closes before </div>, leaving </div> stray -->

<section>
  <div>
    <p>Content here</p>
  </section>
  </div>

Fix the nesting so inner elements close before outer ones:

<!-- ✅ Good -->

<section>
  <div>
    <p>Content here</p>
  </div>
</section>

Closing a void element

Void elements have no closing tag. Adding one produces a stray end tag error:

<!-- ❌ Bad: <br> and <img> are void elements -->

<p>Line one<br></br>Line two</p>
<img src="photo.jpg" alt="A photo"></img>

Remove the closing tags for void elements:

<!-- ✅ Good -->

<p>Line one<br>Line two</p>
<img src="photo.jpg" alt="A photo">

Multiple stray tags from a removed wrapper

When you remove a wrapper element’s opening tag but forget the closing tag:

<!-- ❌ Bad: <main> was removed but </main> remains -->

<body>
  <h1>Welcome</h1>
  <p>Hello world</p>
  </main>
</body>

Either remove the stray </main> or restore the opening tag:

<!-- ✅ Good: stray tag removed -->

<body>
  <h1>Welcome</h1>
  <p>Hello world</p>
</body>
<!-- ✅ Also good: wrapper restored -->

<body>
  <main>
    <h1>Welcome</h1>
    <p>Hello world</p>
  </main>
</body>

When dealing with deeply nested HTML, using consistent indentation and an editor with bracket/tag matching makes it much easier to spot these issues before they reach validation. Many code editors also highlight unmatched tags in real time.

Find issues like this automatically

Rocket Validator scans thousands of pages in seconds, detecting HTML issues across your entire site.

Help us improve our guides

Was this guide helpful?

Ready to validate your sites?
Start your trial today.