Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

HTML term or value: id

The `id` attribute assigns an identifier used by URL fragments, HTML and ARIA relationships, selectors, and DOM APIs.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<section id="chapter-1">…</section>

An id value must be non-empty, contain no ASCII whitespace, and be unique among all identifiers in the element’s tree. Identifier matching is exact; letter case and every character in the value matter.

Simple stable names such as #chapter-1 work cleanly in URLs and selectors. Other permitted characters may require CSS.escape() before building a selector; never interpolate an untrusted value directly into a CSS query.

Renaming an identifier can break a for label, an aria-labelledby reference, or an href="#chapter-1" link. Duplicates are nonconforming and make API results ambiguous, so component generation must maintain uniqueness after dynamic insertion too.

The example follows an href="#chapter-1" link through document.querySelector(), reads the matching id, and confirms a count of 1. It demonstrates a conforming fragment relationship with one target.

Examples

<!doctype html><meta charset="utf-8"><a id="jump" href="#chapter-1">Jump</a><section id="chapter-1">Chapter</section><output id="result"></output><script>const target=document.querySelector(jump.hash);result.textContent=`hash=${jump.hash}; target=${target.id}; matches=${document.querySelectorAll(jump.hash).length}`;</script>

Result

hash=#chapter-1; target=chapter-1; matches=1

The executed example verifies that the link hash selects exactly one element with the matching identifier.

Sources