The id Attribute

The id attribute assigns a unique identifier to an element. These identifiers are used to apply styles to an element or to select an element with JavaScript.

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

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<style>
#w3clink {
      background: grey;
      color: white;
      padding: 5px;
      border: thin solid black;
}
</style>
<body>
      <a href="http://java2s.com">java2s.com web site</a>
      <p />
      <a id="w3clink" href="http://w3c.org">W3C web site</a>
</body>
</html>
  

The id attribute allow to navigate to a particular section in a document.

For example, example.html#myelement can nevigate to an element whose id is myelement in example.html. The # plus the element id is known as the URL fragment identifier.

Home 
  HTML CSS Book 
    HTML  

HTML5 Global Attributes:
  1. The accesskey Attribute
  2. The class Attribute
  3. The contenteditable Attribute
  4. The contextmenu Attribute
  5. The dir Attribute
  6. The draggable Attribute
  7. The dropzone Attribute
  8. The hidden Attribute
  9. The id Attribute
  10. The lang Attribute
  11. The spellcheck Attribute
  12. The style Attribute
  13. The tabindex Attribute
  14. The title Attribute
Related: