Combinators in Cascading Style Sheets

Combinator

A combinator provides relationships among selectors that can be explained. A CSS selector can have more than one simple selector and the combination of those selectors with some relationships (like div + p, div ~ ul, and so on.) is known as a combinator.

Note

Selectors are the rule set that are used to select the HTML content on HTML pages so that they can be styled according to us. For the details follow the link given below.

Selectors in CSS

There are four types of combinators that are as follows:

types of css combinator

Descendant Combinator

The descendant combinator allows you to combine two or more selectors so that you can be more specific in your selection method.

In other words, a descendant combinator matches all the elements that are descendant of the specified element.

Now let's have a look at the example given below. 

  1. <html>  
  2. <head>  
  3. <title>Descendant combinator</title>  
  4. <style>  
  5. div p{color:green;  
  6.           font-family:arial black;  
  7.           text-align: center;}  
  8.   
  9. div ul{list-style-type: circle;  
  10.            border: solid 1px #ccc;  
  11.            color: red;}  
  12. </style>  
  13. </head>  
  14. <body>  
  15. <div><p>This paragraph is descendant of specified element i.e. div</p>  
  16. <p>This paragraph is also descendant of specified element i.e. div</p>  
  17. </div>  
  18. <h5>Hello desendants..!!</h5>  
  19. <h1>This is Descendant combinator</h1>  
  20. <div>  
  21. <ul>  
  22.   <li>Animals</li>  
  23.   <li>Fruits</li>  
  24.   <li>Electronics</li>  
  25. </ul>  
  26. </div>  
  27. <ol>  
  28.   <li>Boy</li>  
  29.   <li>Girl</li>  
  30. </ol>  
  31. </body>  
  32. </html>  
In the preceding example there is only the selection of <p> and <ul> elements that are inside the <div> elements and the rest of all ot them are not in any style.

 Output

output of descendant combinator

In the preceding output you can see that the contents in the grey region are only affected because they are in a <div> element.

Child combinator

This combinator is similar to a descendant combinator but with the slight difference that it selects all the elements that are the immediate children of the specified element.

There is a slight change in syntax also that, instead of the space character between a “div” and a “p”, we are using a greater than symbol or (right angle bracket) in the case of a child combinator.

Example

  1. <html>  
  2. <head>  
  3. <title>Child combinator</title>  
  4. <style>  
  5. #content > .child{color:green;  
  6. font-family:arial black;  
  7. text-align: center;  
  8. background-color:lightgrey;}  
  9.   
  10. #list > .ulist{list-style-type: circle;  
  11. border: solid 1px #ccc;  
  12. color: red;  
  13. background-color:lightgrey;}  
  14. </style>  
  15. </head>  
  16. <body>  
  17. <div id="content">  
  18. <div class="child"><p>This paragraph is immediate child of specified element</p></div>  
  19. <div>  
  20. <div class="child"><p>This paragraph is not the immediate child</p></div>  
  21. </div>  
  22. </div>  
  23. <h5>Hello Children..!!</h5>  
  24. <h1>This is Child combinator</h1>  
  25. <div id="list">  
  26. <div class="ulist">  
  27. <ul>  
  28. <li>Animals</li>  
  29. <li>Fruits</li>  
  30. <li>Electronics</li>  
  31. </ul>  
  32. </div>  
  33. <div>  
  34. <div class="ulist">  
  35. <ul>  
  36. <li>Boy</li>  
  37. <li>Girl</li>  
  38. </ul>  
  39. </div>  
  40. </div>  
  41. </div>  
  42. </body>  
  43. </html>  
In the preceding example, the selector will match all elements that have a class of child and ulist and that are immediate children of the #content element. That means that, unlike the descendant combinator, there can't be another element wrapping .child or .ulist, it must be a direct child element. The style will be applied only to the first <div> element that has a class of child or ulist. As you can see, the second <div> element with a class of child or ulist is inside another <div> element. As a result, the styles will not apply to that element, even though it too has a class of the child or ulist.

 Output will make you clear.

Child combinator

Adjacent sibling combinator

In this combinator, the plus symbol (+) is used for declaration instead of the ">" symbol. The selector that selects all the elements that are adjacent siblings of the specified element is known as an adjacent sibling combinatory. Sibling elements must have the same parent element and "adjacent" means "immediately following".

