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?
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.
The :first-child selector is used to select the specified selector, only if it is the first child of its parent.
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.
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
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 */ }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With