Multiple Class / ID and Class Selectors

Here are some tips on how to use multiple class/ID and class selectors:

  • Use multiple class selectors to style elements that have multiple classes. For example, the following CSS will style all elements that have the classes red and bold:
.red.bold {
  color: red;
  font-weight: bold;
}
  • Use ID and class selectors to style elements that have both an ID and a class. For example, the following CSS will style the element with the ID header and the class red:
#header.red {
  color: red;
}
  • Use multiple selectors to style elements that match multiple criteria. For example, the following CSS will style all elements that are red and have the class bold:
.red.bold {
  color: red;
  font-weight: bold;
}
  • Use the and (&&) operator to combine multiple selectors. The and operator ensures that both selectors match before the style is applied. For example, the following CSS will style all elements that are red and have the class bold, but not elements that are red and have the class italic:
.red.bold && !.italic {
  color: red;
  font-weight: bold;
}
  • Use the or (||) operator to combine multiple selectors. The or operator ensures that either selector matches before the style is applied. For example, the following CSS will style all elements that are red or have the class bold:
.red || .bold {
  color: red;
  font-weight: bold;
}
  • Use the not (!) operator to negate a selector. The not operator ensures that the style is not applied to elements that match the selector. For example, the following CSS will style all elements that are not red:
.red {
  color: red;
}

!.red {
  color: black;
}

I hope these tips help you use multiple class/ID and class selectors more effectively.