5

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.

Sr. Schneider
  • 647
  • 10
  • 20

4 Answers4

4

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>
Soviut
  • 88,194
  • 49
  • 192
  • 260
1

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>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
0

Like that? :

div:not(.mystyle) > p > 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>
Johannes
  • 64,305
  • 18
  • 73
  • 130
0

A way to do this you can check in the below snippet.

div:not(.calc) > p {
  color: green;
}
<div id="content">
<div><p>Hi</p></div>
<div ><div class="calc"><p>Hi</p></div></div>
<div><p>Hi</p></div>
</div>