CSS :not pseudo-class not working(CSS:不是伪类不起作用)
问题描述
我正在学习 :not()
伪类,但它没有按预期工作.
我想将除 .mind
之外的所有文本都涂成红色.出于某种原因,这并不能阻止 .mind
元素变红.
:not(.mind) {红色}
<div class='parent'><div class='child'>一个</div><div class='child'>两个</div><div class='child'>三个</div><div class='child'>一个</div><div class='child'>两个</div><div class='child'>三个</div><div class='child'>一个</div><div class='mind'>mind</div><div class='child'>三个</div><div 类='孩子'><p>第一段</p></div></div>
记住这一点:
:not()
等价于 *:not()
当 :not()
伪类前面没有选择器时,隐含一个通用选择器:
测试此行为的快速方法是使用不可继承的 border
属性.
在下面的示例中,使用您的选择器,您会注意到边框没有应用到 .mind
,并且您的选择器按预期工作:
:not(.mind) {红色;边框底部:1px 虚线黑色;}
<div class='parent'><div class='child'>一个</div><div class='child'>两个</div><div class='child'>三个</div><div class='child'>一个</div><div class='child'>两个</div><div class='child'>三个</div><div class='child'>一个</div><div class='mind'>mind</div><div class='child'>三个</div><div 类='孩子'><p>第一段</p></div></div>
为了您的特定目标:
<块引用>我想将除 .mind
之外的所有文本都涂成红色.
使用更具体的选择器.
.parent >:没关系) {红色;}
<div class='parent'><div class='child'>一个</div><div class='child'>两个</div><div class='child'>三个</div><div class='child'>一个</div><div class='child'>两个</div><div class='child'>三个</div><div class='child'>一个</div><div class='mind'>mind</div><div class='child'>三个</div><div 类='孩子'><p>第一段</p></div></div>
I am learning about the :not()
pseudo class and it is not working as expected.
I want to color all text red except for .mind
. For some reason this is not preventing the .mind
element from being red.
: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>
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>
这篇关于CSS:不是伪类不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CSS:不是伪类不起作用


基础教程推荐
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01