Skip to main content

How to Give an Element an ID in HTML

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<element id="<id>"><content></element>
PartWhat it does
elementThe 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:

index.html, an HTML page with a style element whose rule for #warning sets a red color, and two paragraphs: Meeting at 10:00. and Room 2 is closed today. with the id warning.

index.html
<!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 page in Chromium: Meeting at 10:00. in black, and Room 2 is closed today. in red.

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. #warning in the CSS matches the element whose id is warning, and Chromium reported its color as rgb(197, 15, 31), which is #c50f1f.
  • The first paragraph stayed black, because it has no id.

Common Mistakes​

MistakeWhat happensDo this instead
Giving two elements the same idThe page is no longer valid, and the two disagree: the CSS rule colored both, but document.getElementById('warning') returned only the firstGive each element its own id, or use a class for a group
Changing the capital lettersAn id is case-sensitive: an element with id="Warning2" wasn't found as warning2Write the id the same way everywhere

Troubleshooting​

SymptomLikely causeFix
The #id rule doesn't applyThe id in the HTML and in the CSS differ, maybe only in capital lettersMake them identical
A script only finds one of several elementsThey share an idGive them a class instead, and select by the class

FAQ​

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​