The id attribute gives an element a unique name. No two elements on a page may share the same id.
In CSS, select an id with a hash: #header { ... }.
JavaScript finds it with document.getElementById("header").
Links can jump to it with href="#header".
Class vs id
A class can be used on many elements; an id belongs to exactly one element.
💡 Key Takeaways: HTML Id
Mastering HTML Id helps you write clean and reliable HTML code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
HTML Id Code Example
HTML
<style>
#banner { background: linear-gradient(135deg, #2563EB, #0EA5E9); color: white; padding: 16px; border-radius: 10px; }
</style>
<h1 id="banner">I am unique</h1>
<button onclick="document.getElementById('banner').innerHTML = 'Changed by id!'">Change the banner</button>
Free sign-in required · Google login available
HTML Id Practice Exercises
Check what you learned in “HTML Id”.
/ 3 solved
Fill in the blank
Give the heading a unique id.
<h1 ="main-title">Welcome</h1>
✓ Correct!Not quite. Try again.
Multiple choice
How many elements on a page may share the same id?
✓ Correct!Not quite. Try again.
Code challenge
Give the paragraph the id "intro".
Write the code in the live editor. Your answer is checked automatically.