The ID attribute gives a unique name to an HTML element. It helps you find and style that element easily.
When you want to style one specific element differently from others.
When you want to link to a specific part of a page using a URL.
When you want to select an element in JavaScript to change or read it.
When you want to improve accessibility by labeling elements uniquely.
When you want to create a navigation menu that jumps to sections on the same page.
Syntax
HTML
<tagid="uniqueName">Content</tag>
The id value must be unique on the whole page.
Use only letters, digits, hyphens (-), underscores (_), colons (:), and periods (.) in the id.
Examples
A div element with the ID 'header' to identify the top section.
HTML
<divid="header">Welcome</div>
A paragraph with a unique ID 'intro' for styling or scripting.
HTML
<pid="intro">This is the introduction paragraph.</p>
A section with ID 'contact' to link or style the contact area.
HTML
<sectionid="contact">Contact us here.</section>
Sample Program
This page shows three paragraphs. Only the one with the ID highlight has a yellow background and orange border. The ID lets CSS style just that paragraph.
HTML
<!DOCTYPE html>
<htmllang="en">
<head>
<metacharset="UTF-8">
<metaname="viewport"content="width=device-width, initial-scale=1.0">
<title>ID Attribute Example</title>
<style>
#highlight {
background-color: lightyellow;
border: 2px solid orange;
padding: 1rem;
max-width: 20rem;
margin: 1rem auto;
font-family: Arial, sans-serif;
text-align: center;
}
</style>
</head>
<body>
<h1id="main-title">Welcome to My Page</h1>
<p>This paragraph is normal.</p>
<pid="highlight">This paragraph is special because it has an ID.</p>
<p>Another normal paragraph.</p>
</body>
</html>
OutputSuccess
Important Notes
Remember, each ID must be unique on the page. Using the same ID twice can cause confusion in styling and scripting.
IDs are case-sensitive. Header and header are different.
Use IDs to improve keyboard navigation and screen reader support by linking labels and controls.
Summary
The ID attribute names one unique element on the page.
Use it to style, link, or script that element easily.
Always keep IDs unique and simple.
Practice
(1/5)
1. What is the main purpose of the id attribute in HTML?
easy
A. To uniquely identify one element on the page
B. To add a class to multiple elements
C. To create a hyperlink to another page
D. To add inline styles to an element
Solution
Step 1: Understand the role of the id attribute
The id attribute is used to give a unique name to one element on the page.
Step 2: Compare with other attributes
Unlike classes, which can be shared by many elements, id must be unique and identifies only one element.
Final Answer:
To uniquely identify one element on the page -> Option A
Quick Check:
ID attribute = unique element identifier [OK]
Hint: ID is for one unique element only [OK]
Common Mistakes:
Thinking ID can be used on multiple elements
Confusing ID with class attribute
Using ID to add styles to many elements
2. Which of the following is the correct way to add an ID to a div element?
easy
A.
B.
C.
D.
Solution
Step 1: Check correct syntax for ID attribute
The ID attribute must be written as id='value' with quotes around the value.
Step 2: Identify the correct option
<div id='header'></div> uses correct syntax with quotes and equals sign. <div id header></div> misses equals sign, and D uses wrong colon syntax.
Final Answer:
<div id='header'></div> -> Option D
Quick Check:
ID attribute syntax = id='value' [OK]
Hint: ID attribute needs quotes and equals sign [OK]
Common Mistakes:
Omitting quotes around ID value
Using colon instead of equals sign
Confusing class and ID syntax
3. What will be the visible result in the browser for this HTML snippet?
The snippet uses id='main' on two different div elements, which breaks the rule that IDs must be unique.
Step 2: Verify other syntax
Tags are properly closed, attribute names are correct, and ID values can be any string, so no other errors apply.
Final Answer:
Using the same ID on multiple elements -> Option B
Quick Check:
ID must be unique per element [OK]
Hint: Each ID must be unique on the page [OK]
Common Mistakes:
Reusing same ID on multiple elements
Confusing ID with class for multiple elements
Thinking ID values must be numeric
5. You want to create a navigation menu where each link scrolls to a different section on the same page. Which is the correct way to use the id attribute to achieve this?
hard
A. Assign unique IDs to each section and use <a href='#id'> to link
B. Use the same ID on all sections and link with <a href='#id'>
C. Use classes on sections and link with <a href='.class'>
D. Use inline styles to scroll to sections
Solution
Step 1: Understand linking with IDs
To link to a part of the page, each target section must have a unique id attribute.
Step 2: Use anchor links with href='#id'
The links use href='#id' to jump to the section with that ID. Using unique IDs ensures correct navigation.
Final Answer:
Assign unique IDs to each section and use <a href='#id'> to link -> Option A