HTML Id
The id attribute gives an element a unique name on the page. Where a class is a label that many elements can share, an id is like a passport number: only one element in the whole document should ever carry a given id.
That uniqueness makes ids useful for three jobs:
- Styling one specific element with CSS (
#name { … }). - Linking to a spot inside a page (
<a href="#name">). - Finding an element from JavaScript (
document.getElementById("name")).
Writing an id
<h2 id="pricing">Pricing</h2>Rules for id values:
| Rule | Good | Bad |
|---|---|---|
| Must be unique in the document | one id="menu" |
two elements with id="menu" |
| Must contain at least one character | id="a" |
id="" |
| Must not contain spaces | id="main-menu" |
id="main menu" |
| Case-sensitive | Intro and intro are different |
— |
Tip: Ids may technically start with a digit, but CSS selectors like
#1stthen need escaping. Start ids with a letter and use lowercase words joined by hyphens:contact-form,site-header.
Styling with an id selector
In CSS, an id selector starts with a hash sign #. It matches exactly one element.
<!DOCTYPE html>
<html>
<head>
<style>
#banner {
background: #e34c26;
color: white;
padding: 12px 16px;
border-radius: 8px;
font-family: sans-serif;
}
.note { color: #555; font-family: sans-serif; }
</style>
</head>
<body>
<div id="banner">Summer sale ends Sunday!</div>
<p class="note">This paragraph uses a class.</p>
<p class="note">So does this one — classes can be shared.</p>
</body>
</html>Class vs. id
class |
id |
|
|---|---|---|
| How many elements can share it? | Many | Only one |
| Can an element have several? | Yes: class="card big" |
No, just one id |
| CSS selector | .name |
#name |
| CSS priority (specificity) | Lower | Higher |
| Typical use | Reusable styles | Unique landmarks, links, scripts |
Because id selectors have higher specificity, they beat class rules. That power can make your CSS hard to override later, so many developers prefer classes for styling and keep ids for links and scripts.
Bookmarks: linking to part of a page
If an element has an id, you can jump straight to it with a link whose href is # followed by the id. This is how tables of contents and "Back to top" links work.
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
<nav>
<a href="#ch1">Chapter 1</a> |
<a href="#ch2">Chapter 2</a> |
<a href="#ch3">Chapter 3</a>
</nav>
<h2 id="ch1">Chapter 1</h2>
<p style="height: 300px">A long chapter... scroll down or click a link.</p>
<h2 id="ch2">Chapter 2</h2>
<p style="height: 300px">Another long chapter.</p>
<h2 id="ch3">Chapter 3</h2>
<p>The end. <a href="#ch1">Back to the start</a></p>
</body>
</html>You can also link to a bookmark on another page: <a href="guide.html#install">Installation</a> opens guide.html and scrolls to the element with id="install".
Using ids from JavaScript
JavaScript can grab an element by its id with document.getElementById(). Press the button below — the script finds the paragraph by id and changes its text.
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
<p id="counter">You clicked 0 times.</p>
<button id="btn">Click me</button>
<script>
let clicks = 0;
const output = document.getElementById("counter");
document.getElementById("btn").addEventListener("click", function () {
clicks++;
output.textContent = "You clicked " + clicks + " times.";
});
</script>
</body>
</html>Note: If two elements share an id,
getElementByIdsilently returns only the first one, and bookmark links jump to the first. The page still renders, which makes duplicate ids an easy bug to miss. Validators such as the W3C checker will flag them.
Ids and forms
Ids also connect a <label> to its input through the label's for attribute. Clicking the label then focuses the field, and screen readers announce the label text.
<label for="email">Email address</label>
<input id="email" type="email" placeholder="you@example.com">Try clicking the words "Email address": the cursor jumps into the box.
Summary
idnames one element uniquely; never reuse an id on the same page.- Select it in CSS with
#id, link to it withhref="#id", find it in JS withgetElementById. - Prefer classes for reusable styling; use ids for landmarks, labels and scripts.
Test yourself
Which CSS selector targets the element with id="header"?
- .header
- #header
- header
- *header
How many elements on one page should have id="logo"?
- As many as you like
- At most two
- Exactly one
- None, ids are deprecated
What does <a href="#faq">FAQ</a> do?
- Opens a file called faq
- Scrolls to the element with
id="faq" - Searches the page for the word faq
- Nothing, # is invalid