不是元素类型的子元素的 CSS 选择器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14900763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:40:45  来源:igfitidea点击:

CSS Selector for not a child of element type?

csscss-selectors

提问by bevacqua

I want to style codeelements that are not inside atags.

我想为code不在a标签内的元素设置样式。

What is the best approach to accomplish this?

实现这一目标的最佳方法是什么?

code:not(a code)doesn't seem to work at all, at least on Chrome, even though it seems like it should

code:not(a code)似乎根本不起作用,至少在 Chrome 上,即使它看起来应该

I can't get it to work from the console either.

我也无法从控制台运行它。

Are there any other css-only approaches I could use for this?

我可以使用其他任何仅 css 的方法吗?

回答by Joseph Silber

:notdoes not support combinator selectors.

:not不支持组合选择器。

If we're talking about its direct parent:

如果我们谈论它的直接父母:

:not(a) > code

Otherwise there's no way to do this in CSS. You'll have to override it:

否则没有办法在 CSS 中做到这一点。你必须覆盖它:

code {
    /* some styles */
}

a code {
    /* override previous styles */
}