3 min read · Free · No login needed
The id attribute gives an element a unique name. No two elements on the same page should share an id. It is used for three things: linking to a specific part of a page, styling one single element with CSS, and letting JavaScript find and change that element.
Give a heading an id — <h2 id="contact">Contact Us</h2> — and a link can jump straight to it: <a href="#contact">Go to Contact</a>. This is how "back to top" links and long-page navigation work.
In CSS, an id is selected with a hash: #header { background: navy; }. Because an id is unique, this rule styles exactly one element. But prefer classes for styling — ids are so specific that they are hard to override later, which causes headaches on large sites.
JavaScript finds elements by id with document.getElementById("myBox"). This is the most common way a script grabs one specific element to change its text, hide it, or read a value from it. You will see this in the JavaScript lesson.
id="Header" and id="header" are two different ids. Keep ids lowercase and consistent to avoid confusion.An id is a unique name for one single element on a page. Unlike a class, which can be shared by many elements, an id can be used only once per page. It is how you point at one exact element — from a link, from CSS, or from JavaScript. In CSS you write it with a hash in front, like #header.
<h2 id="contact">Contact Us</h2>
<!-- a link that jumps to it -->
<a href="#contact">Go to Contact</a>
<style>#contact { color: blue; }</style>
Clicking the link scrolls the page straight to the heading with id="contact". This is how "back to top" links and page menus work — each section has an id, and the menu links to those ids.
id="my section" — an id is one word.#main-title { color: navy; } — the hash selects the one element with id "main-title" and turns it navy. An id rule styles exactly one element.<h1 id="main-title">Welcome</h1> — gives the heading its unique name, which the CSS rule uses to find it.<a href="#contact">Jump to Contact</a> — a link to an id. The hash tells the browser to jump to the element called "contact" instead of opening a new page.<p style="height: 300px;">(scroll down…)</p> — an empty tall paragraph that only exists to make the page long enough to scroll, so you can see the jump happen.<h2 id="contact">Contact</h2> — the target of the link. Clicking "Jump to Contact" scrolls the page straight to this heading.<!DOCTYPE html>
<html>
<head>
<title>Id</title>
<style>
#main-title { color: navy; }
</style>
</head>
<body>
<h1 id="main-title">Welcome</h1>
<p><a href="#contact">Jump to Contact</a></p>
<p style="height: 300px;">(scroll down…)</p>
<h2 id="contact">Contact</h2>
<p>Email: support@kamaldnp.com</p>
</body>
</html>
Change the code below and press Run to see the result instantly. No file to save, no login needed.
The questions learners ask most about this lesson — read the question, then open it to see the answer.
It is a unique name for one single element on a page, used for linking to a section, styling one element, or letting JavaScript find it.
No, an id must be unique on the whole page, or links and JavaScript will pick the first one and behave strangely.
Give the heading an id, then link to it with a hash, such as a link to contact jumping to the contact heading.
AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.
Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.
Take the testPrefer watching? Video courses teach the same topics step by step.
Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.
Browse all coursesAsk about courses, website & more
Request human callback:
Please log in to download. You will stay on this page after logging in!
New here? Register now →