CSS Basics · Lesson 2 of 11

Classes and IDs

A selector like p styles every paragraph. To style only some elements, give them a name in the HTML and use that name in the CSS.

Classes

A class is a label you can put on as many elements as you like:

<p class="warning">The floor is wet.</p>

In CSS, select a class with a dot in front of its name:

.warning {
  color: red;
}

An element can have several classes, separated by spaces: class="warning big".

IDs

An id names one single element. No two elements on a page may share an id.

<p id="intro">Welcome!</p>

Select an id with a hash sign:

#intro {
  font-size: 20px;
}

Which one to use?

Use classes for almost all styling, because they can be reused. Keep ids for the few things that truly exist once, and for connecting labels to form fields.

You choose the names. Pick ones that describe what the thing is (warning, price, card), not how it looks (red, big). The look may change later; the meaning will not.

Your Task

  1. In index.html, add the class special to the two list items marked as today's specials.
  2. In style.css, make everything with the class special crimson.
  3. Give the paragraph at the bottom the id note, and use an id selector to make its text gray.
  • The two specials have the class special, and the other items do not
  • The specials are crimson and the other items are not
  • The last paragraph has the id note
  • The CSS uses the #note selector to make it gray