---
title: "Keep ids unique and stable · Agent Wranglers"
url: https://agentwranglers.se/playbook/html-css/html-structure-and-reuse/unique-stable-ids
updated: 2026-10-21
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Keep ids unique and stable

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers). Updated 2026-10-21.

For AI agents: [llms.txt](https://agentwranglers.se/llms.txt) is a short guide that sends you to the rules your problem needs, and [rules.json](https://agentwranglers.se/playbook/html-css/rules.json) holds every rule of this playbook as JSON.

- Level: Recommended
- Chapter: [HTML structure and reuse](https://agentwranglers.se/playbook/html-css/html-structure-and-reuse)
- Rule: Give each id on a page a value that appears once on that page and stays the same in every build. Make it from the content or the data key, never from a counter or a random number.
- How: Links such as `/menu#rye-loaf`, labels, `aria-controls` and tests all point at ids. A counter changes the id when a section is added above it, and the old links break. Build the id from a slug of the heading or the record's key, and add the module prefix when two modules could clash. Ids are never used for styling, as [Never style by id](https://agentwranglers.se/playbook/html-css/the-cascade/no-id-selectors) requires.
- Check: Every built page has 0 duplicate ids. Two builds of the same content produce the same list of ids on every page.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### JavaScript example

```js
const ids = [...html.matchAll(/\sid="([^"]+)"/g)].map(([, id]) => id);
const duplicates = ids.filter((id, i) => ids.indexOf(id) !== i);
if (duplicates.length) throw new Error(`${path}: duplicate ids ${[...new Set(duplicates)].join(', ')}`);
```

### Python example

```python
import re
from collections import Counter
ids = re.findall(r'\sid="([^"]+)"', html)
duplicates = [i for i, count in Counter(ids).items() if count > 1]
if duplicates:
    raise SystemExit(f"{path}: duplicate ids {', '.join(duplicates)}")
```

### HTML example

```html
<!-- Avoid: changes when a section is added above -->
<section id="section-3">
<!-- Do: made from the content -->
<section id="menu-rye-loaf">
```

## Sources

- [HTML Standard on the id attribute](https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute)

## Related rules

- [Never style by id](https://agentwranglers.se/playbook/html-css/the-cascade/no-id-selectors) in HTML and CSS

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/html-css/html-structure-and-reuse/unique-stable-ids">Agent Wranglers HTML and CSS playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Give each template one root element](https://agentwranglers.se/playbook/html-css/html-structure-and-reuse/one-root-per-template)
- Next: [Avoid wrapper elements that only hold a class](https://agentwranglers.se/playbook/html-css/html-structure-and-reuse/no-wrapper-divs)
- [HTML structure and reuse](https://agentwranglers.se/playbook/html-css/html-structure-and-reuse)
