Lesson 31 of 51 · Module 5, lesson 4 of 7 · 3 min
The id attribute
Give one element a unique name for jump links, form labels and JavaScript.
What you'll learn
- What the
idattribute is, and how it differs fromclass- The three jobs of an
id- The rules for writing one
Why this matters
You already used id to make jump links. It has two more jobs that you will meet soon: connecting a form label to its field, and letting JavaScript find one exact element. All three depend on one rule: an id is unique.
id and class
id | class | |
|---|---|---|
| How many elements can use one name | Exactly one on the page | Many |
| How many names per element | One | Many, separated by spaces |
| Typical use | Jump links, form labels, JavaScript | Styling groups with CSS |
The three jobs of an id
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Newsletter</title>
</head>
<body>
<h1>Home kitchen</h1>
<p><a href="#subscribe">Subscribe to the newsletter</a></p>
<h2 id="subscribe">Subscribe</h2>
<p>
<label for="email">Your email</label>
<input id="email" type="email">
</p>
</body>
</html>
What you'll see: a link, a heading, and a text box labelled "Your email". Clicking the words "Your email" puts the cursor in the box.
(<input> makes a box that the visitor can type in, and <label> is its visible name. You will learn them properly in the forms module.)
- Jump links —
href="#subscribe"jumps toid="subscribe". (See Links to a place on the same page.) - Form labels —
for="email"connects the label to the field withid="email". Screen readers then read "Your email" when the field is selected. You will use this in every form. - JavaScript — scripts can find one element by its
id, to change or read it. You will do that in the JavaScript course.
Rules for writing an id
- Unique on the page. Two elements with the same
idbreak all three jobs. - No spaces. Use hyphens:
id="contact-form". - Start with a letter, lower case by habit.
- Case-sensitive:
#Emaildoes not findid="email".
Try it yourself
- Select the example and choose Try it.
- Click the words "Your email" in the preview. The cursor jumps into the box — that is the label connected by
id. - Change
for="email"tofor="mail". Click the label again: nothing happens. Change it back.
Common mistakes
- The same
idtwice on a page. Links jump to the wrong place and labels connect to the wrong field. Fix: everyidis unique — use a class if many elements share a name. - A
#in theiditself, such asid="#email". Fix:id="email"; the#goes only in links. - A mismatch between
forandid. The label no longer works. Fix: copy the name exactly.
Check yourself
- Can two elements on one page share an
id? - Which attribute on
<label>points to a field'sid? - When would you use a
classinstead of anid?
Answers
- No. An
idmust be unique. for.- When several elements share the name — for example, all recipe cards.
Next: now the big picture. HTML has elements that mark the main areas of a page — header, navigation, main content, footer — so that everyone can find their way.

