All subjects · Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

CSS selectors, including attribute selectors

Edexcel IAL Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

Selectors choose which elements a rule styles. The specification lists ten kinds of selector and six attribute selectors (spec 8.1.4 and 8.1.7).

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

The main selectors (8.1.4)

SelectorExampleSelects
Type (element)`p`Every p element
Class`.notice`Every element with class="notice"
ID`#banner`The one element with id="banner"
Universal`*`Every element
Descendant`nav a`Every a anywhere inside a nav
Child`ul > li`Every li that is a direct child of a ul
Adjacent sibling`h2 + p`A p that comes immediately after an h2 (same parent)
General sibling`h2 ~ p`Every p that comes after an h2 (same parent)
Multiple (grouping)`h1, h2, h3`Every h1, h2 and h3
Element with class`p.notice`Only p elements with class notice

Attribute selectors (8.1.7)

TypeExampleMatches elements whose attribute
Existence`[required]`exists (any value)
Equality`[type="email"]`is exactly email
Space`[class~="sale"]`is a space-separated list containing the word sale
Prefix`[href^="https"]`begins with https
Substring`[href*="example"]`contains example anywhere
Suffix`[href$=".pdf"]`ends with .pdf

Also [lang|="en"] matches en or a value starting en- (such as en-GB)

Pseudo-classes and examples

A pseudo-class selects an element in a particular state: a:hover (pointer over it), a:visited, input:focus (selected for typing), li:first-child, tr:nth-child(even) (every second row).

/* links to PDF files get a red colour */
a[href$=".pdf"] {
  color: #b00020;
}
/* the paragraph straight after each heading is larger */
h2 + p {
  font-size: 1.2em;
}
/* only links directly inside the nav list items */
nav > ul > li > a {
  text-decoration: none;
}
input:focus {
  outline: 3px solid orange;
}

Worked example

Using the HTML below, which elements does `section p` select, and which does `section > p` select? ```html <section> <p>One</p> <div> <p>Two</p> </div> <p>Three</p> </section> ```

  1. section p is a descendant selector: any p inside the section at any depth, so One, Two and Three.
  2. section > p is a child selector: only p elements whose parent is the section, so One and Three (Two's parent is the div).

Answer: section p selects One, Two and Three; section > p selects One and Three.

Key idea: Type, .class, #id, *, descendant (space), child (>), adjacent sibling (+), general sibling (~), grouping (,). Attribute selectors: [a], [a=v], [a~=v], [a^=v], [a*=v], [a$=v]. Pseudo-classes such as :hover and :focus select states.

Check yourself

  1. Which selector styles every element with class="offer"?
    • #offer
    • offer
    • .offer
    • *offer
    Show answer

    .offer. A class selector starts with a full stop.

  2. Which selector matches only li elements that are direct children of a ul?
    • `ul > li`
    • `ul li`
    • `ul + li`
    • `ul ~ li`
    Show answer

    `ul > li`. > is the child combinator; a space is the descendant combinator.

  3. Which selector matches links whose href ends in .pdf?
    • a[href^=".pdf"]
    • a[href*=".pdf"]
    • a[href$=".pdf"]
    • a[href~=".pdf"]
    Show answer

    a[href$=".pdf"]. $= is the suffix (ends with) selector. ^= is prefix, *= is substring and ~= matches a word in a space-separated list.

  4. What does `h2 + p` select?
    • Every p inside an h2
    • A p that immediately follows an h2 with the same parent
    • Every h2 and every p
    • Every p that comes anywhere after an h2
    Show answer

    A p that immediately follows an h2 with the same parent. + is the adjacent sibling combinator. ~ would select every following p.

  5. Write the selector that targets the element with id="footer-note".
    Show answer

    #footer-note. An ID selector is a hash followed by the id.

  6. What does the selector `h1, p` select?
    • Only p elements inside an h1
    • Every h1 and every p
    • Only an h1 followed by a p
    • Only elements that have the two tags at once
    Show answer

    Every h1 and every p. A comma groups selectors so one rule applies to each.

  7. Which selector styles every link that starts with https?
    • a[href$="https"]
    • a[href="https"]
    • a:https
    • a[href^="https"]
    Show answer

    a[href^="https"]. ^= matches the start of the value.

  8. Which selector changes a button's colour when the mouse pointer is over it?
    • button:focus
    • button:visited
    • button:hover
    • button::before
    Show answer

    button:hover. :hover applies while the pointer is over the element.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.