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 main Tag: How to Create a Primary Content Area

The <main> tag in HTML marks the central content of a webpage. It tells browsers and assistive technologies where the…

HTML Class Attribute: Use in CSS and JavaScript

The HTML class attribute helps group elements. It helps you update the page style without changing the structure of HTML.…

HTML link Tag: Linking Stylesheets

The link tag in HTML connects a web page to external resources like stylesheets or icons. It's placed in the…

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 autocomplete Attribute: How to Use it with form and input

The form autocomplete attribute in HTML helps users fill out forms faster because it shows them saved input for common…

HTML Data Attributes with Examples

data-* attributes store extra values on elements without an extra DOM nodes or backend requests. What are data-* attributes in…

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 Article Tag: How to Create Semantic Content

Semantic article tag in HTML uses clear tags that show the role of each part. These tags tell browsers and…

The em Tag in HTML: How to Emphasize Text in HTML

The HTML em tag shows stress or emphasis in text. It changes meaning for readers and screen readers. What is…

HTML template Tag: How it Works with Examples

The HTML <template> tag lets you store HTML content that won’t show up right away. JavaScript can pull it in…

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.