CSS :not pseudo-class not working

元气小坏坏 提交于 2019-11-29 14:32:25

Keep this in mind:

:not() is equivalent to *:not()

When there is no selector prefixing the :not() pseudo-class, a universal selector is implied:

6.2. Universal selector

If a universal selector represented by * (i.e. without a namespace prefix) is not the only component of a sequence of simple selectors selectors or is immediately followed by a pseudo-element, then the * may be omitted and the universal selector's presence implied.

Therefore, the rule you have:

:not(.mind) {
  color: red
}

... is saying apply red color to all elements except the element with the class mind.

Okay, except in this case, the color property is inheritable, so even though the red doesn't get applied to the .mind element, it still gets the red through inheritance from the .parent element.

Here's what the browser is doing:

A quick way to test this behavior is with the border property, which is not inheritable.

In the example below, using your selector, you'll notice that the border doesn't get applied to .mind, and your selector works as you were expecting:

:not(.mind) {
  color: red;
  border-bottom: 1px dashed black;
}
<div class='parent'>
  <div class='child'>One</div>
  <div class='child'>Two</div>
  <div class='child'>Three</div>
  <div class='child'>One</div>
  <div class='child'>Two</div>
  <div class='child'>Three</div>
  <div class='child'>One</div>
  <div class='mind'>mind</div>
  <div class='child'>Three</div>
  <div class='child'>
    <p>First paragraph</p>
  </div>
</div>

To your particular objective:

I want to color all text red except for .mind.

Use a more specific selector.

.parent > :not(.mind) {
  color: red;
}
<div class='parent'>
  <div class='child'>One</div>
  <div class='child'>Two</div>
  <div class='child'>Three</div>
  <div class='child'>One</div>
  <div class='child'>Two</div>
  <div class='child'>Three</div>
  <div class='child'>One</div>
  <div class='mind'>mind</div>
  <div class='child'>Three</div>
  <div class='child'>
    <p>First paragraph</p>
  </div>
</div>

your selector works fine, because :not(.mind) is the same as *:not(.mind) - which is a universal selector - but because .mind is child of .parent it will get color:red inherited from .parent, and that's why you see it colored red.

To simplify, in this case no need to use :not() just use .child

.child {
  color: red
}
<div class='parent'>
  <div class='child'>One</div>
  <div class='child'>Two</div>
  <div class='child'>Three</div>
  <div class='child'>One</div>
  <div class='child'>Two</div>
  <div class='child'>Three</div>
  <div class='child'>One</div>
  <div class='mind'>mind</div>
  <div class='child'>Three</div>
  <div class='child'>
    <p>First paragraph</p>
  </div>
</div>

However if you want the .mind div to have also .child class, in that case, you can use :not() like this:

.child:not(.mind) {rules}

Snippet

.child:not(.mind) {
  color: red
}
<div class='parent'>
  <div class='child'>One</div>
  <div class='child'>Two</div>
  <div class='child'>Three</div>
  <div class='child'>One</div>
  <div class='child'>Two</div>
  <div class='child'>Three</div>
  <div class='child'>One</div>
  <div class='child mind'>mind</div>
  <div class='child'>Three</div>
  <div class='child'>
    <p>First paragraph</p>
  </div>
</div>

It's targeting the parent selector, and the child (.mind) is inheriting it. The code works just fine if .mind doesn't inherit a color from the parent.

div:not(.mind) {
  color: red;
}
		<div class='child'>One</div>
		<div class='child'>Two</div>
		<div class='child'>Three</div>
		<div class='child'>One</div>
		<div class='child'>Two</div>
		<div class='child'>Three</div>
		<div class='child'>One</div>
		<div class='mind'>mind</div>
		<div class='child'>Three</div>
		<div class='child'><p>First paragraph</p></div>

You'll need to define rules for .mind specifically in this case.

You can use :not As follows :

.parent :not(.mind){
    color:red;
}

Code :

.parent :not(.mind){
	color:red;
}
<div class='parent'>
	<div class='child'>One</div>
	<div class='child'>Two</div>
	<div class='child'>Three</div>
	<div class='child'>One</div>
	<div class='child'>Two</div>
	<div class='child'>Three</div>
	<div class='child'>One</div>
			  
	<div class='mind'>mind</div>
			  
	<div class='child'>Three</div>
	<div class='child'>
		<p>First paragraph</p>
	</div>
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!