The id attribute adds a unique name to an HTML element. It helps with CSS and JavaScript in the page. You can only use each id once.
What Is the HTML id Attribute?
The id attribute gives a unique name to one element in a page. No other element should use the same name. It works as a reference point for styles or scripts.
This name can include letters, numbers, hyphens, and underscores. It must not have spaces.
You can add an id to almost any element. It sits inside the opening tag. Here’s a basic use:
<div id="main-content">Welcome</div>This tag adds the id name main-content to the <div>. Now you can target this div from CSS or JavaScript.
Here are the rules to help you choose a name for an HTML id attribute:
- Use short names that explain the role or purpose.
- Use lowercase with hyphens or underscores if needed.
- Don’t use the same
idtwice on the same page. - Avoid starting with numbers.
Good names:
header, user-list, main_section
Bad names:
123box, test, content1 and content1 again
You can use the id in CSS by placing a # before the name. This lets you style one single element.
Here is an example:
#main-content {
font-size: 18px;
color: black;
}This rule targets only the element that has id="main-content".
You can grab any element with its id in JavaScript. The function getElementById finds the element fast.
For example:
const box = document.getElementById('box');
box.style.background = 'red';This script finds the element with id="box" and sets its background color.
The id helps with these:
- Style one element
- Change one element with JavaScript
- Make internal links in a long page
- Help screen readers know the structure
So, why id must values be unique in an HTML document?
If you reuse the same id, the browser may act in strange ways. JavaScript will only find the first match. CSS will not behave as expected.
Examples of id Attribute in HTML
Style One Section with CSS
<section id="about">About Our Work</section>
<style>
#about {
background: lightgray;
padding: 10px;
}
</style>This code adds a gray background and padding to the <section> with the id="about". The style rule only affects this part of the page.
Use id to Scroll to a Section
<a href="#contact">Go to Contact</a>
<div style="height: 700px;"></div> <!-- for scroll -->
<section id="contact">Contact Us</section>This link jumps to the #contact section when clicked. It helps in long pages to move the user to the right place without reload.
Control Element with JavaScript:
<div id="notice">Click the button</div>
<button onclick="hideNotice()">Hide</button>
<script>
function hideNotice() {
document.getElementById('notice').style.display = 'none';
}
</script>This example hides the div when you click the button. The script targets the element using its id and changes its style.
Wrapping Up
In this article you learned what the id attribute does and how it helps with styling, scripting, and navigation. You also saw how each id must be unique.
Here is a quick recap:
- The
idgives a unique name to an element. - You can use it in CSS with
#idname. - You can find it in JavaScript with
getElementById. - It helps users jump to parts of a page.
- Every
idmust appear only once in a page.
FAQs
What is the purpose of the HTML <id> attribute?
<div id="main">Content</div>
Can you use the same <id> value for multiple HTML elements?
<p id="intro">First paragraph</p>
<p id="intro">Second paragraph</p> <!-- Invalid -->
How to select an element with an <id> in CSS or JavaScript?
document.getElementById to access it.
/* CSS */
#header {
color: red;
}
// JavaScript
const el = document.getElementById("header");
el.style.fontSize = "18px";
How is the HTML <id> attribute used in anchor links?
<h2 id="contact">Contact Us</h2>
<a href="#contact">Go to Contact Section</a>Similar Reads
The br tag adds a single line break in HTML. It is an empty tag that does not need a…
The HTML noscript tag tells the browser what to show when JavaScript does not run. This tag targets users who…
The breadcrumb in HTML shows a path through a site and marks the place of a page inside it. It…
The HTML embed tag is used to display media content, such as videos or audio. It can also display PDFs…
The HTML form tag helps you collect user input on a website. You use the HTML form tag to create…
This article shows you how to open a link in a new tab. Read the code and follow the examples…
Use the HTML object tag to embed different types of external files into your webpage. It works for videos and…
The HTML <cite> tag is a small but important tool for marking up the title of a creative work. You…
data-* attributes store extra values on elements without an extra DOM nodes or backend requests. What are data-* attributes in…
HTML Boolean attributes control the behavior of elements and do not need a value. These attributes simplify logic and reduce…