The ID Selector

The id selector uses the id attribute of an HTML element to select a specific element. An id should be unique within a page, so the id selector is used if you want to select a single, unique element. To select an element with a specific id, write a hash character, followed by the id of the element:

HTML file: Displayed by browser:
<style>
#para1 {
   text-align: center;
   color: blue;
}
</style>
<body>
  <p>Not every paragraph may be affected by the style.</p>
  <p id="para1">This one is!</p>
  <p>But not this one!</p>
</body>

Not every paragraph may be affected by the style.

This one is!

But not this one!

The style rule is applied ONLY to the HTML element with id="para1".

Good habit NEVER start an ID name with a number! It won't work! ID is short for 'fragment identifier', and is often used improperly. You can only use an ID name once in any XHTML or HTML document. Duplicate ID tags will cause your page to fail validation, and can have negative effects when using JavaScript with them. Simply put, an ID is like the sticker that says "you are here" on a mall directory, and there can only ever be one of those.
Back button Table of Contents Next button