I have a simple html hierarchy:
<div>
<div>
<div class="custom-class">whatever</div>
</div>
<div>
<div class="custom-class">whatever</div>
</div>
<div>
<div class="custom-class">thing i want to select</div>
</div>
</div>
I am currently adding attributes to .custom-class
but would like to add a specific attribute to only the last-child’s .custom-class
.
However I would like to apply the style inside the .custom-class
that is being used for all children. Its parent does not have a custom class, nor does the grandparent.
A pseudo example would be:
.custom-class {
color: red;
&.where-parent-is-last-child {
color: green;
}
}
Meaning, I would like to know the scss
selector that can access <div class="custom-class">thing I want to select</div>
without affecting the other siblings INSIDE the singular .custom-class
class.
2
Answers
:last-child > .custom-class
should do what you are asking for: This selectscustom-class
if it is a direct child of an element that is a last-child.This applies the
green
color to.custom-class
only when it’s the child of a<div>
that is the:last-child
in its container.Result
Here is the compiled CSS with the accompanying HTML in a snippet: