1. Home
HTML

Learn HTML: A Comprehensive Tutorial for Beginners | Step-by-Step Guide

Learn HTML from scratch! Our tutorial covers basics to advanced concepts. Start coding websites today with step-by-step guidance.

  • 49
  • 12 Hours
right-top-arrow
47

HTML ID: Key to Efficient Web Development

Updated on 28/08/2024441 Views

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!

What is HTML ID?

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.

Advantages of using HTML ID

Here are the benefits of using HTML ID in your project.

  • Identify elements: The HTML ID attribute enables you to uniquely identify individual elements in an HTML document. This distinction is critical for precisely targeting specific elements, particularly in scripting and styling tasks.
  • Targeting an element during scripting: HTML IDs allow you to precisely target and manipulate specific elements using JavaScript or other scripting languages. This is especially useful for dynamic web applications that require updating or interacting with specific elements in response to user actions.
  • Styling with CSS: IDs allow you to apply specific styles to individual elements. This targeted styling aids in the creation of custom designs and layouts, which enhances the visual appeal and user experience of web pages.
  • SEO optimization: Search engine crawlers can use IDs to determine the structure and hierarchy of content on a website. This may improve search engine optimization (SEO) by providing clearer indications of the page's content organization.
  • Improves document structure: IDs can be used to identify key sections or elements within a document, resulting in a well-structured and organized HTML hierarchy. This makes the codebase more understandable and maintainable.

Elements That Support the HTML ID Attribute

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.

Sections and divisions

Elements such as <div> and <section> are frequently assigned IDs to divide the webpage into logical sections, making it easier to target specific content.

Headings

IDs are commonly assigned to headings like <h1> to <h6> to help navigate long documents or create anchor links.

Links and anchors

Anchor elements <a> use IDs as targets for internal links ('<a>tags withhref="#id"' attribute), allowing users to navigate specific sections of a page.

HTML elements

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

Form elements, including <form>, <input>, <textarea>, <select>, and <button>, can be identified using IDs for scripting or validation.

Table elements

Table-related elements, such as <table>, <tr>, <td>, <th>, and <caption>, can use IDs to manipulate or style individual table components.

Images

IDs can be used in scripting tasks or to create clickable image maps with specific regions linked to IDs.

Navigation elements

IDs are commonly used to link sections or pages in navigation menu elements such as <nav>, <ul>, <ol>, <li>, and <a>.

Interactive elements

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.

HTML ID example

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.

HTML ID example

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>&copy; HTML ID Example Website</p>

</div>

</body>

</html>

In the above example,

  • To style the elements like header, navigation, content, and footer sections, we have used HTML IDs like #header, #nav, #content, and #footer.
  • Each div, like footer, content, etc., has been given a unique div ID in HTML.
  • Anchor links ('<a>') in the navigation (#nav) point to specific sections (#section1, #section2, #section3) in the content area.
  • Each section in the content area (#section1, #section2, and #section3) is assigned an ID for internal linking and styling using id HTML CSS.

HTML ID Using JavaScript

We can also use Javascript to manipulate elements using id tag in HTML Let me demonstrate this using an example HTML program.

HTML id using JavaScript

Output for HTML id using JavaScript

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,

  • We have two JavaScript functions (changeText and changeColor) that show how to edit items using their HTML ID. 
  • The changeText function modifies the text content of the element with ID "demo," whereas the changeColor function modifies the background color of the same element.
  •  These functions are activated by buttons, which, when clicked, call the corresponding functions.

Summing Up

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.

Frequently Asked Questions

  1. What is an HTML ID?

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. 

  1. How is an HTML ID different from a class?

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.

  1. How do you assign an ID to an HTML element?

To assign an ID to an HTML element, place the id attribute within the element's opening tag. 

  1. What is data ID in HTML?

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.

  1. What is the form id in HTML?

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.

  1. How to find HTML elements by ID?

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.

  1. Can an HTML document have multiple elements with the same 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.

  1. Can you use special characters in HTML IDs?

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.

Mukesh Kumar

Mukesh Kumar

Working with upGrad as a Senior Engineering Manager with more than 10+ years of experience in Software Development and Product Management.

Get Free Career Counselling
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...