5

In the following HTML/CSS, why is the link color green and not blue, i.e. why does "p.description" override "#nav" but "p.description a" does not override "#nav a"?

alt text

<!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>
BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
Edward Tanguay
  • 176,854
  • 291
  • 683
  • 1,015

2 Answers2

13

Because an id selector plus a type selector is more specific than two type selectors and a class selector. See the specification on specificity.

So it does cascade, but the rules for the order in which the cascade happens are not what you thought they were.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
-3

Its green because the css rule #nav a {color: green;} stipulates it.

To make it blue do this #nav a {color: blue;}

user466764
  • 1,179
  • 6
  • 7
  • 1
    This doesn't answer the question. He wants to know why it's behaving like this, and he only wants blue links within the description paragraph. – Matthew Flaschen Oct 08 '10 at 15:43