English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML Reference Manual

Complete List of HTML Tags

HTML id attribute

The HTML id global attribute defines a unique identifier (ID) for the entire document. It is used to identify elements in links (using fragments), scripts, and styles (through CSS).

HTML Global Attributes

Online Example

Change the content of a paragraph using JavaScript script through the id attribute:

<!DOCTYPE html
<html
<head
<meta charset="utf-8"> 
<title>Basic Tutorial Website(oldtoolbag.com)</title> 
<script
function changeText()
{
    document.getElementById("myHeader").innerHTML="Haha, I've been changed, darling!";
}
</script>
</head>
<body
<h1 id="myHeader">Hello World!/h1>
<button onclick="changeText()">Dynamically change the text content of the id</button>
</body>
</html>
Test and see ‹/›

Browser Compatibility

IEFirefoxOperaChromeSafari

All mainstream browsers support the id attribute

Definition and Usage

The id attribute specifies the unique id of an HTML element.

The id must be unique in the HTML document.

The id attribute can be used as a link anchor (link anchor), to change or add styles to the element with the specified id through JavaScript (HTML DOM) or through CSS.

HTML 4.01 with HTML5differences

in HTML5 in, the id attribute can be used for any HTML element (it will validate any HTML element. But it may not be useful).

in HTML 4.01 in, the id attribute cannot be used for: <base>, <head>, <html>, <meta>, <param>, <script>, <style>, and <title>.

Note: HTML 4.01 There are strict restrictions on the value of id (For example: in HTML 4.01 The id value cannot start with a number).

Syntax

    <element id="id">

Attribute value

ValueDescription
id

Specifies a unique id for the element.

Naming rules:

  • Must start with letter A-Z or a-z starts

  • The following characters: letter (A-Za-z)、number (0-9(")、hyphen ("--(")、underscore ("_")、colon (":") and period (".")

  • Values are case-sensitive

More examples

Using the id attribute in CSS
How to use the id attribute in CSS to add styles to elements

HTML Global Attributes