Selecting and Styling

CSS Rule Structure:

  1. Selector – targets which element(s) to style (e.g. h1, #headerOne).
  2. Declaration Block – enclosed in { }, contains one or more declarations.
  3. Declaration – a property: value; pair (e.g. color: gray;).

Example:

h1 {
  color: purple;
  background-color: gray;
}

styles all <h1> tags with purple text on a gray background.

Linking CSS: Use the <link rel="stylesheet" href="style.css"> tag in your HTML’s <head> to apply external styles.

Specificity & Precedence: ID selectors (#headerOne) override type selectors (h1) because they are more specific.


Different Types of Selectors