HTML ID Attributes

The id attribute

id, which simply means identifier, can also be used as an attribute for the element. Well, id means a unique identifier, and by unique, we mean that there can be only one element in the document with the specified id. For example, if there is a heading(h1) element with the id as the main heading, then there can be only one main heading in the document.

Let’s have a look at the below example, where we are using the id attribute. Have a look –

<!DOCTYPE html>
<html lang=”en”>
<meta charset=”UTF-8″>
<meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>The id example</title>
<h1 id=”mainheading”>This is the main heading</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Placeat earum deserunt amet, quam deleniti recusandae inventore vel dolore at quia ipsa architecto modi quidem, hic reiciendis nihil ea animi dolorem.</p>

As you can see, we have used the id attribute here. There is a heading element h1, which is also given an id attribute, which is the main heading. Now, using the main heading id, we can identify that particular heading element from our document.

Using the id attribute, we can identify some particular unique element, from the document. For example, we can use the id main heading, to identify the main heading element.

So, whenever we need to have some particular unique element in our document, we can make use of the id attribute. Remember that there can be only one particular element with the given id.