There are two classes .float-left
& .float-right
given to each two div
. Now I am trying to apply first-child
and last-child
CSS selector properties to these two classes. Child selector for float-left
class works fine, but not with the float-right
class.
.float-left:first-child {background: yellow;}
.float-left:last-child {background: green;}
.float-right:first-child{background: blue;}
.float-right:last-child{background: red;}
<div>
<div class="float-left">Left1</div>
<div class="float-right">Right1</div>
</div>
</br>
<div>
<div class="float-right">Right2</div>
<div class="float-left">Left2</div>
</div>
You can see in above snippet result that the first div
of class float-right
having content Right1 has red background, while CSS property for first child of float-right
class is background:blue;
. Same issue is seen for the last-child
.
I understand from the discussion in comments and the answer for
CSS selector for first element with class that this is how the first-child
and last-child
selectors work but I need a way to select the last element across multiple parents. JavaScript/jQuery solutions are also welcome.
The scenario described above is a just a minimal example but my actual scenario is as follows:
Suppose I have 50 div's with class float-right
and all of them are individuals in different parent elements, also they are not in the even-odd pattern. Then what would be my approach to select the last (50th) div with class float-right
?