Topic 47 of 57 9. HTML, CSS, and JavaScript Interactive Tutorial

HTML ID Attribute: Syntax, Uses, and Examples

11 mins read Last updated: 12 Nov 2026 15,800 views

Topic Overview & Definition

Key Question
What is the HTML id attribute and what strict uniqueness rules govern it?
Direct Answer & Overview
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").

Comprehensive Step-by-Step Concepts

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

Runnable HTML5 Code Example & Practical Implementation

<!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>

Key Takeaways, Best Practices & Standards

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

Common Technical & Interview Questions

Q

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.
Q

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.
Q

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.