SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Welcome to CSS
Selecting ElementsCombinatorsAttribute SelectorsPseudo-ClassesPseudo-ElementsSpecificity and InheritanceResolving Style Conflicts Matching selectors and tracing the cascade
Selectors and the Cascade

Combinators

Select elements based on their relationships in the document.

Sometimes a class or element name is not enough. You may want paragraphs inside a particular section, or the paragraph immediately after a heading.

A combinator connects selectors by describing a relationship between elements in the HTML structure.

Read the relationships

Use the same HTML for all four relationships. Start with the descendant selector in the stylesheet tab.

.article p {
  color: green;
}

The structure gives us three useful terms:

  • Child: An element directly inside another element. The heading is a child of the section.
  • Descendant: An element anywhere inside another element, including deeper levels. All three paragraphs inside the section are its descendants.
  • Sibling: An element that shares a parent with another element. The heading, two outer paragraphs, and div are siblings.

When a selector has two parts, start with the element on the right. That is the element the rule can style. Then use the combinator and the selector on the left to check its relationship. For example, in .article > p, begin with each p, then keep only the ones whose direct parent is .article.

The section contains paragraphs A, B and C, with B nested in a div. A and C are direct children and later siblings of the heading. Only A immediately follows it. D is outside.

Descendants at any depth

A space between selectors matches descendants at any depth.

All three paragraphs inside .article match, including the paragraph inside the div. The paragraph outside the section does not match. The section itself is not selected by this rule.

Direct children: >

Use > when the target must be directly inside the first element:

.article > p {
  color: green;
}

This matches “Read the introduction” and “Check the summary.” The nested paragraph does not match because its parent is the div, not the section.

Compare the reach:

  • .article p: Paragraphs at any depth inside the section.
  • .article > p: Only paragraphs whose direct parent has the class article.

The next sibling: +

Use + to select a matching element immediately after another element with the same parent:

h2 + p {
  color: green;
}

Only “Read the introduction” matches: it is the next element after the heading. Spaces and line breaks between HTML tags do not affect the match.

The + combinator is also called the adjacent-sibling combinator.

Later siblings: ~

Use ~ to match later siblings, even when other elements are between them:

h2 ~ p {
  color: green;
}

Both outer paragraphs match. The nested paragraph does not share the heading's parent, so it does not match. This is the subsequent-sibling, or general-sibling, combinator.

Predict the next sibling

Use the relationships in the article to complete the target selector before checking the result:

/* Select the paragraph immediately after the heading. */
h2 + ____ {
  color: green;
}

The blank is p: start with the target element, then check that it is the next sibling of h2. Once that relationship is clear, write the full selector h2 + p without the prompt.

Try it yourself

Try it yourself

Select by relationship

HTML + CSS

Loading code editor…

Live preview

Updating…

Final output

No comparison yetChecks after 3s without edits
Loading your draft…

Key Takeaways

  • Space: Matches descendants at any depth.
  • >: Matches direct children only.
  • +: Matches the next element sibling when it satisfies the second selector.
  • ~: Matches later siblings that satisfy the second selector.
  • Target: In these examples, the selector on the right identifies the elements being styled.

How is this lesson?

Previous lessonSelecting ElementsNext lessonAttribute Selectors
Edit on GitHubReport on GitHub
On this page
%

On this page

Read the relationshipsDescendants at any depthDirect children: >The next sibling: +Later siblings: ~Predict the next siblingTry it yourselfKey Takeaways