HTML idAttributes

HTML 全局属性HTML Global Attributes

Examples

Use the id attribute to manipulate a paragraph using JavaScript:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Example Tutorial (example.com)</title> <script> function displayResult() { document.getElementById("myHeader").innerHTML="Have a nice day!"; } </script> </head> <body> <h1 id="myHeader">Hello World!</h1> <button onclick="displayResult()">Edit Text</button> </body> </html>

Try it Yourself »

More examples are included at the bottom of this chapter.

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the id attribute


Definition and Usage

The id attribute specifies a unique id for an HTML element.

The id must be unique in the HTML document.

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


Differences Between HTML 4.01 and HTML5

In HTML5, the id attribute can be used on any HTML element (it will validate on any HTML element, but may not necessarily be useful).

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

Note:HTML 4.01 has strict restrictions on the value of id (for example, in HTML 4.01, id values cannot start with a number).


Syntax

<element id="id">

Attribute Values

Value Description
id

Specifies the unique id of the element.

Naming rules:

  • Must begin with a letter A-Z or a-z
  • Subsequent characters: letters (A-Za-z), digits (0-9), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")
  • 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 全局属性HTML Global Attributes Other Extensions