HTML id Attribute: How to Assign Unique Identifiers to Elements

html id attribute

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 id twice 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 id gives 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 id must appear only once in a page.

FAQs

What is the purpose of the HTML <id> attribute?

The <id> attribute uniquely identifies an HTML element on a page. It helps with styling using CSS, scripting with JavaScript, and linking to anchors.
<div id="main">Content</div>

Can you use the same <id> value for multiple HTML elements?

No, each <id> must be unique in a single HTML document. Reusing an <id> can break CSS and JavaScript functionality.
<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?

In CSS, use the hash symbol (#) before the <id>. In JavaScript, use 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?

The <id> attribute allows you to jump to a section of the page using a link with a hash (#) followed by the <id> value.
<h2 id="contact">Contact Us</h2>
<a href="#contact">Go to Contact Section</a>

Similar Reads

HTML sup Tag: How to Add Superscript in Web Pages

The HTML sup tag puts text above the normal line. Writers use it for math power and marks. So, readers…

HTML Elements and Tags: Elements vs Tags Explained

HTML builds web pages. It does this with elements and tags. Many new learners confuse them, so you need to…

HTML details Tag: How to Toggle Hidden Content

The <details> tag hides content and shows it when needed. It helps organize long pages. The tag shows extra info…

HTML a Tag: How to Add Links to Web Page with Examples

The HTML a tag allows you to create links that move users from one page or resource to another. Understand…

HTML object Tag: How to Embed External Objects with Examples

Use the HTML object tag to embed different types of external files into your webpage. It works for videos and…

HTML del Tag: How to Mark Deleted Text in Pages

The HTML del tag shows text that a user or author removed from a page. A browser shows this text…

HTML Select Tag: How Combo Box & Option Work with Examples

HTML select tag and option tags let users pick values in a form. You use them to create dropdowns. The…

HTML meta Tags for SEO

In this article, you will learn what HTML meta tags are and how they work. You also see how they…

HTML Header Tag: The Complete Guide with Examples

The header tag defines the top part of a page or a section in HTML. It usually holds a heading…

HTML address Tag: Contact Information

The address tag in HTML shows contact details. The <address> tag helps show who owns the page or article. Understand…

Previous Article

HTML Data Attributes with Examples

Next Article

HTML Form Validation Attributes

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.