HTML Giving an element an ID


The ID attribute of an element is an identifier which must be unique in the whole document. Its purpose is to uniquely identify the element when linking (using an anchor), scripting, or styling (with CSS).

<div id="example-id"></div>

You should not have two elements with the same ID in the same document, even if the attributes are attached to two different kinds of elements. For example, the following code is incorrect:

<div id="example-id"></div>
<span id="example-id"></span>

Browsers will do their best to render this code, but unexpected behavior may occur when styling with CSS or adding functionality with JavaScript.

To reference elements by their ID in CSS, prefix the ID with #.

#example-id { color: green; }

To jump to an element with an ID on a given page, append # with the element name in the URL.

This feature is supported in most browsers and does not require additional JavaScript or CSS to work.