- Home
- /
- CSS Combinators
CSS Tutorial
Introduction
CSS Properties
CSS Designing
CSS Advance Topics
CSS FAQs
- Home
- /
- CSS Combinators
CSS Combinators
CSS combinators allow you to define relationships between HTML elements, helping you apply styles in a structured way. There are four main combinators that define how selectors interact.
Descendant Selector
Selects all elements inside a specified parent element, regardless of how deeply nested they are.
Syntax:
div p {
color: white;
background-color: teal;
}
Example:
Paragraph 1 inside div
Paragraph 2 inside section inside div
Paragraph 3 outside div
Both Paragraph 1 and 2 are styled because they are descendants of the div
.
Child Selector
Selects only direct children of a specified parent — not nested deeper.
Syntax:
div > p {
color: wheat;
background-color: rebeccapurple;
}
Example:
Paragraph 1 (direct child)
Paragraph 2 (nested inside section)
Only Paragraph 1 is styled, because it is a direct child of the div
.
Adjacent Sibling Selector
Selects the immediate sibling element that comes right after the specified element.
Syntax:
div + p {
color: white;
background-color: seagreen;
}
Example:
This is a div
Paragraph right after div
Another paragraph (won't be selected)
Only the first paragraph after the div gets styled.
General Sibling Selector
Selects all sibling elements of a certain type that come after the specified element.
Syntax:
div ~ p {
color: black;
background-color: lightgoldenrodyellow;
}
Example:
This is a div
Paragraph 1 (selected)
Paragraph 2 (selected)
Paragraph inside section (not selected)
All <p>
tags after the <div>
are styled — but only siblings, not deeply nested elements.