Hey there π
In this article, we will discuss CSS combinators. Which are a type of CSS selectors.
My Previous Articles π
This is the third part of my CSS selectors series if you do not already know the basics of CSS selectors. Be sure to check them out before reading this article.
What are combinators
Combinators are CSS selectors that select elements based on the relationship between the selectors of two elements.
Combinators are applied within two selectors, and if the relationship between the first and second element is as defined by the combinator, then the second element is styled.
Types of combinators
There are four types of CSS selectors
- 
Descendant Selector (" ")Syntax: Element DescendantDescendant selectors select elements that are children, grandchildren, great-grandchildren, and so on, of the selected elements. An element is selected if it can be found anywhere within the parent element. Even if it is nested in a different element. It is represented by a single space (" ") between the 2 selectors. Example: 
 div p{ color: blue; }This example changes the text color of pelements that can be found inside adivelement.
 <h1>This is a header</h1> <p>This is outside a div element</p> <div> <h2>This is a header</h2> <p>This is inside a div element</p> <ul> <li>This is a list item</li> <p>This is a p tag inside an element that is inside a div element</p> <li>This is a list item</li> <p>This is a p tag inside an element that is inside a div element</p> </ul> </div> <p>This is outside a div element</p>Result: The results will be displayed in a codepen towards the end of the article. Even pelements that are inside theulelement are also selected. This is because theulelement is a child of thedivelement and that makes thepelements within thedivelements grandchildren of thedivelement, hence they are also selected.
- 
Child Selector (">")Syntax: Element > ChildChild selectors or direct descendants select only the direct children of an element. If the element is nested within another element, it is not selected. It is represented with a right-angle bracket >between the two elements.Example: 
 div > p{ color: red; }This example changes the text color of only the pelements that are children ofdivelements.
 <h1>This is a header</h1> <p>This is outside a div element</p> <div> <h2>This is a header</h2> <p>This is inside a div element</p> <ul> <li>This is a list item</li> <p>This is a p tag inside an element that is inside a div element</p> <li>This is a list item</li> <p>This is a p tag inside an element that is inside a div element</p> </ul> </div> <p>This is outside a div element</p>Result: The results will be displayed in a codepen towards the end of the article 
 Onlypelements that are children ofdivelements are selected, and thepelements that are inside theulare not affected.
- 
Adjacent Sibling Selector ("+")Syntax: Element + siblingAdjacent sibling selectors select adjacent siblings, that is elements that share the same parent and come immediately after the element. If there is an element of a different type between the element and the sibling, then the sibling is not selected. It is represented with a plus sign +between the two elements.
 Example:
 h1 + p{ border: 2px solid black }This example adds a border around all pelements that come immediately after anh1element.
 <h1>This is a header</h1> <p>This paragraph comes after a header</p> <p>This is a paragraph</p> <h1>This is a header</h1> <span>This is a span</span> <p>This is a paragraph</p> <div> <h1>This is a header</h1> <p>This paragraph comes before a header</p> <h2>This is a header</h2> </div>Result: The results will be displayed in a codepen towards the end of the article. 
 Only thepelements that come after anh1element are styled, and all otherpelements are omitted from the styling.
- 
General sibling selector ("~")Syntax: Element ~ siblingGeneral sibling selectors select all elements that come after a particular element. If there is an element of a different tag, between the 2 elements, it still selects them. It is represented by the tilde sign ~,(the button right under theEsckey on your keyboard, pressing shift + the key will give you the tilde sign).Example: 
 .limit ~ li{
 text-decoration: line-through;
 }This example adds a line through all lielements that come after the element with the class of "limit".
 <ul> <li>List item 1</li> <li>List item 2</li> <li class="limit">List item 3</li> <li>List item 4</li> <li>List item 5</li> <ul> <li>Nested item 1</li> <li>Nested item 2</li> </ul> <li>List item 6</li> <li>List item 7</li> </ul>Result: The results will be displayed in a codepen towards the end of the article.All elements after the element with a class of "limit" receive the styling, even those that are separated by a nested list. 
Conclusion π
CSS combinators select elements indirectly, so you should be careful when using them.
They can also be used with any type of selector(simple or attribute)
  
  
  Note that the comma , is not a combinator
The comma is used to separate selectors in a selectors list
Here is a codepen where the effects of the different combinators are shown.
This is the pen containing all the results of the above code examples Results by Joseph Taiwo (@teejay128) on CodePen.
Also check out MDN web docs and w3schools for further knowledge on CSS combinators
Although I did my research, I may still be missing out on some things since I decided to make this article a short one, feel free to drop a comment on anything, be it a correction or something I missed out.
 
 
               
    
Top comments (0)