KodingKaro
CSS · Intermediate · Advanced Selectors and Combinators

Combinators and relationship selectors

Combinators express relationships between elements, letting a rule target a descendant, direct child, adjacent sibling, or following sibling.

✓ What you will learn in this topic

  • Explain the key idea: Combinators encode structure; shorter relationships are easier to maintain.
  • Apply the concept: The direct heading and the paragraph immediately after a header are styled according to their exact relationships.
  • Avoid this common mistake:

Concept explanation

A space selects descendants at any depth; > selects direct children. The adjacent sibling combinator + selects the next sibling, and ~ selects later siblings with the same parent. These relationships can be useful for compact component styling.

Relationship selectors describe DOM structure, so keep them short. If a selector depends on many nested wrappers, small markup changes can unexpectedly break its styling.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

.demo > h2 {
  margin-block-start: 0;
}
.demo h2 + p {
  color: #475569;
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
.demo > h2 {
  margin-block-start: 0;
}
.demo h2 + p {
  color: #475569;
}

Expected result: The direct heading and the paragraph immediately after a header are styled according to their exact relationships.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.card > header + p {
  margin-block-start: 0.5rem;
}
.card .actions > button + button {
  margin-inline-start: 0.5rem;
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

Long chains of selectors become brittle when markup changes; a component class is often a clearer contract.

Best practice

Use the least complex selector that expresses the intended relationship and avoid unnecessary specificity.

Practice exercise

Style a card’s direct heading differently from headings inside nested cards, and add an external-link indicator with an attribute selector. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Combinators encode structure; shorter relationships are easier to maintain.
  • Use the least complex selector that expresses the intended relationship and avoid unnecessary specificity.

Knowledge check

Which combinator selects only direct children?

Reveal the explanation

The child combinator is written > and does not match deeper descendants.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
.demo > h2 {
  margin-block-start: 0;
}
.demo h2 + p {
  color: #475569;
}

Expected result:

The direct heading and the paragraph immediately after a header are styled according to their exact relationships.

Hands-on practice task

Style a card’s direct heading differently from headings inside nested cards, and add an external-link indicator with an attribute selector.

In this section

Other lessons in Advanced Selectors and Combinators

Broaden your knowledge

Related learning topics