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 id attribute is, and how it differs from class
  • 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

idclass
How many elements can use one nameExactly one on the pageMany
How many names per elementOneMany, separated by spaces
Typical useJump links, form labels, JavaScriptStyling groups with CSS

The three jobs of an id

html
<!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.)

The page "Home kitchen" with a link "Subscribe to the newsletter", a heading "Subscribe", and a text field labelled "Your email".

  1. Jump links — href="#subscribe" jumps to id="subscribe". (See Links to a place on the same page.)
  2. Form labels — for="email" connects the label to the field with id="email". Screen readers then read "Your email" when the field is selected. You will use this in every form.
  3. 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 id break all three jobs.
  • No spaces. Use hyphens: id="contact-form".
  • Start with a letter, lower case by habit.
  • Case-sensitive: #Email does not find id="email".

Try it yourself

  1. Select the example and choose Try it.
  2. Click the words "Your email" in the preview. The cursor jumps into the box — that is the label connected by id.
  3. Change for="email" to for="mail". Click the label again: nothing happens. Change it back.

Common mistakes

  • The same id twice on a page. Links jump to the wrong place and labels connect to the wrong field. Fix: every id is unique — use a class if many elements share a name.
  • A # in the id itself, such as id="#email". Fix: id="email"; the # goes only in links.
  • A mismatch between for and id. The label no longer works. Fix: copy the name exactly.

Check yourself

  1. Can two elements on one page share an id?
  2. Which attribute on <label> points to a field's id?
  3. When would you use a class instead of an id?

Answers

  1. No. An id must be unique.
  2. for.
  3. 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.

Chat with us on WhatsApp