Selector lists and compound selectors
Definition and purpose
A selector list joins alternatives with commas. A compound selector joins multiple conditions on the same element without a combinator. Use a list for several kinds of target and a compound selector when a target must satisfy several conditions.
Group titles
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style>h1, h2 { color: navy; }</style> </head> <body> <h1 id="a">Museum</h1> <h2 id="b">Tickets</h2> <p id="c">Details</p> </body></html>Both heading types match; the paragraph does not.
Syntax and mental model
h1, h2 matches either heading type. p.note matches only paragraphs with class note. .card.featured matches elements with both class tokens. In contrast, .card .featured contains a space and means a featured descendant (next lesson). Put the type selector first if present. A normal selector list containing an invalid selector invalidates the rule; forgiving lists are a later topic.
Combine conditions
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style>.card.featured { color: green; }</style> </head> <body> <p id="a" class="card featured">A</p> <p id="b" class="card">B</p> <p id="c" class="featured">C</p> </body></html>Only A has both class tokens.
Common mistakes
A comma is not AND, and a space is not harmless inside a compound selector. Check a selector against an element that should not match as well as one that should.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Type, class, ID and universal selectors. Next lesson: Descendant and child combinators.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Selector lists and compound selectors — knowledge check
Five questions on selector lists and compound selectors.
What does h1, h2 select?
Which matches a p with class note?
What does .a.b require?
Which character changes .a.b into a descendant relationship?
What happens to a normal list with an invalid selector?