Example

  1. <html>  
  2. <head>  
  3. <title>Adjacent sibling combinator</title>  
  4. <style>  
  5. div + p{color:green;  
  6. font-family:arial black;  
  7. text-align: center;  
  8. background-color:lightgrey;}  
  9.   
  10. div + ul{list-style-type: circle;  
  11. border: solid 1px #ccc;  
  12. color: red;  
  13. background-color:lightgrey;}  
  14. </style>  
  15. </head>  
  16. <body>  
  17. <div>  
  18. <p>This paragraph is not immediate child of specified element</p>  
  19. <p>Not even this paragraph is the immediate child</p>  
  20. </div>  
  21. <p>Now i am the immediate child</p>  
  22. <p>what about me..???</p>  
  23. <h5>Hello adjacent siblings..!!</h5>  
  24. <h1>This is Adjacent sibling combinator</h1>  
  25. <div>  
  26. <ul>  
  27. <li>Animals</li>  
  28. <li>Fruits</li>  
  29. <li>Electronics</li>  
  30. </ul>  
  31. </div>  
  32. <ul>  
  33. <li>Boy</li>  
  34. <li>Girl</li>  
  35. </ul>  
  36. </body>  
  37. </html>  
In the preceding example the style will be applied only to adjacent siblings, in other words the paragraph “Now I am the immediate child” comes just after the <div> element. Because “div + p” means sibling that is immediately following after the <div> element. Not even the paragraph comes after the adjacent sibling will be affected.

Here's the output that will make it clear.

Adjacent sibling combinator

General sibling combinator

The general sibling combinator is declared by the symbol (~) which selects all the elements that are the siblings of specified element.

Example

  1. <html>  
  2. <head>  
  3. <title>General sibling combinator</title>  
  4. <style>  
  5. div ~ p{color:green;  
  6. font-family:arial black;  
  7. text-align: center;  
  8. background-color:lightgrey;  
  9. }  
  10. div ~ ul{list-style-type: circle;  
  11. border: solid 1px #ccc;  
  12. color: red;  
  13. background-color:lightgrey;}  
  14. </style>  
  15. </head>  
  16. <body>  
  17. <div>  
  18. <p>I am not the general sibling</p>  
  19. <p>Even not me..</p>  
  20. <p>Because we are not in "div"</p>  
  21. </div>  
  22. <p>This paragraph is general sibling of specified element</p>  
  23. <p>This paragraph is also</p>  
  24. <p>Me too..!!</p>  
  25. <h5>Hello general siblings..!!</h5>  
  26. <h1>This is General sibling combinator</h1>  
  27. <div>  
  28. <ul>  
  29. <li>Animals</li>  
  30. <li>Fruits</li>  
  31. <li>Electronics</li>  
  32. </ul>  
  33. </div>  
  34. <ul>  
  35. <li>Boy</li>  
  36. <li>Girl</li>  
  37. </ul>  
  38. </body>  
  39. </html>  
In the preceding example, those lists are, or that paragraph is, not affected that are in the <div> element because “div ~ p” or “div ~ ul” means all the elements just after the <div> element will be styled.

 Output:

general sibling combinator

Note: There could be other elements between a <div> and a <p> and the style would still apply. What you need to do is that you just make a group of selectors that are added between <div> and <p> separating each selector with a comma. Just look at the example given below.

Example

  1. <html>  
  2. <head>  
  3. <title>General sibling combinator</title>  
  4. <style>  
  5. div ~ p,h1,h5{color:green;  
  6. font-family:arial black;  
  7. text-align: center;  
  8. background-color:lightgrey;}  
  9. </style>  
  10. </head>  
  11. <body>  
  12. <div>  
  13. <p>I am not the general sibling</p>  
  14. <p>Even not me..</p>  
  15. <p>Because we are not in "div"</p>  
  16. </div>  
  17. <p>This paragraph is general sibling of specified element</p>  
  18. <h5>Hello general siblings..!!</h5>  
  19. <p>This paragraph is also</p>  
  20. <h1>This is General sibling combinator</h1>  
  21. <p>Me too..!!</p>  
  22. </body>  
  23. </html>  
Output
 
output 

Up Next
    Ebook Download
    View all
    Learn
    View all