Combinator and grouping selectors, online

Six activities your students do on their own devices. See who's finished, and their scores.

Combinator and grouping selectors in one minute

What students need to know

The > child combinator restricts the match to direct children, which 'ul > li' demonstrates. The + adjacent sibling combinator selects the element right after its sibling, as 'h2 + p' shows.

A comma groups selectors so they share one set of declarations. > selects direct children only; a space selects descendants at any depth.

Mistakes students make

  • The comma groups selectors, so this targets every <article> and every <p> separately. To target paragraphs inside an article, use a space (descendant combinator): 'article p'.
  • Line 1 ends with a period, which starts a class selector and breaks the group. Each grouped selector is separated by a comma: 'h2,'.
  • Line 2 has two child combinators in a row, which is invalid. Use a single '>': 'main > a'.
  • Two adjacent sibling symbols in a row are invalid. Use one '+': 'div + p'.
PREVIOUS · MODULE 37ID selectors
YOU ARE HERE · MODULE 38Combinator and grouping selectors
NEXT · MODULE 39Pseudo-classes

Discover other journeys with this module

What students will learn in this module

Every question in Module 38

Quick questions

  1. Which selector targets every <p> nested anywhere inside a <div>?Adiv > pBdiv p
  2. True or False: 'h1, h2' applies the same rule to both h1 and h2.ATrueBFalse
  3. Which symbol selects the element immediately after a sibling?A~B+
  4. Which targets only the direct child <li> of a <ul>?Aul > liBul li
  5. True or False: 'h2 ~ p' selects every <p> that follows an <h2> and shares its parent.ATrueBFalse

Key terms

Students type the meaning of:

  • div p

Match the pairs

div pMatch it to what it does
h1, h2, h3Match it to what it does
Selects only the direct child elements of a parentMatch it to what it does
Selects the element immediately after a siblingMatch it to what it does
div > > p { }Match it to what it does
h2 + + p { }Match it to what it does

Fill in the blanks

  1. div _ p {
      color: red;
    }

    Hint: Type the child combinator symbol that matches direct children only

  2. h1 _ p {
      margin: 0;
    }

    Hint: Type the adjacent sibling combinator symbol

  3. h1, h2, __ {
      color: teal;
    }

    Hint: Type the next heading selector to continue the comma-separated group

  4. h2 _ p {
      color: gray;
    }

    Hint: Type the general sibling combinator symbol

  5. header a, footer _ a {
      color: navy;
    }

    Hint: Type the child combinator symbol that matches direct children only

Spot the error

Question 1

Aarticle, p {B  color: navy;C}

Question 2

Ah1,Bh2.Ch3 {D  margin: 0;E}

Question 3

Aheader a,Bfooter a,Cmain > > a {D  color: blue;E}

Question 4

Adiv + + p {B  margin-top: 0;C}

Question 5

Aul li,Bol, li {C  color: red;D}