# \`autoCloseTags\` doesn't close a nested tag when an identical outer closing tag already exists

**URL:** https://discuss.codemirror.net/t/autoclosetags-doesnt-close-a-nested-tag-when-an-identical-outer-closing-tag-already-exists/9831
**Category:** v6
**Created:** [July 22, 2026, 6:38am UTC](https://discuss.codemirror.net/t/autoclosetags-doesnt-close-a-nested-tag-when-an-identical-outer-closing-tag-already-exists/9831 "2026-07-22T06:38:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![bajrangCoder](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/bajrangcoder/32/5497_2.png) [@bajrangCoder](https://discuss.codemirror.net/u/bajrangCoder)
#### Post date: [July 22, 2026, 6:38am UTC](https://discuss.codemirror.net/t/autoclosetags-doesnt-close-a-nested-tag-when-an-identical-outer-closing-tag-already-exists/9831/1 "2026-07-22T06:38:51Z")

</div>

Hi, I’m seeing unexpected behavior from `autoCloseTags` in `@codemirror/lang-html`.

Starting with:

```html
<div>
  
</div>

```

When I type another `<div>` inside the existing element, the result is:

```html
<div>
  <div>
</div>

```

I expected:

```html
<div>
  <div></div>
</div>

```

The syntax tree appears to associate the existing `</div>` with the newly typed inner `<div>`, leaving the outer `<div>` unclosed. Tag-matching highlighting also shows the inner opening tag and the existing closing tag as a pair.

This is especially noticeable with nested elements of the same name. Tags with a different name close as expected because the existing closing tag cannot be paired with the new opening tag.

Is this intended behavior, or would it make sense for `autoCloseTags` to distinguish between a closing tag that existed before the input and one belonging to the newly typed element?

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)
#### Post date: [July 22, 2026, 7:47am UTC](https://discuss.codemirror.net/t/autoclosetags-doesnt-close-a-nested-tag-when-an-identical-outer-closing-tag-already-exists/9831/2 "2026-07-22T07:47:10Z")

</div>

This patch should improve this.

> **[Make autoCloseTags smarter about nested tags · 31e485be58](https://code.haverbeke.berlin/codemirror/lang-html/commit/31e485be584c0798c7bc3c08db5c616af886c297)**
>
> FIX: Don't prevent auto-closing of tags when there's a matching close tag that
> belongs to a parent node present.
> 
> See https://discuss.codemirror.net/t/autoclosetags-doesnt-close-a-nested-tag-when-an-identical-outer-closing-tag-already-exists/9831

---

<div class="post-metadata">

### Author: ![bajrangCoder](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/bajrangcoder/32/5497_2.png) [@bajrangCoder](https://discuss.codemirror.net/u/bajrangCoder)
#### Post date: [August 3, 2026, 12:59am UTC](https://discuss.codemirror.net/t/autoclosetags-doesnt-close-a-nested-tag-when-an-identical-outer-closing-tag-already-exists/9831/3 "2026-08-03T00:59:34Z")

</div>

Publish the update for lang-html package
