I have recently started learning development, and I am practicing HTML & CSS by following tutorials on YouTube.
In one of the tutorial videos, the instructor explained advanced selectors, including adjacent and general sibling combinators.
For example: h2 + a {}, h2 ~ p {}
When I tried these selectors as demonstrated, they worked. However, when I attempted to use them on ul + li, tr ~ td, it did not work as expected.
Can someone please explain why this is?
h2 ~ p {
color: red;
}
ul + li {
color: purple;
}
<h2 class="subtitle">About Me</h2>
<img src="../img/img-1.jpg">
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
<span>Lorem ipsum dolor sit amet</span>, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.
Cillum dolore eu fugiat nulla , pariatur:
<ul><!--parent-->
<li>Excepteur sint occaecat</li><!--first child-->
<li>My numbered list item</li><!--second child-->
<li>Nulla excepteur irure</li><!--third child-->
</ul>
</p>
h2 + a {
color: red;
}
tr ~ td {
color:green;
}
<h2 class="subtitle" id="subtitle-id">Services</h2>
<a href="http://www.google.com" id="google-link">Click here to go to Google</a>
<table border="1">
<thead>
<tr>
<td>Lorem</td>
<td>Ipsum</td>
<td>Dolor</td>
</tr>
</thead>
<tbody>
<tr>
<td>Sit</td>
<td>Amet</td>
<td>Consectetur</td>
</tr>
<tr>
<td>Adipisicing</td>
<td>Elit</td>
<td>Sed</td>
</tr>
</tbody>
</table>