21

我的 HTML 是这样的:

<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" href="stylesheet.css">
<head>
</head>
<body>
<p>hiya</p>
<h1>this is h1</h1>
<h2>this is h2</h2>
</body>
</html>

我的 stylsheet.css 是这样的:

:not(p)
{
color:#ff0000;
} 

然而一切都是红色的,包括<p>. 我尝试过 Firefox 20、Internet Explorer 10 和 Chrome。它似乎没有比这更基本的了,但我无法弄清楚为什么这不起作用(<p>即防止变红)。对此的任何帮助将不胜感激。

4

3 回答 3

24

:not(p)匹配body

默认color值为inherit

p没有样式集。

p因此从 继承其颜色body,即红色。

解决方案:明确定义你想要的红色,或者明确设置不同的颜色p(即不要使用:not),或者使用:not(body):not(p)

于 2013-04-16T03:07:49.920 回答
7

这看起来是因为您没有为 p 标签定义特定的样式。所以 :not(p) 甚至适用于 body 元素并继承。

于 2013-04-16T03:08:37.367 回答
0

您可以创建一个单独的 id<p> 并且可以not在 css 中使用。

HTML

<p class="hiya" id="hiya">hiya</p>
<p class="hi" id="hi">hi</p>
<h1>this is h1</h1>
<h2>this is h2</h2>

CSS

p:not(#hiya) { color:#ff0000; }

<p>这将为除了<p>id 为“hiya”的之外生成红色输出。

于 2013-04-16T07:08:49.513 回答