CSS 否定伪类 :not() 用于父/祖先元素 [英] CSS negation pseudo-class :not() for parent/ancestor elements

查看:50
本文介绍了CSS 否定伪类 :not() 用于父/祖先元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我发疯:

HTML:

Hello World!

CSS:

*:not(div) h1 { color: #900;}

这不是说选择所有具有不是 div 元素的祖先的 h1 元素......?"因此,你好世界!"不应该是红色,但它仍然是.

对于上述标记,添加子组合器有效:

*:not(div) >h1 { 颜色:#900;}

但不影响 h1 元素,如果它不是 div 元素的子元素.例如:

Hello World!

这就是为什么我想将 h1 元素表示为 div 元素的后代,而不是子元素.有人吗?

解决方案

这不是说,选择所有具有不是 div 元素的祖先的 h1 元素......?"

确实如此.但是在典型的 HTML 文档中,每个 h1 至少有两个不是 div 元素的祖先——而那些祖先正是 bodyhtml.

这是尝试使用 :not() 过滤祖先的问题:它只是不能可靠地工作,尤其是当 :not() 不是由一些其他选择器(例如类型选择器或类选择器)限定,例如.foo:not(div).只需将样式应用于所有 h1 元素并使用 div h1 覆盖它们,您就会轻松得多.

选择器 4 中,:not() 已得到增强,以接受包含组合器的完整复杂选择器,包括后代组合器.这是否会在快速配置文件(以及 CSS)中实现还有待测试和确认,但一旦实现,那么您将能够使用它来排除具有某些祖先的元素.由于选择器的工作方式,为了可靠地工作,必须对元素本身而不是祖先进行否定,因此语法看起来有点不同:

h1:not(div h1) { color: #900;}

任何熟悉 jQuery 的人都会很快指出这个选择器现在可以在 jQuery 中使用了.这是 选择器 3 之间的许多差异之一code>:not() 和 jQuery 的 :not(),这是 Selectors 4 试图纠正的.

This is driving me nuts:

HTML:

<div><h1>Hello World!</h1></div>

CSS:

*:not(div) h1 { color: #900; }

Doesn't this read, "Select all h1 elements that have an ancestor that is not a div element...?" Thus, "Hello World!" should not be coloured red, yet it still is.

For the above markup, adding the child combinator works:

*:not(div) > h1 { color: #900; }

But doesn't affect the h1 element if it is not a child of a div element. For example:

<div><article><h1>Hello World!</h1></article></div>

Which is why I'd like to indicate the h1 element as a descendant, not a child, of the div element. Anyone?

解决方案

Doesn't this read, "Select all h1 elements that have an ancestor that is not a div element...?"

It does. But in a typical HTML document, every h1 has at least two ancestors that are not div elements — and those ancestors are none other than body and html.

This is the problem with trying to filter ancestors using :not(): it just doesn't work reliably, especially when the :not() is not being qualified by some other selector such as a type selector or a class selector, e.g. .foo:not(div). You'll have a much easier time simply applying styles to all h1 elements and overriding them with div h1.

In Selectors 4, :not() has been enhanced to accept full complex selectors containing combinators, including the descendant combinator. Whether this will be implemented in the fast profile (and thus CSS) remains to be tested and confirmed, but once it is implemented, then you will be able to use it to exclude elements with certain ancestors. Due to how selectors work, the negation has to be done on the element itself and not the ancestor in order to work reliably, and therefore the syntax will look a little different:

h1:not(div h1) { color: #900; }

Anyone who's familiar with jQuery will quickly point out that this selector works in jQuery today. This is one of a number of disparities between Selector 3's :not() and jQuery's :not(), which Selectors 4 seeks to rectify.

这篇关于CSS 否定伪类 :not() 用于父/祖先元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