Chapter 58. Defining ID Styles

An ID selector in CSS is much like a tag-specific class in that it is an exclusive club. It's so exclusive, in fact, that it applies to one and only one element per page. No two elements should belong to the same ID, even if the elements are of the exact same type: paragraphs, headings, images, links, or what have you.

To define an ID selector, you use special syntax—namely, the number sign (#), followed by the name of the ID:

#barney {
  font-weight: bold;
  color: #FF00FF;
}

GEEKSPEAK

An ID selector is a selector that applies to one and only one element per page.

Then, set the id attribute of the tag to which you want to apply the style:

<p id="barney">This paragraph has the ID of "barney."</p>

IDs come in handy ...

Get Web Design Garage now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.