Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS :not() selector on all descendants [duplicate]

How can I make it happen that the :not()-selector works in the following example?

HTML

<div id="content">
  <div id="one">
    <div><p>This is a <a href="">link</a>.</p></div> <!-- should be green -->
    <div><p>This is a <a href="">link</a>.</p></div> <!-- should be green -->
</div>
  <div id="two">
    <div class="mystyle"><p>This is a <a href="">link</a>.</p></div> <!-- should NOT be green -->
    <div><p>This is a <a href="">link</a>.</p></div> <!-- should be green -->
</div>
</div>

CSS

#content a:not(.mystyle) {
  color: green;
}

JSFIDDLE

Link to jsfiddle

Important

I am trying to style an external widget on my blog. I don't have access to it's HTML so changing the HTML is not an option. I am looking for an CSS-only solution.

like image 733
Sr. Schneider Avatar asked Mar 01 '17 18:03

Sr. Schneider


2 Answers

In your example, the :not() selector is applied to the a element. This would only select a tags that did not have a .mystyle class on it.

#content > * > *:not(.mystyle) a {
    color: green;
}

The above will select any descendants 2 levels down that don't have a .mystyle class, then colour all their decendant a tags green.

Below is a working example:

#content > div > div:not(.mystyle) a {
  color: green;
}
<div id="content">
  <div id="one">
    <div><p>This is a <a href="">link</a>.</p></div> <!-- should be green -->
    <div><p>This is a <a href="">link</a>.</p></div> <!-- should be green -->
</div>
  <div id="two">
    <div class="mystyle"><p>This is a <a href="">link</a>.</p></div> <!-- should NOT be green -->
    <div><p>This is a <a href="">link</a>.</p></div> <!-- should be green -->
</div>
</div>
like image 112
Soviut Avatar answered Sep 20 '22 18:09

Soviut


There are four links. All links should be green with one exception: the link in the class="mystyle".

Then why are you using :not()? Just give the color to the existing class.

* { color: green; }

.mystyle * { color: red; }
<div id="content">
  <div id="one">
    <div><p>This is a <a href="">link</a>.</p></div> <!-- should be green -->
    <div><p>This is a <a href="">link</a>.</p></div> <!-- should be green -->
</div>
  <div id="two">
    <div class="mystyle"><p>This is a <a href="">link</a>.</p></div> <!-- should NOT be green -->
    <div><p>This is a <a href="">link</a>.</p></div> <!-- should be green -->
</div>
</div>
like image 43
Michael Benjamin Avatar answered Sep 17 '22 18:09

Michael Benjamin