If you look at how your SCSS actually gets compiled, you'll see it gets compiled to:
.my-class :nth-child(2) {
padding-right: 15px;
padding-left: 0;
}
Because CSS cascades (hence cascading style sheet), your :nth-child(2)
will affect every second child.
I changed the padding to the left side, so it can be seen.
.my-class :nth-child(2) {
padding-right: 0;
padding-left: 15px;
}
<div class="my-class">
<div>
first
<div>
first child
</div>
<div>
second child
<div>
first - second child
</div>
<div>
second - second child
</div>
</div>
</div>
<div>
second
</div>
</div>
If you want to stop this, you need to get specific on the what the :nth-child(2) should be applied to.
Like this:
.my-class > :nth-child(2) {
padding-right: 0;
padding-left: 15px;
}
<div class="my-class">
<div>
first
<div>
first child
</div>
<div>
second child
<div>
first - second child
</div>
<div>
second - second child
</div>
</div>
</div>
<div>
second
</div>
</div>
That essentially will only affect DIRECT children of .my-class
.
Here's the SCSS:
.my-class {
> :nth-child(2) {
padding-right: 15px;
padding-left: 0;
}}
Documentation for the direct child combinator >
https://developer.mozilla.org/en-US/docs/Web/CSS/Child_combinator