How to Give an Element an ID in HTML
The id attribute gives one element a name that no other element on the page has. CSS can style it with # and that name, scripts can find it by it, and a link ending in # and the name jumps to it.
Syntax
<element id="<id>"><content></element>
| Part | What it does |
|---|---|
element | The tag name of any element, such as p or div. |
id="<id>" | The element's unique name. Use each id on one element only, and don't put spaces in it. |
<content> | What's inside the element. |
MDN's id page explains that an id must be unique in the whole page and must not contain spaces.
The Example
The example is a meeting note with two paragraphs. The second one has id="warning", and a CSS rule for #warning colors it red. The rule sits in a style element in head.
Step 1: Write the Code
Create a file named index.html in a text editor, such as Visual Studio Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Meeting</title>
<style>
#warning {
color: #c50f1f;
}
</style>
</head>
<body>
<p>Meeting at 10:00.</p>
<p id="warning">Room 2 is closed today.</p>
</body>
</html>
Step 2: Open It in a Browser
Double-click index.html. It opens in your browser:

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
- Only the paragraph with
id="warning"is red.#warningin the CSS matches the element whoseidiswarning, and Chromium reported its color asrgb(197, 15, 31), which is#c50f1f. - The first paragraph stayed black, because it has no
id.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Giving two elements the same id | The page is no longer valid, and the two disagree: the CSS rule colored both, but document.getElementById('warning') returned only the first | Give each element its own id, or use a class for a group |
| Changing the capital letters | An id is case-sensitive: an element with id="Warning2" wasn't found as warning2 | Write the id the same way everywhere |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The #id rule doesn't apply | The id in the HTML and in the CSS differ, maybe only in capital letters | Make them identical |
| A script only finds one of several elements | They share an id | Give them a class instead, and select by the class |
FAQ
How do I link to an element with an id?
Add # and the id to the address. On a long test page, a link with href="#warning" added #warning to the address and scrolled the page down until the element was on the screen.
What's the difference between id and class?
An id names one element. A class can be shared by as many elements as you like, and an element can have several classes.
Can an id start with a number?
It can, but CSS can't use it as it is. document.getElementById('1st') found the element, while document.querySelector('#1st') threw SyntaxError: Failed to execute 'querySelector' on 'Document': '#1st' is not a valid selector. MDN's id page recommends ids that work in CSS without escaping, so start them with a letter.
Conclusion
An id names one element, once on the page. Style it with # and the name, link to it with # and the name, and keep every id unique and spelled the same everywhere.
Additional Resources
- id global attribute, on MDN.
