← Back to HTML & CSS, page 2

html-css-0030 · Easy fresher

What are the CSS combinators: descendant, child, adjacent sibling and general sibling?

Combinators describe how two selectors are related in the tree: a space means any descendant, > a direct child, + the next adjacent sibling and ~ any following sibling. Attribute selectors such as [type="email"] and [href^="https"] add matching by attributes. Siblings only look forward; :has() is needed to look back.

Full answer

A combinator is the symbol between two selectors that says how the elements must be related in the HTML tree.

  • Descendant (a space): nav a matches any a inside a nav, at any depth.
  • Child (>): ul > li matches an li that is a direct child of ul. Grandchildren are excluded.
  • Adjacent sibling (+): h2 + p matches a p that comes immediately after an h2 with the same parent.
  • General sibling (~): h2 ~ p matches every p that comes after an h2 with the same parent, not necessarily right after it.

.menu > li draws a border around the outer li only. The inner li is a grandchild of .menu, so it is not matched. (A border is not inherited, which makes the effect easy to see. Something like font-weight would pass down to the inner item anyway.) The adjacent rule colours only the first paragraph, while the general sibling rule underlines both. The link matches nav a and a[href^="https"], and green wins because the attribute selector adds a class-level point to the score, not because it comes later.

Attribute selectors are close relatives. [type="email"] is an exact match, [href^="https"] means starts with, [href$=".pdf"] ends with, and [class*="btn"] contains.

html
<style>
  nav a            { color: navy; }
  .menu > li       { border: 1px solid #999; }
  h2 + p           { color: crimson; }
  h2 ~ p           { text-decoration: underline; }
  input[type="email"] { border: 2px solid teal; }
  a[href^="https"] { color: green; }
</style>
<nav><ul class="menu">
  <li id="outer">Courses
    <ul><li id="sub"><a href="https://example.com" id="link">Python</a></li></ul>
  </li>
</ul></nav>
<h2>Notice</h2>
<p id="p1">First paragraph</p>
<p id="p2">Second paragraph</p>
<input type="email" id="mail">

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Descendant (space) matches at any depth; child (>) only direct children.
  • + matches the very next sibling; ~ matches all later siblings.
  • Attribute selectors can match exactly, by prefix, suffix or substring.
  • CSS siblings only look forward.
  • Prefer short class selectors to long descendant chains.

Common mistakes

  • expecting + to select an element that comes before. CSS siblings only look forward. The :has() selector is the tool for looking at earlier or surrounding elements.
  • using long descendant chains such as .page .content .list .item a. They are brittle and hard to override. Use a class on the element.

Follow-ups

  • how do you select a parent based on its child?
  • What is the difference between h2 + p and h2 ~ p?
  • How do you select every link that ends with .pdf?

Tips

  • Prefer the child combinator when you want to limit how far a style reaches. It keeps nested components from picking up each other's rules.
  • Run through one HTML snippet and say which selector matches which element. That is how this question is usually asked.
  • Mention :has() when asked about selecting a parent or earlier sibling.