1

I want to color all my p in blue, except the one inside the no-color div.

I tried p:not(.no-color), :not(.no-color) p, div:not(.no-color) p but I think I misunderstand something

p:not(.no-color) {
  color: blue;
}
<div class="container">
  <p>Lorem ipsum</p>
  <div class="no-color"><p>Lorem ipsum</p></div>
  <p>Lorem ipsum</p>
  <div class="random-class"><p>Lorem ipsum</p></div>
</div>

Edit: The HTML code is retrieved automatically, so I can't choose on which elements I apply the classes. I can only stylize in CSS.

10
  • 1
    Can't do that with CSS only, as it would take a parent selector, which doesn't exists (yet) Commented Feb 9, 2019 at 18:31
  • You can try this jsfiddle.net/0zLonhjc .no-color p {..} p{..} Commented Feb 9, 2019 at 18:33
  • "." before an item specifies a css class for that object, try .no-color p{color: blue} Commented Feb 9, 2019 at 18:33
  • If the parent element is known, e.g. they all where a child of the body, this would work though: body > p { color: blue; } Commented Feb 9, 2019 at 18:35
  • @LGSon I don't understand what you mean by parent selector ? I edited my code, is that possible now ? Commented Feb 9, 2019 at 18:45

3 Answers 3

2

You can use something like this if you can't modify the HTML :

.container > p,
.container > div:not(.no-color) > p {
  color: blue;
}
<div class="container">
  <p>Lorem ipsum</p>
  <div class="no-color">
    <p>Lorem ipsum</p>
  </div>
  <p>Lorem ipsum</p>
  <div class="random-class">
    <p>Lorem ipsum</p>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

4 Comments

Yes I know, the problem is that I have stylized blockquote (with more than 20 CSS lines), so I have to cancel the 20 CSS properties for the no-color class. I was wondering if there was an easier way with the :not selector
Unfortunaltely as stated by @LGSon this is not possible if you don't apply it from a known parent (I used body here but you can use a closer one of course), because there is no parent selector in CSS.
@Yann39 Now you can edit your answer, as OP added some more code that will work with your logic.
@LGSon Thanks :)
1

This selector should work, without modifying the HTML:

:not(.no-color) > p {
    color: blue;
}
<div class="container">
  <p>Lorem ipsum</p>
  <div class="no-color"><p>Lorem ipsum</p></div>
  <p>Lorem ipsum</p>
  <div class="random-class"><p>Lorem ipsum</p></div>
</div>

(Sorry for my previous, unhelpful answers... I actually tested this one!)

EDIT: Fixed answer

5 Comments

The markup can't be changed, which make this answer wrong.
Please see my edited answer, with an additional solution.
Always try what you suggest before posting it, and if you would, you would see this won't work
@LGSon You are absolutely right! I did test this latest edit.
This indeed works with new OP requirement, but note that this will not work if you have other enclosing elements arround your p (i.e. <div class="no-color"><div><p>Lorem ipsum</p></div></div>)
0

I would place the class on the <p> instead, then p:not(.no-color) would work.

If however, you can't change the HTML structure, you can target <p>s which are descendants of an element with a .no-color class by using the .no-color p selector and then set the color to inherit.

Setting the color to inherit allows you to get the color of the enclosing parent without specifying it.

This technique works for arbitrarily nested <p> elements below a .no-color parent.

p {
  color: blue;
}
.no-color p {
  color: inherit;
}
<p>Lorem ipsum</p>
<div class="no-color">
  <p>Lorem ipsum</p>
</div>
<p>Lorem ipsum</p>
<div class="no-color">
  <div>
    <p>Lorem ipsum</p>
  </div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.