Your page has many <p> elements, and you want only the paragraphs inside the <article> to be red. You could add a class to each of those paragraphs, but that is tiring, and it is easy to miss one. It is easier if CSS can pick paragraphs by where they are: "the paragraphs inside the <article>".
That is the job of a combinator, a symbol written between two selectors. A combinator picks elements by how they relate to another element, for example inside it or written right after it. These relationships use family words. An element written directly inside another element is its child, and the element that wraps it is the parent. A child's child is a grandchild. Elements with the same parent are siblings.
A space and >: inside another element
On this page, the <article> has one child <p> and one grandchild <p>, the one inside the <div>:
<h2>Title</h2>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<article>
<p>Child paragraph</p>
<div>
<p>Grandchild paragraph</p>
</div>
</article>
<style>
article > p {
color: red;
}
</style>
articleandpare two ordinary selectors, both tag names.- The
>between them is the child combinator: pick a<p>whose direct parent is an<article>. { color: red; }holds the declaration:color(text colour) set tored.
The result: only "Child paragraph" turns red. "Grandchild paragraph" stays black, because its direct parent is the <div>, not the <article>.
If you replace the > with a space, so the selector is article p, "Child paragraph" and "Grandchild paragraph" both turn red. The space is the descendant combinator (a descendant is a child, a grandchild, or anything deeper): pick every <p> inside the <article>, at any depth.
Combinators work with classes too: .container > p picks a <p> that is a direct child of an element with class="container".
Now try to guess. A <p> is inside a <section>, and that <section> is inside a <div>. Does div > p pick that <p>? No, because the direct parent of that <p> is the <section>. The selector that picks it is div p.
+ and ~: siblings that come after
h2 + p(adjacent sibling: the sibling next to it) picks the one<p>written right after an<h2>. In the example above, that is only "Paragraph 1".h2 ~ p(general sibling) picks every<p>that is written after the<h2>and has the same parent as the<h2>: "Paragraph 1" and "Paragraph 2". The paragraphs inside the<article>are not picked, because their parent is different. The~sign is called a tilde.
An example: h2 + p { margin-top: 0; }. The margin-top property sets the empty space above an element. A <p> has some space above it by default. The value 0 (the same as 0px) removes that space. The heading also has a space below it by default, and when two spaces meet, only the larger one counts. So the first paragraph sits right under its heading only when the space below the heading is 0 too, as in the exercise.
Spaces around >, + and ~ are optional: h2+p means the same as h2 + p.
⚠️ Space, plus and tilde, easy to mix up
- A space that changes the meaning.
.card .title(with a space) picks a.titlethat is inside a.card..card.title(no space) picks one element that has both classes. - Thinking
+picks them all.+picks only the one sibling right after. For all the siblings after, use~. - Another element in between. If there is a
<div>between the<h2>and the<p>,h2 + ppicks nothing, because that<p>is no longer right after the<h2>.h2 ~ pstill picks it. - Looking for an earlier sibling.
+and~only pick siblings written after. There is no combinator for a sibling written before, but there is:has(), covered later in the :has() Selector lesson.h2:has(+ p)picks the<h2>, the earlier sibling, when a<p>comes right after it.
TL;DR: A combinator is a symbol between two selectors that picks elements by how they relate to another element. A space (A B): every B inside A, at any depth. > (A > B): a B that is a direct child of A (A wraps B directly). + (A + B): the one B written right after A. ~ (A ~ B): every B after A that has the same parent as A.
⚡ A direct child or any depth
Write:
article > p {
color: red;
}
Guess first: > only picks a <p> that the <article> wraps directly. A <p> is inside a <div>, and that <div> is inside the <article>. Does that <p> turn red too?
No. The element that wraps that <p> directly is the <div>, not the <article>.
Meaning: The > between two selectors is called a combinator. article > p picks a <p> that is a direct child of the <article>, which means the <article> wraps it directly. If you replace the > with a space (article p), every <p> inside the <article> is picked, at any depth, including a <p> inside a <div> inside the <article>.
⚡ Siblings that come after
Write:
h2 + p {
margin-top: 0;
}
Meaning: + picks the one <p> written right after an <h2>, as long as the two are siblings (the same element wraps both). margin-top: 0; removes the empty space above that <p>. h2 ~ p picks every sibling <p> written after the <h2>. + and ~ never pick a sibling written before.
⚡ One space changes the meaning
Guess first: are .card .title and .card.title the same?
No. .card .title (with a space) picks a .title that is inside a .card. .card.title (no space) picks one element that has both classes.
Meaning: A space between two selectors is a combinator that means "inside". With no space, .card.title means one element that has both classes. But spaces around >, + and ~ are optional: h2+p means the same as h2 + p.