Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Combinators in CSS?

CSS is a powerful language that allows us to apply various styles and layouts to our web pages. One of the most important aspects of CSS is selecting elements to apply styles to, and for that, we have CSS selectors. In this article, we'll explore a specific type of CSS selectors called combinators.

What are combinators?

Combinators are a type of CSS selector that allow us to target elements based on their relationships with other elements in the HTML structure. They help us to be more specific with our styling, and they make it easier to manage complex designs.

There are four main types of combinators:

  1. Descendant combinator (space)
  2. Child combinator (>)
  3. Adjacent sibling combinator (+)
  4. General sibling combinator (~)

Let's explore each of these combinators in detail, with code examples and intuitive explanations.

1. Descendant combinator (space)

The descendant combinator is represented by a space between two selectors. It targets all elements that match the second selector and are descendants (children, grandchildren, etc.) of an element that matches the first selector.

Here's an example:

<!DOCTYPE html>
<html>
<head>
<style>
  div p {
    color: red;
  }
</style>
</head>
<body>

<div>
  <p>This paragraph is inside a div and is red.</p>
  <section>
    <p>This paragraph is also inside a div (through the section) and is red.</p>
  </section>
</div>
<p>This paragraph is outside the div and is not red.</p>

</body>
</html>

In the example above, the CSS rule div p selects all p elements that are descendants of a div element. This means that both paragraphs inside the div will be red, while the paragraph outside of the div will remain unaffected.

You can think of the descendant combinator like a family tree. If you were to look for all descendants of a specific person, you would be looking for their children, grandchildren, and so on. The same concept applies to elements in the HTML structure.

2. Child combinator ( > )

The child combinator is represented by the > symbol between two selectors. It targets all elements that match the second selector and are direct children of an element that matches the first selector.

Here's an example:

<!DOCTYPE html>
<html>
<head>
<style>
  div > p {
    color: blue;
  }
</style>
</head>
<body>

<div>
  <p>This paragraph is a direct child of a div and is blue.</p>
  <section>
    <p>This paragraph is inside a div (through the section) but not a direct child, so it's not blue.</p>
  </section>
</div>
<p>This paragraph is outside the div and is not blue.</p>

</body>
</html>

In this example, the CSS rule div > p selects all p elements that are direct children of a div element. This means that only the first paragraph inside the div will be blue, while the second paragraph inside the div (which is a grandchild, not a direct child) and the paragraph outside of the div will remain unaffected.

Think of the child combinator like a parent-child relationship. In this case, we're only interested in the direct children, not the grandchildren or great-grandchildren.

3. Adjacent sibling combinator ( + )

The adjacent sibling combinator is represented by the + symbol between two selectors. It targets an element that matches the second selector and immediately follows an element that matches the first selector.

Here's an example:

<!DOCTYPE html>
<html>
<head>
<style>
  h2 + p {
    color: green;
  }
</style>
</head>
<body>

<h2>Heading</h2>
<p>This paragraph is immediately after the heading and is green.</p>
<p>This paragraph is not immediately after the heading and is not green.</p>

</body>
</html>

In this example, the CSS rule h2 + p selects the p element that immediately follows an h2 element. This means that only the first paragraph after the heading will be green, while the second paragraph will remain unaffected.

You can think of the adjacent sibling combinator like a line of people. In this case, we're looking for the person who is standing immediately next to a specific person.

4. General sibling combinator ( ~ )

The general sibling combinator is represented by the ~ symbol between two selectors. It targets all elements that match the second selector and follow an element that matches the first selector, regardless of whether they are immediately next to each other.

Here's an example:

<!DOCTYPE html>
<html>
<head>
<style>
  h2 ~ p {
    color: purple;
  }
</style>
</head>
<body>

<h2>Heading</h2>
<p>This paragraph is after the heading and is purple.</p>
<p>This paragraph is also after the heading and is purple.</p>

</body>
</html>

In this example, the CSS rule h2 ~ p selects all p elements that follow an h2 element. This means that both paragraphs after the heading will be purple.

Think of the general sibling combinator like a group of people who all share a common friend. In this case, we're looking for all the people who are friends with a specific person, regardless of the order in which they became friends.

Conclusion

Combinators are a powerful tool in CSS that allow us to target elements based on their relationships with other elements in the HTML structure. By understanding and using combinators effectively, we can create more specific and manageable styles for our web pages.

Remember the four main types of combinators:

  1. Descendant combinator (space) - targets all descendants of an element
  2. Child combinator (>) - targets direct children of an element
  3. Adjacent sibling combinator (+) - targets an element immediately following another element
  4. General sibling combinator (~) - targets all elements following another element

Now, with a solid understanding of combinators, you can take your CSS skills to the next level and create more complex and visually appealing web pages. Happy coding!