So I have a box:
<div class="box">Box</div>
And sometimes this box may be wrapped in an a tag:
<a href="#"><div class="box">Box in an A tag</div></a>
Is there a way in LESS to specifically target A tags that are parents of .box?
e.g.
a {
   color: red;
}
a [that is a parent of .box] {
   color: blue;
}
                No, you cannot select ancestor elements using LESS or CSS, but LESS has what is called the parent selector, or &.
The & operator represents the parent selectors of a nested rule and is most commonly used when applying a modifying class or pseudo-class to an existing selector
So, in your case, instead of changing the color of the parent, you could use & to target instances of .box that have an a parent, e.g.
.box {
  color: red;
  a & {
    color: blue;
  }
}
This translates to:
.box {
  color: red;
}
a .box {
  color: blue;
}
                        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