For working professionals
For fresh graduates
More
Learn HTML: A Comprehensive Tu…
1. HTML Tutorial
2. HTML Basics
3. HTML Syntax
4. HTML Elements
5. HTML Attributes
6. HTML Comments
7. HTML Semantic
8. HTML Form Elements
9. HTML Head
10. HTML Title
11. HTML Styles
12. HTML Paragraphs
13. HTML Symbols
14. HTML Emojis
15. HTML Formatting
16. HTML Entities
17. HTML Audio
18. HTML Images
19. HTML Lists
20. HTML Links
21. SVG in HTML
22. HTML Forms
23. HTML Video
24. HTML Canvas
25. Adjacency Lists
26. HTML Input Types
27. HTML Tables
28. HTML Table Border
29. Cell Spacing and Cell Padding
30. HTML Semantic Elements
31. HTML Layout
32. html blocks and inline
33. HTML Div
34. Difference Between HTML and CSS
35. Image Map in HTML
36. HTML Drag and Drop
37. HTML Iframes
38. Divide and Conquer Algorithm
39. Difference Between HTML and XHTML
40. HTML Code
41. HTML Colors
42. HTML CSS
43. HTML Editors
44. HTML Examples
45. Class in HTML
46. HTML Exercises
47. HTML ID
Now Reading
48. Understanding HTML Encoding: A Comprehensive Guide
49. HTML Table Style
50. HTML Script
51. Introduction to HTML
When we create a web page, we use many HTML elements. Later, when we want to operate on a specific element using something like CSS, it often becomes difficult to target a single element. This is where the concept of HTML ID comes in. It helps us identify each element by assigning it a name that can be referred to.
As an HTML developer myself, I use the HTML ID attribute very frequently in my projects. Hopefully, after this tutorial, you will be able to use it in your future HTML projects as well!
Before delving into the definition of HTML ID, you need to have a firm grasp of the concept of HTML. Consider going through the basics of HTML first.
Now, let’s talk about HTML ID.
An HTML ID is a unique attribute assigned to an HTML element in order to identify it within the document. IDs are primarily used for scripting and styling purposes. They enable developers to target specific elements with JavaScript for dynamic behavior or to apply specific styles to them with CSS.
Here’s how you can incorporate it into your project. Look at the below example:
<div id="uniqueID">This is an element with an ID attribute in HTML.</div>
In this case, the <div> element is assigned the HTML ID "uniqueID". We used the id tag in HTML. It is also important to keep in mind that IDs must be unique within the HTML document. This means no two elements should share the same ID. This unique trait ensures that individual elements can be reliably targeted and manipulated in scripting or styling using their HTML IDs.
Here are the benefits of using HTML ID in your project.
Let’s further discuss all the HTML elements to which you can assign an HTML ID. Here is a list of HTML elements that support HTML ID.
Elements such as <div> and <section> are frequently assigned IDs to divide the webpage into logical sections, making it easier to target specific content.
IDs are commonly assigned to headings like <h1> to <h6> to help navigate long documents or create anchor links.
Anchor elements <a> use IDs as targets for internal links ('<a>tags withhref="#id"' attribute), allowing users to navigate specific sections of a page.
If we think about it, any HTML element can be assigned an HTML ID. This includes elements such as <div>, <span>, <p>, <h1> to <h6>, <ul>, <ol>, <li>, <a>, <img>, <form>, <input>, <button>, <table>, <tr>, <td>, and many more.
Form elements, including <form>, <input>, <textarea>, <select>, and <button>, can be identified using IDs for scripting or validation.
Table-related elements, such as <table>, <tr>, <td>, <th>, and <caption>, can use IDs to manipulate or style individual table components.
IDs can be used in scripting tasks or to create clickable image maps with specific regions linked to IDs.
IDs are commonly used to link sections or pages in navigation menu elements such as <nav>, <ul>, <ol>, <li>, and <a>.
IDs can be assigned to elements such as <button>, <input type="checkbox">, <input type="radio">, and <input type="submit"> to facilitate scripting and form handling.
Now that we know how HTML ID works, let me share an ID attribute in HTML example. This will give you an idea of industry common practices.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML ID Example</title>
<style>
/* CSS for styling elements with IDs */
#header {
background-color: #853;
color: #fff;
padding: 10px;
text-align: center;
}
#nav {
background-color: #944;
color: #fff;
padding: 5px;
text-align: center;
}
#content {
padding: 20px;
}
#footer {
background-color: #333;
color: #aff;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<div id="header">
<h1> Website to explain HTML ID</h1>
</div>
<div id="nav">
<a href="#section1">Section a</a> |
<a href="#section2">Section b</a> |
<a href="#section3">Section c</a>
</div>
<div id="content">
<h2 id="section1">Section a</h2>
<p>This is the content of Section a.</p>
<h2 id="section2">Section b</h2>
<p>This is the content of Section b.</p>
<h2 id="section3">Section c</h2>
<p>This is the content of Section c.</p>
</div>
<div id="footer">
<p>© HTML ID Example Website</p>
</div>
</body>
</html>
In the above example,
We can also use Javascript to manipulate elements using id tag in HTML Let me demonstrate this using an example HTML program.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML ID JavaScript Example</title>
<script>
function changeText() {
// Get the element with the ID "demo"
var element = document.getElementById("demo");
// Change the text content of the element
element.textContent = "Hello, JavaScript used HTML id!";
}
function changeColor() {
// Get the element with the ID "demo"
var element = document.getElementById("demo");
// Change the background color of the element
element.style.backgroundColor = "lightgreen";
}
</script>
</head>
<body>
<h1>HTML ID JavaScript Example</h1>
<!-- Button to change text -->
<button onclick="changeText()">Change the Text</button>
<!-- Button to change color -->
<button onclick="changeColor()">Change the Color</button>
<!-- Element with ID "demo" -->
<p id="demo">This is the demo paragraph.</p>
</body>
</html>
In the above example,
HTML ID is a powerful tool for uniquely identifying and interacting with elements, customizing styling, improving accessibility, supporting document structure, and improving the web user experience.
To further understand HTML concepts, it is a smart choice to check out certified courses. I would strongly recommend courses from upGrad. They offer courses in collaboration with some of the best universities around the world, and some of the best professors in this field curate their courses.
An HTML ID is a unique identifier for an element in an HTML document. It is used to uniquely identify and target a specific element in CSS for styling or in JavaScript for interactivity and manipulation.
An HTML ID uniquely identifies a single element, whereas a class applies the same styling or behavior to multiple elements. IDs must be unique within a document, whereas classes can be used on multiple elements.
To assign an ID to an HTML element, place the id attribute within the element's opening tag.
There is nothing called data-id in HTML. However, the id attribute, which we have discussed extensively in the article, is used for a different purpose, such as uniquely identifying elements for styling or scripting with JavaScript.
The form ID in HTML is a unique identifier assigned to a <form> tag in HTML using the id attribute. It enables specific form targeting and manipulation with JavaScript or CSS.
You cannot find HTML elements by id in HTML. We can assign an ID to each HTML element. However, in JavaScript, we can use the function getElementById() to find an HTML element by id.
Although HTML elements can have multiple elements with the same HTML ID, they should not. The id attribute in HTML is used to identify a single element within a document uniquely. Having multiple elements with the same ID can cause bugs.
IDs in HTML are case-sensitive. HTML IDs can contain letters, numbers, hyphens (-), underscores (_), and periods. However, in the case of HTML IDs, the usage of special characters like spaces, commas, colons, semicolons, slashes, backslashes, and question marks is prohibited.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.