Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

css

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?

like image 372
Jitendra Vyas Avatar asked Apr 24 '10 17:04

Jitendra Vyas


People also ask

What is the difference between first child and first-of-type?

The :first-child: The :first-child selector is used to select those elements which are the first-child elements. For :first-child selector the <! DOCTYPE> must be declared for IE8 and earlier versions. The :first-of-type: The :first-of-type Selector is used to targeting the first child of every element of it's parent.

How do I get my first child in CSS selector?

The :first-child selector is used to select the specified selector, only if it is the first child of its parent.

What is a not (: first child?

This selector is used to select every element which is not the first-child of its parent element. It is represented as an argument in the form of :not(first-child) element.


2 Answers

You're looking for the :first-of-type psuedo selector! So, you'd do this to get that first paragraph:

#content p:first-of-type
like image 167
Jordan Brennan Avatar answered Sep 20 '22 09:09

Jordan Brennan


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 */ }
like image 21
David Thomas Avatar answered Sep 21 '22 09:09

David Thomas