HTML & CSS
Article
By Adam Roberts

id (HTML attribute)

By Adam Roberts
Help us help you! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min
--ADVERTISEMENT--

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.

Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.Is it good?