tags:

views:

65

answers:

3

Why first paragraph not taking this style p:first-child

#content p:first-child {color:#4C4C4C;
font-size:1.2em;
font-weight:bold;
line-height:1.8;
margin-bottom:0.5em;}


<div id="content">
 <h1>Welcome</h1>
   <p>first paragraph</p>
   <p>second paragraph</p>
   <p>third paragraph</p>
</div>

How to select first paragraph from css?

+2  A: 

The selector matches any p element which is the first child of its parent.

In this case the p is the second child of its parent.

Have a look at: ttp://www.w3schools.com/css/pr_pseudo_first-child.asp

Allain Lalonde
A: 

the content of P isn't its first child - from what I've seen a tag such as or would be the first child not the actual content.

Mauro
+2  A: 

While the previous answers have already defined the problem (that the p isn't the first child of the parent div), here's a solution to your problem, to target the first p that follows a h1, depending on your browser:

h1 + p { /* styles any paragraph that immediately follows a h1 element */ }
David Thomas
Very good point. If he's lucky that'll work for him across the board. +1
Allain Lalonde
Well, this seemed the best pure-css method; though obviously applying a class of `.first` (or whatever) to the first paragraph is more cross-browser compatible. Compatibility seems okay (for almost all but IE6, with some problems for IE7 and IE8 *as* IE7): http://quirksmode.org/css/contents.html
David Thomas