Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

ID selector — #id

The #id selector matches an element by its identifier. Learn its syntax, specificity, and what ID uniqueness means for matching.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

#id { property: value; }

#id denotes an ID selector: place the ID value immediately after #, without whitespace. In HTML, #notice matches an element with id="notice", regardless of its type. p#notice additionally requires a p element.

The value after # must use valid CSS identifier syntax; some values require escape sequences. Matching is case-sensitive except in quirks mode, where it is ASCII case-insensitive.

An ID should be unique within the document. Duplicate IDs do not restrict the selector to the first element, however: it can match multiple elements in a nonconforming document.

The specificity of #notice is (1,0,0), whereas [id="notice"] has (0,1,0). Do not confuse them. In the example, the #notice rule wins over the p rule: Selected should be blue and Other black.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>ID selector</title>
  <style>
    p { color: rgb(0, 0, 0); }
    #notice { color: rgb(0, 0, 255); }
  </style>
</head>
<body>
  <p id="notice">Selected</p>
  <p>Other</p>
</body>
</html>

Result

Selected: rgb(0, 0, 255)
Other: rgb(0, 0, 0)

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources