Element with IDs

HTML id attribute plays a crucial role in identifying and manipulating elements on a webpage. While it may seem simple, the use of id is pivotal in building structured and interactive web pages. This chapter will explore the concept of id in HTML from basic to advanced levels, covering its purpose, usage, and best practices, with comprehensive examples and explanations.

What is the id Attribute in HTML?

The id attribute is used to assign a unique identifier to an HTML element. Each id must be unique within a webpage, meaning no two elements can have the same id value on the same page. This uniqueness allows developers to target specific elements for styling in CSS or to interact with them in JavaScript.

Syntax

				
					<tag id="uniqueId">Content</tag>

				
			
  • The id value should be unique across the page.
  • The id is case-sensitive, meaning "header" is different from "Header".

Example

				
					<div id="header">This is the header</div>
<p id="intro">This is the introduction paragraph.</p>

				
			

Here, the <div> and <p> elements have unique identifiers "header" and "intro", allowing developers to target these specific elements in CSS or JavaScript.

Limitations of HTML id

While the id attribute is powerful, it comes with certain limitations:

  • Uniqueness Requirement: Since an id must be unique on the page, it can’t be used for grouping multiple elements for shared styles or behaviors. For such cases, the class attribute is a better choice.
  • Case-Sensitive: HTML is case-sensitive when it comes to id values, meaning "header" is different from "Header". This can lead to unexpected behavior if not handled carefully.

Difference Between id and class

Both id and class attributes are used to target HTML elements, but they serve different purposes.

  • id: A unique identifier for a specific element on the page. Only one element can have a given id.
  • class: Used to apply the same styling or behavior to multiple elements. Multiple elements can share the same class.

Example

				
					<p id="intro" class="text">This is an introduction paragraph with a unique id and shared class.</p>

				
			

In this example, the paragraph has both a unique id of "intro" and a shared class of "text". This allows for distinct element targeting using id and multiple element styling with class.

Using id in CSS

In CSS, the id attribute is referenced using the # symbol followed by the id value. Since each id is unique, this provides a precise way to style individual elements.

Example

HTML code:

				
					<div id="header">This is the website header</div>
<p id="intro">Welcome to the site!</p>

				
			

CSS code:

				
					#header {
    background-color: lightblue;
    font-size: 24px;
    padding: 10px;
}

#intro {
    color: darkgray;
    font-size: 18px;
}

				
			

Explanation

  • The element with id="header" gets a light blue background and larger font size.
  • The element with id="intro" gets dark gray text and a slightly smaller font size.

Output: The webpage will display a styled header with a blue background and an introductory paragraph in dark gray.

Using id in JavaScript

JavaScript uses the id attribute to identify and manipulate elements on the page. You can select elements by their id using methods like getElementById, which allows for precise DOM manipulation.

Example

HTML code:

				
					<button id="changeTextBtn">Change Text</button>
<p id="displayText">Original text.</p>


				
			

JavaScript code:

				
					document.getElementById("changeTextBtn").addEventListener("click", function() {
    document.getElementById("displayText").textContent = "Text has been changed!";
});

				
			

Explanation

  • A button with id="changeTextBtn" is clicked.
  • When the button is clicked, the JavaScript function changes the content of the paragraph with id="displayText".

Output: Before clicking the button, the paragraph displays “Original text.” After clicking the button, it changes to “Text has been changed!”

Best Practices for Using id in HTML

  • Unique id Values: Always ensure that each id is unique within the page. Reusing the same id on multiple elements will lead to unpredictable behavior, especially when interacting with JavaScript.

  • Use Descriptive id Names: Choose meaningful and descriptive names for your id values to make your HTML more readable and maintainable. For example, use "mainContent" or "footerSection" instead of "div1" or "item2".

  • Avoid Overusing id: While id is useful for targeting specific elements, avoid relying too much on it for styling in CSS. Classes are generally better suited for reusable styles, while id is more appropriate for one-off elements that require unique handling.

Interaction Between id and Forms

The id attribute is crucial when creating accessible forms in HTML. When associating form elements with labels, the id attribute helps create a connection between the form control and its label.

Example

				
					<label for="emailInput">Email:</label>
<input type="email" id="emailInput">

				
			

Explanation

  • The for attribute in the <label> tag links to the id of the <input> field, ensuring that clicking the label focuses on the input field.

This connection improves form usability and accessibility, especially for screen readers.

Handling id with URLs and Anchors

The id attribute can be used to create in-page navigation links. By assigning an id to an element, you can create a link that jumps to that specific section of the page.

Example

HTML code:

				
					<a href="#section2">Go to Section 2</a>

<h2 id="section2">Section 2</h2>
<p>This is the content of section 2.</p>

				
			

Explanation

  • Clicking the “Go to Section 2” link will scroll the page directly to the heading with id="section2".
  • This is useful for creating navigational links, such as a “back to top” button or a table of contents.

Common Mistakes with id

  • Reusing id: Using the same id on multiple elements causes conflicts, especially in JavaScript interactions. Always ensure id values are unique.
  • Overuse in Styling: While it’s possible to use id for styling, it’s generally better to use classes for reusable styles. Use id sparingly for unique, one-time styles or interactions.

Example of a Mistake

				
					<div id="content">Content 1</div>
<div id="content">Content 2</div>

				
			

In this case, both <div> elements have the same id, which should be avoided.

The id attribute is a powerful tool in HTML that allows developers to uniquely identify and interact with elements on a webpage. Whether you're using it for styling in CSS, manipulation in JavaScript, or form controls, the id attribute provides a precise way to target elements. However, with great power comes responsibility: always ensure your id values are unique, meaningful, and used appropriately. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India