I have this HTML:
<div class="holder">
<span class="holder__title">TITLE ONE</span>
</div>
<div class="holder">
<span class="holder__title">TITLE TWO</span>
</div>
<div class="holder">
<span class="holder__title">TITLE THREE</span>
</div>
Now, I want to modify TITLE TWO and TITLE THREE spans only and leave the first as it is, but I cannot get it to work. This is what I have tried:
.holder {
&:not(:first-child) {
&__title {
display:none; // just to test
}
}
}
and
.holder {
&:nth-child(n+2):nth-child(-n+3) {
&__title {
display:none; // just to test
}
}
}
It works ok in developer tools, but when I enter it in .scss file and compile nothings happens. Like selectors do not even get targeted.
How can I resolve this please?
Thank you.
& translates into existing selector at this exact point. Which means that this
.holder {
&:nth-child(n+2):nth-child(-n+3) {
...some-rule...
&__title {
...other-rule...
}
}
}
translates into this CSS:
.holder:nth-child(n+2):nth-child(-n+3) {
...some-rule...
}
.holder:nth-child(n+2):nth-child(-n+3)__title {
...other-rule...
}
If you're really keen on doing it properly, you should put .holder inside a variable, which doesn't break BEM (you're able to change it from only one place):
$name: '.holder';
#{$name} {
&:nth-child(n+2):nth-child(-n+3) {
...some-rule...
#{$name}__title {
...other-rule...
}
}
which translates into this:
.holder:nth-child(n+2):nth-child(-n+3) {
...some-rule...
}
.holder:nth-child(n+2):nth-child .holder__title {
...other-rule...
}
What you are trying to write is invalid SCSS. Remember, the & always refers to the parent selector when nesting.
So your SCSS
.holder {
&:not(:first-child) {
&__title {
display:none; // just to test
}
}
}
will translate to this CSS which is invalid:
.holder:not(:first-child) .holder:not(:first-child)__title {
display:none; // just to test
}
A solution would be:
.holder {
&:not(:first-child) {
.holder__title {
display:none;
}
}
}
Even though, this will break the BEM notation. Still, i will leave this here in case no better answer shows up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With