Skip to content
Learn Netverks

Lesson

Step 23/58 40% through track

id-attribute

The id attribute

Last reviewed May 28, 2026 Content v20260528
Track mode
iframe_html
Means
HTML preview sandbox
Reading
~2 min
Level
advanced

This lesson

This lesson teaches The id attribute—the ideas, syntax, and habits you need before moving on in HTML.

Without a solid grasp of The id attribute, you will repeat mistakes in HTML exercises and on real pages or scripts.

You will apply The id attribute in contexts like: Websites, hybrid apps, email templates, design systems, and CMS-driven content.

Read the lesson, edit HTML/CSS in the playground, press Run to preview, then answer the lesson MCQs. Also use the HTML reference desk when you need tag or attribute lookup.

When intermediate lessons feel comfortable and you are ready for production-style trade-offs.

An id must be unique within a live document. Duplicate IDs produce unpredictable querySelector results and break accessibility mappings.

Primary uses

  • Fragment identifiers in URLs (#pricing).
  • <label for="email"> referencing <input id="email">.
  • ARIA relationships (aria-labelledby, aria-controls).
  • Stable anchors for scripts when necessary.

Contrast with classes

  • IDs have extreme specificity in CSS—avoid styling via IDs unless unavoidable.
  • Classes scale for repeating patterns; IDs designate singletons.

Dynamic apps

Server-rendered duplicates happen when lists reuse IDs inside loops—switch to classes or generate stable unique IDs per row (keys in React reconciliation are not IDs in the DOM).

Why duplicate IDs haunt production

  • First match wins APIs—analytics or tests attach to wrong node silently.
  • Browser extension password managers autocomplete into wrong inputs when duplicated fields share IDs.

Fragment link + target id

<a href="#notes-section">Jump to notes</a>
…
<h2 id="notes-section">Notes</h2>

Rendered — click to scroll inside this lesson

Jump to demo target

Intermediate filler so the jump is noticeable…

Demo target (id anchor)

Screen readers and search engines use stable ids for deep links.

Important interview questions and answers

  1. Q: What is the practical difference between `id` and `class`?
    A: `id` must be unique and is used for fragments/labeling; `class` is reusable for styling and behavior grouping.
  2. Q: Why is `defer` commonly preferred for scripts?
    A: It preserves HTML parsing, executes after parse, and avoids blocking rendering unlike classic synchronous scripts.
  3. Q: How do `srcset` and `sizes` work together?
    A: `srcset` provides candidate files and `sizes` tells expected rendered width so the browser can pick an optimal resource.

Pitfall: IDs must be unique per page—use classes for repeated styling.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • What confused you about this lesson?
  • How would you explain this to a teammate in 30 seconds?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump