WDD 130: Web Fundamentals

CSS Selectors: Type, Universal, Class and ID

Overview

CSS Selectors allow us to target specific elements on the page to apply a CSS rule which rule is a collection of CSS declarations.

"A CSS selector is the first part of a CSS Rule. It is a pattern of elements and other terms that tell the browser which HTML elements should be selected to have the CSS property values inside the rule applied to them. The element or elements which are selected by the selector are referred to as the subject of the selector." - MDN

Prepare

Activity Instructions

  1. In your browser, navigate to the "Try-It" section on this MDN page: Class Attribute.
  2. In the HTML panel, remove the "note" class from the second paragraph (<p>) leaving only one class for that paragraph named editorial .
    What happens? Use the CSS panel to help you answer the question.
    Solution Example That second paragraph is no longer styled with the "note" class and therefore loses the italicized and bold font style and weight.
  3. In the HTML panel, add an id attribute to the fourth (last) paragraph (<p>) and name it "highlight".
  4. In the CSS panel, write a CSS rule for the highlight id selector what underlines the text and gives a yellowish background to the selected element.
    Solution Example

    HTML

    <p id="highlight" class="note"> ... </p>

    CSS

    #highlight {
      text-decoration: underline;
      background-color: lightyellow;
    }
    Example Solution from MDN TryIt Editor
    Figure 1: Screenshot of example solution form the TRY IT editor on MDN

Optional Resources