Selecting Elements by ID

Description

The ID selector selects elements by the id attribute.

  • Selector: #idvalue or elementType.#idvalue
  • Matches: The element with idvalue as the id global attribute
  • Since CSS Version: 1

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

The ID selector is looking for a single element.

Example

The following code demonstrates the use of the id selector.


<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
#w3canchor {<!--   w  w w. ja va 2 s. com-->
  border: thin black solid;
  padding: 4px;
}
</style>
</head>
<body>
  <a id="anchor" href="http://java2s.com">Visit the website </a>
  <a id="w3canchor" href="http://w3c.org">Visit the W3C website</a>
</body>
</html>

Click to view the demo





















Home »
  HTML CSS »
    CSS »




CSS Introduction
CSS Background
CSS Border
CSS Box Layout
CSS Text
CSS Font
CSS Form
CSS List
CSS Selectors
CSS Others
CSS Table