为什么CSS中的超链接样式不会层叠?

5
在下面的HTML/CSS中,为什么链接颜色是绿色而不是蓝色,即为什么“p.description”覆盖了“#nav”,但“p.description a”没有覆盖“#nav a”?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<style type="text/css">
    #nav {
        color: black;   
    }
    #nav a {
        color: green;
    }
    p.description {
        color:red;
    }
    p.description a {
        color:blue;
    }
</style>
</head>
<body>
    <div id="nav">
        <p class="description">This is a test and <a href="#">this is a link</a>.</p>
    </div>
</body>
</html>

相关:https://dev59.com/gHE85IYBdhLWcg3wYSVk - Pekka
2个回答

13

因为ID选择器加类型选择器比两个类型选择器和一个类选择器更具体。请参见特异性规范

所以它确实会级联,但级联发生的顺序规则并不是你想象的那样。


4
为了增加答案的价值,他可以通过将选择器更改为#nav p.description a来变得更加具体。 - Josh Stodola

-3

它是绿色的,因为CSS规则#nav a {color: green;}规定了它。

要使它变成蓝色,请执行以下操作#nav a {color: blue;}


1
这个并不能回答问题。他想知道为什么会出现这种情况,而且他只想在描述段落中看到蓝色链接。 - Matthew Flaschen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接