id (HTML attribute)

    Adam Roberts
    Share

    Description

    The id attribute provides a unique identifier for an element within the document. It may be used by an a element to create a hyperlink to this particular element.

    This identifier may also be used in CSS code as a hook that can be used for styling purposes, or by JavaScript code (via the Document Object Model, or DOM) to make changes or add behavior to the element by referencing its unique id.

    Note that the id attribute cannot be applied to the following elements:

    • base
    • head
    • html
    • meta
    • script
    • style
    • title
    <span id="thisspan">A uniquely identifiable element.</span>

    Value

    The most important aspect of the id attribute is that it must be absolutely unique. Unlike the class attribute, which may apply the same value to many elements in a page, an id that’s applied to an element must not match an id used anywhere else on the same page.

    The id attribute value must begin with a letter in the roman alphabet (a–z or A–Z); this can be followed by any combination of letters (a–z or A–Z), digits (0–9), hyphens (-), underscores (_), colons (:), and periods (.). The id value is case sensitive, thus <span id="me">This is me</span> and <span id="ME">This is me</a> would be considered to be separate and uniquely identifiable elements on the same web page.