HTML & Web Development 9. HTML, CSS, and JavaScript 11 mins read 15,805 views

HTML ID Attribute: Syntax, Uses, and Examples

The HTML 'id' attribute specifies a globally unique identifier for an element across the entire document. An 'id' value can only be used once per page. It is us...
Published: Nov 1, 2026Last Updated: Nov 21, 2026Curriculum: Class 12 / AP Physics / JEE-NEETReviewed by: OperateTools Editorial Board
Quick Summary & Direct Answer (AEO / GEO Extraction)

What is HTML ID Attribute: Syntax, Uses, and Examples? The HTML 'id' attribute specifies a globally unique identifier for an element across the entire document. An 'id' value can only be used once per page. It is us...

Audience & Scope: Students, educators, and candidates preparing for academic examinations and technical assessments in Html & Web Development.
What is the HTML id attribute and what strict uniqueness rules govern it?
The HTML 'id' attribute specifies a globally unique identifier for an element across the entire document. An 'id' value can only be used once per page. It is used to target specific elements with CSS (#id), manipulate unique nodes with JavaScript (document.getElementById), create bookmark anchor jumps (#section), and associate labels with form inputs (for="inputId").

Explanation & In-Depth Details

1) The Strict Document Uniqueness Rule: An 'id' must be unique across the entire HTML document. Having multiple elements with the same 'id' results in invalid HTML, breaks CSS specificity, causes form accessibility failures, and causes JavaScript's getElementById() to only return the first occurrence.

2) Four Primary Uses for the 'id' Attribute:
- JavaScript DOM Manipulation: document.getElementById('navbar') is the fastest node lookup in browsers.
- Form Accessibility: Linking <label for="userEmail"> to <input id="userEmail">.
- Bookmark Fragment Navigation: Linking <a href="#features"> smoothly jumps to <section id="features">.
- ARIA Relationships: Linking aria-describedby="errMessage" to <p id="errMessage">.

3) Syntax Rules: ID values are case-sensitive, must not contain whitespace or tabs, and should begin with an alphabetical letter.

4) CSS ID Selector Specificity: In CSS, IDs are selected using '#' (e.g. #hero { background: #000; }). However, because IDs carry high CSS specificity, industry standards recommend using classes (.hero) for styling and reserving IDs for scripting and accessibility hooks.

5) Global Window Property Quirk: By legacy web convention, elements with an 'id' automatically create a matching property on the JavaScript global 'window' object (e.g. window.myHeader). Relying on this is an anti-pattern; always use document.getElementById().

Code Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HTML ID Attribute Guide</title>
</head>
<body>
    <!-- Top Navigation with Anchor Fragment Links -->
    <nav>
        <a href="#aboutSection">About Us</a> |
        <a href="#contactSection">Contact</a>
    </nav>

    <!-- Accessible Form Input Binding -->
    <form style="margin: 20px 0;">
        <label for="newsletterEmail">Subscribe to Newsletter:</label>
        <input type="email" id="newsletterEmail" name="newsletter_email" placeholder="you@domain.com">
    </form>

    <!-- Unique Bookmark Target: aboutSection -->
    <section id="aboutSection" style="margin-top: 40px; padding: 20px; background: #f8fafc;">
        <h2>About Our Organization</h2>
        <p>This section is uniquely identified by id="aboutSection".</p>
    </section>

    <!-- Unique Bookmark Target: contactSection -->
    <section id="contactSection" style="margin-top: 40px; padding: 20px; background: #eff6ff;">
        <h2>Contact Information</h2>
        <p>This section is uniquely identified by id="contactSection".</p>
    </section>

    <script>
        // High-speed unique DOM lookup
        const contactSec = document.getElementById('contactSection');
        console.log('Located unique section:', contactSec.id);
    </script>
</body>
</html>

Interview Question & Tips

Q1: What happens if two elements on the same webpage share the identical 'id' attribute?
A1: The document becomes invalid according to W3C validators. When executing 'document.getElementById()', the browser will only return the first element found in DOM order, causing unpredictable JavaScript bugs and broken accessibility relationships.

Q2: Why do modern CSS architectures discourage using IDs for styling?
A2: IDs have very high CSS specificity (0-1-0-0). Overriding an ID style requires another ID selector or '!important', which quickly leads to an unmaintainable CSS codebase. Use classes for styling instead.

Q3: Can an HTML element have both a 'class' and an 'id' attribute?
A3: Yes! An element frequently has both: an 'id' for unique JavaScript targeting and accessibility form binding, and multiple 'classes' for visual CSS styling.

Best Practices

Ensure every 'id' is 100% unique per page. Use IDs for form label bindings, accessibility ARIA references, and bookmark anchors.
Academic Standards & Verified Citations

This study reference for HTML ID Attribute: Syntax, Uses, and Examples is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation:

HTML & Web Development