HTML HTML Tutorial HTML Forms HTML Graphics HTML Media HTML APIs HTML Tags



HTML Id

HTML Id is a unique identifier that is used to identify a specific element on a web page. It is a way to give a name to an element so that it can be easily referenced and manipulated using CSS or JavaScript.

The Id attribute is used to define the unique identifier for an element. It is a case-sensitive attribute and must be unique within the HTML document. If two or more elements have the same Id, it will cause errors in the web page and may not function properly.

The syntax for defining an Id attribute is as follows:

<element id="unique_id">

The "unique_id" can be any string of characters that is unique within the HTML document. It can contain letters, numbers, and underscores, but cannot start with a number or contain spaces.

Here are some examples of how to use the Id attribute:

<div id="header">
  <h1>Welcome to my website</h1>
</div>

<p id="intro">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

<img src="image.jpg" alt="My Image" id="my_image">

In the above examples, the first element is a div with an Id of "header", the second element is a paragraph with an Id of "intro", and the third element is an image with an Id of "my_image".

Using the Id attribute, we can easily target specific elements on a web page using CSS or JavaScript. For example, if we want to change the font color of the "intro" paragraph to red, we can use the following CSS code:

#intro {
  color: red;
}

This will change the font color of the "intro" paragraph to red.

Similarly, if we want to change the source of the "my_image" element using JavaScript, we can use the following code:

document.getElementById("my_image").src = "new_image.jpg";

This will change the source of the "my_image" element to "new_image.jpg".

Overall, the Id attribute is a powerful tool for web developers to identify and manipulate specific elements on a web page. It allows for greater control and customization of the user interface, and can greatly enhance the user experience.

References

Activity