HTML global attribute id








The id attribute assigns a unique identifier to an element.

These identifiers are commonly used to apply styles to an element or to select an element with JavaScript.

Syntax

<element id="id">

Attribute Values

id
uniqe id for element

Browser compatibility

id Yes Yes Yes Yes Yes




Example

The following code uses id attribute to add style to an element.

<!DOCTYPE HTML>
<html>
<style>
#w3clink   {<!--  ww w  .j a va  2 s . c o m-->
    background:grey; 
    color:white; 
    padding:5px;
    border:thin solid black;
}
</style>
<body>
    <a href="http://java2s.com">web site</a>
    <br/>
    <a id="w3clink"  href="http://w3c.org">W3C web site</a>
</body>
</html>

Click to view the demo

To apply a style based on an id attribute value, you prefix id with the # character when defining the style.