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).
The main selectors (8.1.4)
| Selector | Example | Selects |
|---|---|---|
| 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)
| Type | Example | Matches 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> ```
- section p is a descendant selector: any p inside the section at any depth, so One, Two and Three.
- 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.
Check yourself
- Which selector styles every element with class="offer"?
- #offer
- offer
- .offer
- *offer
Show answer
.offer. A class selector starts with a full stop.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Information Technology specification (first teaching 2018), Unit 2 Topics 7 to 11
- Pearson qualifications: Edexcel International Advanced Level Information Technology (2018)
Last updated 8 November 2026. Spotted a mistake? Tell us.