HTML Id
Classes gave you stickers many elements can share. An id is different: it names one element on the page — a unique door label for bookmarks, a single CSS hook, or a script target. If two elements claim the same id, browsers and assistive tools get confused. Keep class for reusable looks; reach for id when uniqueness matters.
Unique names and why they matter
Fragment links, “skip to content” patterns, and document.getElementById all assume one match. Duplicate ids may appear to “work” in a quick click test and still break accessibility tooling or later scripts. Treat each id like a primary key for that page: invent it once, attach it once, and link to it with a hash in the URL — not inside the attribute value.
Direct answer
Write id="faq" on exactly one element. Link to it from higher on the page with href="#faq". In CSS, select that element with #faq. Do not reuse the same id on two elements. Do not write id="#faq" — the # belongs in href and in CSS selectors, not in the HTML attribute value. Prefer class when many paragraphs should share one tip style.
📊 id versus class
| Feature | id | class |
|---|---|---|
| Uniqueness | One per page (ideally) | Many elements may share |
| HTML attribute | id="faq" (no #) | class="tip" (no .) |
| CSS selector | #faq | .tip |
| Common uses | Bookmarks, unique hooks, script targets | Reusable styles and groups |
Fragment link to a unique heading
A classic beginner pattern: a jump link near the top and a heading lower on the page that owns the matching id. Clicking the link scrolls to that heading.
Unique id and fragment link
html<p><a href="#faq">Jump to FAQ</a></p>
<h2 id="faq">FAQ</h2>
<p>Answers go here.</p>
Reading the fragment sample
href="#faq"points at the fragment namedfaqid="faq"lives on exactly one heading- The bare name is in the attribute; the
#is only in the link - Change the label text freely; keep the id token matching the href
- If the jump fails, check spelling and confirm the id was not duplicated
- This is the same idea as bookmarks/fragments you may have seen in the Links module — now tied explicitly to
id
Unique CSS hook with #id
Sometimes one panel needs a one-off highlight that should not become a shared class. An id plus a #name rule targets that single box. Use this sparingly — most visual systems still prefer classes so themes stay reusable.
Unique id with CSS
html<div id="promo">
<h2>Limited offer</h2>
<p>One panel, one unique hook.</p>
</div>
<style>
#promo { border: 2px solid #0a7; padding: 12px; }
</style>
Reading the unique-style sample
- Only this
divhasid="promo" #promoin CSS means that exact element- Padding and border apply to the unique panel
- If you copy the panel twice, give the copy a new id or switch both to a shared
class - Do not put
#insideid="…"— keep#in the stylesheet selector - Mixing
class="card"withid="promo"on the same element is fine when you need both shared and unique hooks
Choosing id or class
Ask: “Will more than one element need this label?” If yes, use class. If you need a single bookmark target or a script handle for one node, use id. Avoid decorating every reusable tip paragraph with its own id — that pattern fights the uniqueness rule and clutters the document.
Practice in your editor
- Give a heading a unique
id(for examplesteps). - Add a link higher on the page with
href="#steps"and click to jump. - Confirm that id appears only once in the file.
- Optionally style that heading with
#stepsin a<style>block. - Temporarily duplicate the id on another element, notice the confusion risk, then remove the duplicate.
- Restyle two tip paragraphs with a shared
classinstead of two ids. - Write one sentence contrasting
#inhref/CSS with the bare name in the HTML attribute. - Optional: combine
class="card"andid="promo"on one box from the Div lesson.
Easy mistakes
- Two elements with the same
id - Putting
#inside the HTMLidattribute value - Using id for every reusable style (prefer
class) - Forgetting the
#inhrefso the jump becomes a normal path request - Choosing ids that start with a digit or contain spaces
- Copy-pasting demo markup until three “unique” panels share one id
Summary
idnames one element per page for bookmarks and unique hooks- Fragment links use
href="#name"; CSS uses#name - The HTML attribute holds the bare name — no leading
# - Prefer
classfor shared looks; reserveidfor uniqueness - Never duplicate an id on the same page
- Next sessions build on these hooks inside larger page structure
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top