CSS Selectors

The three main methods for applying styles are as follows:

  1. Apply to all instances of an element
  2. Apply to all elements with a certain id
  3. Apply to all elements with a certain class

Apply to all instances of an element

This is the method with the broadest application, because it affects all instances of the given element. For example, you could assign a certain style to all paragraphs, all links, all unordered lists, or any other element.

Apply to all elements with a certain id

This method applies the style to only one item on the page, because each id must be unique, and only one item per page can have that id. For example, if you have a paragraph with an id of "unique" (<p id="unique">), you could assign styles just to that paragraph.

Apply to all elements with a certain class

This method can be used to apply the same style to an unlimited number of items on the page, by assigning the same class to the items that you want to receive the style. For example, you might have two paragraphs that you want to assign a certain style two. You can do this by giving them the same class.

<p class="highlight">This paragraph will be highlighted.</p>
<p class="highlight">This paragraph will also be highlighted</p>

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.