HTML id Attribute Tutorial

In this section, we will learn what the id attribute is and how to use it in HTML.

id Attribute in HTML: Definition and Usage

The `id` attribute is a way of uniquely identifying an element in an HTML document.

Basically, with the help of id attribute we can set a value for an HTML element, and then in other languages like JavaScript or CSS, we can access that element via the value of its id attribute.

Note: the value we set for an id attribute should be unique in that HTML document. Meaning only one HTML element can have that value for its id attribute.

HTML id Attribute Syntax

<element id = “value”>

HTML id Attribute Values

The value set for the id attribute could be any general or specific names. But it’s better to always endeavor for names that specifically describe the element it applied to.

Example: div with id

See the Pen div with id by Omid Dehghan (@odchan1) on CodePen.

In this example, we’ve used the id value of the <div> element in CSS to apply a style to it.

Example: CSS by id

See the Pen CSS by id by Omid Dehghan (@odchan1) on CodePen.


Top Technologies