Introduction
This article will tackle the CSS combinator selectors (an advanced concept of CSS selectors). Usually, these advanced concepts are discussed in the last part of most books or video tutorials, such as child, adjacent sibling, and general sibling combinator, which is quite reasonable because you need to grasp many concepts. That's why the main objective of this post is to get you started with the advanced concepts.
However, if you're somewhere between a CSS newbie and intermediate and want to know the concepts of CSS combinator selectors quickly, you came to the right place. We'll try to explain everything simple as possible.
Lastly, knowing the basic selectors would help you before learning these concepts.
OK, let's get started.
CSS Combinator Selectors
Descendant Combinator
Before we start, let's try to review first the descendant selector.
When I started CSS back then, I was really confused with the term descendant, and if you are like me, no worries, we'll explain that here. OK, here it goes.
Descendant means another HTML element: a child, grandchild, great-grandchild, and so on. Take, for example, the following code below.
<div class="employees">
<h3>List Of Employees</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec fringilla magna.</p>
<table>
<thead>
<tr>
<th>Employee Id</th>
<th>Fullname</th>
<th>Department</th>
</tr>
</thead>
<tbody>
<tr>
<td>EMP101</td>
<td>Necesario, Jin Vincent</td>
<td>IT Department</td>
</tr>
<tr>
<td>EMP102</td>
<td>Barkley, Kevin</td>
<td>Finance Department</td>
</tr>
<tr>
<td>EMP103</td>
<td>Man, Joana</td>
<td>Sales and Marketing Department<td>
</tr>
</tbody>
</table>
<p>Do you want to add a new employee? <button>Click Here</button></p>
</div>
Now, in our current example the <h3>,<p> (the first and the last one),<table> are child elements of <div>. While <thead>, <tbody> are child elements of <table> but also descendants of <div> (which in our case, grandchildren). Same goes with the <tr> (great-grand children) and <th> or <td> (great-great grand children).
Another thing is which is also true, <h3>,<p> (the first and the last one), table and all elements within the <table> have <div> as their common ancestor.
Let's try to show an example using the descendant selector.
div.employees h3 {
font-family: courier;
}
div.employees p{
color: rgb(128,0,0);
}
div.employees th,td {
padding: 10px;
text-align:left;
}
In our example, the first one, the <div> class of employees, sets the <h3> font family into a courier.
While the second one set the <p> to have a color of maroon.
Then the third one sets the <th> and <td> padding to 10 pixels and sets the text alignment to the left.
Output

Direct Child Combinator
In this section, we'll discuss the direct child combinator. One thing to remember when you see a greater than symbol (>) inside a CSS stylesheet this means it is using a direct child combinator selector.
Moreover, this operates much like descendant selectors, but it applies only to its immediate children of the element. Not unlike the descendant selector are more ambiguous because they apply to any descendant of an element; the descendant can be a grandchild or a great-grandchild or a great-great-grandchild, and so on.
Let's try to show an example using a direct child combinator selector.
<div class="content">
<h2>Submit <strong>Realiable</strong> Feedback</h2>
<p>Please be at least honest to your feedback.</p>
<form>
<div>
<label for="name">Name:</label>
<input type="text" id="name" name="name"/>
</div>
<div>
<label for="email">Email:</label>
<input type="email" id="email" name="email"/>
</div>
<div>
<label for="feedback">Feedback:</label>
<textarea id="feedback" name="feedback"></textarea>
</div>
<div id="button">
<button>Submit</button>
</div>
</form>
</div>
In the HTML above, there are a lot of immediate children of a certain element.




Bohdan StupakPosted Dec 15, 2021, 11:26 AM
Your articles span across multiple topics both backend and frontend. That is impressive!