Combinators explain the relationship between two selectors, allowing precise targeting based on DOM hierarchy.
1. The 4 CSS Combinators
⊞Code Example
/* Descendant: all <p> inside .article */
.article p {
line-height: 1.8;
}
/* Child: only direct <li> children of .nav-menu */
.nav-menu > li {
display: inline-block;
}
/* Adjacent: <p> immediately after an <h2> */
h2 + p {
font-size: 1.2rem;
color: #04AA6D;
}
/* General Sibling: all <p> following <h2> */
h2 ~ p {
margin-bottom: 1rem;
}
