Combinators
Published Jun 9, 2022Updated Jun 10, 2022
Contribute to Docs
Combinators are CSS selectors that are used to style elements that have a certain relationship with other selected elements.
Syntax
selector1 selector2 ... selectorN {
/* declarations here */
}
selector1 > selector2 ... > selectorN {
/* declarations here */
}
selector1 + selector2 ... + selectorN {
/* declarations here */
}
selector1 ~ selector2 ... ~ selectorN {
/* declarations here */
}
- The space selector (
- The child selector (
>
) matches all children of a specified element. - The adjacent sibling selector (
+
) matches all elements directly after the preceding element. - The general sibling selector (
~
) matches all elements that are next to the preceding element.
Example
The example below shows how these combinators can be used:
div p {background-color: blue;color: #fff;}div > p {background-color: blue;color: #fff;}h3 + p {background-color: blue;color: #fff;}h3 ~ p {background-color: blue;color: #fff;}
The rendered output may look like this:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn CSS on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn CSS
In this CSS tutorial, you’ll learn how to add CSS to visually transform HTML into eye-catching sites.Beginner Friendly6 hours