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.
<element id="id">
id |
Yes | Yes | Yes | Yes | Yes |
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>
To apply a style based on an id
attribute value,
you prefix id with the #
character when defining the style.