You're having an XY problem. You need to override the color of a
, but instead of asking for an optimal way to do that, you invent some weird construction and ask why it is not working.
The matter is that you don't need to put a
inside .foo
to override the color. An a
gets it's color not from .foo
directly, but from a
's own color
property.
By default color
is set to inherit
, which naturally inherits from parent. So to override a
's color, you change it's color
property from inherit
to whatever you want. To do that, you don't need to put parent into the selector!
UPD: As cimmanon has corrected me below, the color of a
is not inherited from parent, it uses its own default value instead.
So an optimal solution would be this:
.foo {
color: blue; }
a {
color: red;
.foo.bar & {
color: green; } }
Also, the fact that you want to override exactly for .foo.bar a
indicates that you're overcomplicating things.
You only need .foo.bar a
if you need to handle four different situations: a
, .foo a
, .bar a
and .foo.bar a
. Do you really have that much different colors for the links? Why?
Most certainly you could refactor your HTML structure, make it more semantic and be happy with code like this:
a {
color: red; }
#header {
color: blue; }
.cart-item
& a {
color: green; } }
With this code, the link can only be two different colors: green for cart items and red for everything else.
If you do have a good reason to have many colors, consider using a mixin:
@mixin link-with-color($color: red) {
a {
color: $color; } }
#header {
@include link-with-color(blue); }
.cart-item {
@include link-with-color(green); }