Selecting Elements by ID

The ID selector selects elements by the global id attribute.

Syntax:

 
#idvalue
  

or


elementType.#idvalue

The value of an element's id attribute must(should) be unique within the HTML document.

 
<!DOCTYPE HTML> 
<html> 
    <head> 
        <title>Example</title> 
        <style type="text/css"> 
        #myAnchor { 
            border: thin black solid; 
            padding: 4px; 
        } 
        </style> 
    </head> 
    <body> 
        <a id="myAnchor" href="http://java2s.com"> Visit the java2s.com </a> 
    </body> 
</html>
  
Click to view the demo
Home 
  HTML CSS Book 
    CSS  

Selectors:
  1. Selecting All Elements
  2. Selecting Elements by Type
  3. Selecting Elements by Class
  4. Selecting Elements by ID
  5. Selecting Elements by Attribute
Related: