Why doesn't CSS nesting appended nesting selector work within pseudoselectors? - Stack Overflow

admin2025-03-19  1

Why is the first div red, but not the other? If you enable SCSS preprocessor in the pen, both divs are red as expected.

.regular {
  html & {
    color: red;
  }
}

.pseudo:before {
  content: '(pseudo) style me red';

  html & {
    color: red;
  }
}
<div class="regular">(regular) style me red</div>

<div class="pseudo"></div>

Why is the first div red, but not the other? If you enable SCSS preprocessor in the pen, both divs are red as expected.

https://codepen.io/aeharding/pen/wvVLBZg

.regular {
  html & {
    color: red;
  }
}

.pseudo:before {
  content: '(pseudo) style me red';

  html & {
    color: red;
  }
}
<div class="regular">(regular) style me red</div>

<div class="pseudo"></div>

Share Improve this question edited Nov 20, 2024 at 7:49 Temani Afif 274k28 gold badges365 silver badges485 bronze badges asked Nov 20, 2024 at 7:45 aehardingaeharding 5981 gold badge4 silver badges18 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

It's by design but it may change in the future: https://github/w3c/csswg-drafts/issues/7433

From the actual Specification:

The nesting selector cannot represent pseudo-elements

.regular {
  html & {
    color: red;
  }
}

.pseudo:before {
  content: '(pseudo) style me red';

  html & {
    color: red;
  }
}
<div class="regular">(regular) style me red</div>

<div class="pseudo"></div>

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1742373997a210334.html

最新回复(0)