Loading...
Published on September 24, 2025
CSS Combinators
When you’re starting out with CSS, it’s easy to stick with the most simple of selectors like classes (.class) or IDs (#ID). But as your projects grow, you’ll often need more precise ways to target elements without using individual classes or IDs. That’s where combinator selectors will save you.
Combinators define relationships between elements in your HTML. Instead of targeting just one element, you can select elements based on how they relate to each other in the document structure.
Now, let's take a look at the most common of CSS combinator selectors.
1. Descendant Selector ( ) The descendant selector is represented by a single space. It targets an element that is nested anywhere inside another element, no matter how deep.
parent descendant { /* styles here */
}div p {
color: blue;
}
This rule makes all <p> elements inside a <div> turn blue — even if the <p> is several levels deep. The descendant selector is good for when you want to apply broad styles to all children of a container.
2. Child selector (>) The child selector is represented by the > symbol (Shift + .). This selector is more specific than the previous selector, It only selects elements that are direct children of a parent. It does not select elements any deeper than the direct children.
parent > child {
/* styles here */
}
div > p { color: green; }
This only styles <p> elements that are immediate children of a <div>.
If a <p> is nested inside another (HTML) tag within the <div>, it won’t be affected. The child selector is good for when you want styles to target only direct child elements and not any elements that are deeper.
3. Adjacent Sibling Selector (+) The adjacent sibling is represented by the + symbol (Shift + =). This selector targets an element that comes immediately after another element, at the same level.
element + element {
/* styles here */
}
h2 + p {
font-style: italic;
}
The Adjacent Sibling Selector is good for styling elements that follow right after a heading, image, or any other (HTML) element.
4. General Sibling Selector (~) Finally, the general sibling selector, represented by the ~ symbol (Shift + `), is like the adjacent siblingbut broader. It selects all siblings that come after a given element, not just the first.
element ~ element {
/* styles here */
}
h2 ~ p { color: gray; }
The General Sibling Selector is good for styling multiple related elements after one element.
In short, combinator selectors give you a powerful way to style elements based on their relationships or position in the HTML structure. Instead of adding classes to every element, you can use these selectors to target elements more naturally, keeping your CSS cleaner and easier to maintain. Whether you need to style deeply nested elements, direct children, or siblings that appear next to each other, these four selectors cover the most common scenarios. By using them you’ll gain more control over your layouts and write more efficient CSS.