SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
HTML
1. HTML Introduction: What is HTML? Learn with Examples 2. Best Free HTML Editors: Notepad to VS Code for Beginners 3. HTML Basic Structure: Doctype, Head, Body Explained 4. HTML Elements Explained: Tags, Content & Empty Elements 5. HTML Attributes List: href, src, alt, width, title & More 6. HTML Headings h1 to h6: Which Heading Tag to Use When 7. HTML Paragraphs: p Tag, br Line Breaks & pre Text 8. HTML Style Attribute: Colour, Font Size & Alignment 9. HTML Text Formatting Tags: bold, italic, mark & More 10. HTML Comments: How to Add Notes & Hide Code in HTML 11. HTML Colors: Names, HEX, RGB & HSL Colour Codes 12. HTML CSS: Inline, Internal & External Stylesheets 13. HTML Links: a Tag, href, New Tab & Email Links 14. HTML Images: img Tag, src, alt, Width & Formats 15. HTML Favicon: Create & Add a Browser Tab Icon 16. HTML Title Tag: How to Write a Good Page Title 17. HTML Tables: table, tr, th, td, colspan & rowspan 18. HTML Lists: ul, ol, dl & Nested Lists Explained 19. HTML Block vs Inline Elements: div, p, span Explained 20. HTML Div Tag: Group Content & Build Page Layouts 21. HTML Class Attribute: Style Many Elements at Once 22. HTML id Attribute: Unique IDs, Links, CSS & JavaScript 23. HTML Iframes: Embed YouTube, Maps & Pages in HTML 24. HTML JavaScript: script Tag, Clicks & First Example 25. HTML File Paths: Relative vs Absolute Paths Explained 26. HTML Head Element: title, meta, charset & viewport 27. HTML Layout: header, nav, main, footer Page Structure 28. HTML Responsive Design: Viewport, Images & Media Queries 29. Semantic HTML Tags: header, nav, main, article & footer 30. HTML Forms: input, label, select, checkbox & Submit 31. HTML Entities: & < > © with Examples 32. HTML Audio & Video Tags: controls, source & Fallback 33. HTML Accessibility: alt Text, Labels, Landmarks & ARIA 34. HTML Meta Tags & SEO: charset, viewport, OG Tags 35. HTML Quotations: blockquote, q, cite, abbr & address
Final Test — Certificate ₹10
Try this code live
Lesson 22 of 35

HTML id Attribute: Unique IDs, Links, CSS & JavaScript

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.

Diagram — see how it fits together
Infographic: class vs ID in HTML

Linking to a section

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.

Styling with id

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 and id

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.

Tip: Id values are case-sensitive — id="Header" and id="header" are two different ids. Keep ids lowercase and consistent to avoid confusion.

What an id is

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.

Syntax

<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.

Note: An id must be unique on the whole page. If two elements share the same id, the browser picks the first one, JavaScript picks the first one, and your link jumps to the wrong place. It will not show an error — it will just behave strangely, which is worse.
Tip: Use ids for things that exist once: the main header, the contact section, the top of the page. Use classes for things that repeat: cards, buttons, notes.

Common mistakes

  • Using the same id twice on one page — it must be unique, no exceptions.
  • Putting a space in an id, like id="my section" — an id is one word.
  • Starting an id with a number — it must start with a letter.
  • Styling everything with ids — ids are very strong in CSS and hard to override later. Classes are the better default.

Example explained

  • #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.

Example

Example
<!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>

Output

Output — how it looks in the browser
Output of HTML id Attribute: Unique IDs, Links, CSS & JavaScript

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Frequently asked questions

The questions learners ask most about this lesson — read the question, then open it to see the answer.

1. What is an id in HTML?

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.

2. Can two elements share the same id?

No, an id must be unique on the whole page, or links and JavaScript will pick the first one and behave strangely.

3. How do you link to a section with an id?

Give the heading an id, then link to it with a hash, such as a link to contact jumping to the contact heading.

Why learn this yourself — and not just ask AI?

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.

Get certified — just ₹10

Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

Core Python Programming – 3 Month Complete Course
₹700
View
Full Stack PHP Development Course 2026
₹3,000
View
Ethical Hacking & Cyber Security Workshop
View
Browse all courses
HTML — Lessons
1. HTML Introduction: What is HTML? Learn with Examples 2. Best Free HTML Editors: Notepad to VS Code for Beginners 3. HTML Basic Structure: Doctype, Head, Body Explained 4. HTML Elements Explained: Tags, Content & Empty Elements 5. HTML Attributes List: href, src, alt, width, title & More 6. HTML Headings h1 to h6: Which Heading Tag to Use When 7. HTML Paragraphs: p Tag, br Line Breaks & pre Text 8. HTML Style Attribute: Colour, Font Size & Alignment 9. HTML Text Formatting Tags: bold, italic, mark & More 10. HTML Comments: How to Add Notes & Hide Code in HTML 11. HTML Colors: Names, HEX, RGB & HSL Colour Codes 12. HTML CSS: Inline, Internal & External Stylesheets 13. HTML Links: a Tag, href, New Tab & Email Links 14. HTML Images: img Tag, src, alt, Width & Formats 15. HTML Favicon: Create & Add a Browser Tab Icon 16. HTML Title Tag: How to Write a Good Page Title 17. HTML Tables: table, tr, th, td, colspan & rowspan 18. HTML Lists: ul, ol, dl & Nested Lists Explained 19. HTML Block vs Inline Elements: div, p, span Explained 20. HTML Div Tag: Group Content & Build Page Layouts 21. HTML Class Attribute: Style Many Elements at Once 22. HTML id Attribute: Unique IDs, Links, CSS & JavaScript 23. HTML Iframes: Embed YouTube, Maps & Pages in HTML 24. HTML JavaScript: script Tag, Clicks & First Example 25. HTML File Paths: Relative vs Absolute Paths Explained 26. HTML Head Element: title, meta, charset & viewport 27. HTML Layout: header, nav, main, footer Page Structure 28. HTML Responsive Design: Viewport, Images & Media Queries 29. Semantic HTML Tags: header, nav, main, article & footer 30. HTML Forms: input, label, select, checkbox & Submit 31. HTML Entities: & < > © with Examples 32. HTML Audio & Video Tags: controls, source & Fallback 33. HTML Accessibility: alt Text, Labels, Landmarks & ARIA 34. HTML Meta Tags & SEO: charset, viewport, OG Tags 35. HTML Quotations: blockquote, q, cite, abbr & address
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Please log in to download. You will stay on this page after logging in!

New here? Register now →