HTML id Attribute


Use the id ascribe to control content with JavaScript:


<h1 id="myHeader">Hello World!</h1>
<button onclick="displayResult()">Change text</button>

work displayResult() {
    document.getElementById("myHeader").innerHTML = "Have a pleasant day!";

Try it yourself »

More "Attempt it Yourself" precedents below.

Definition and Usage

The id characteristic indicates a remarkable id for a HTML component (the esteem must be one of a kind inside the HTML document).

The id credit is most used to point to a style in a template, and by JavaScript (by means of the HTML DOM) to control the component with the particular id.

Browser Support

id Yes Yes Yes Yes Yes

Differences Between HTML 4.01 and HTML5

In HTML5, the id property can be utilized on any HTML component (it will approve on any HTML component. Be that as it may, it isn't really useful).

In HTML 4.01, the id quality can't be utilized with: <base>, <head>, <html>, <meta>, <param>, <script>, <style>, and <title>.

Note: HTML 4.01 has more noteworthy limitations on the substance of id esteems (for instance; in HTML 4.01 id esteems can't begin with a number).


<element id="id">

Attribute Values

Value Description
id Specifies a one of a kind id for the component. Naming rules:
  • Must contain no less than one character
  • Must not contain any space characters
  • In HTML, all qualities are case-insensitive


More Examples

Example 1

Use the id credit to connection to a component with a predetermined id inside a page:


<h2><a id="top">Some heading</a></h2>

<p>Lots of text....</p>
<p>Lots of text....</p>
<p>Lots of text....</p>

<a href="#top">Go to top</a>

Try it yourself »

Example 2

Use the id ascribe to style content with CSS:

#myHeader {
    shading: red;
    content adjust: center;

<h1 id="myHeader">welookups is the best!</h1>

Try it yourself »

Related Pages

HTML Tutorial: HTML Attributes

CSS Tutorial: CSS Selectors

CSS Reference: CSS #id Selector

HTML DOM Reference: HTML DOM getElementById() Method

HTML DOM Reference: HTML DOM id Property

HTML DOM Reference: HTML DOM Style Object