tags:

views:

30

answers:

2

Is there any way to get a .body_content p:first-child {yada yada} to work if there are elements that come before the first <p>?

Like an H1 or H2

Trying to find the answer for: http://twitter.com/jackmcdade/status/2673916752

We need to target the first p's font-size in a div, even if there are html elements before, such as an h1 or h2.

+2  A: 

If the P isn't the first child, why would you want to apply rules as if it were? What exactly are you trying to accomplish? If you want to apply rules to only the first paragraph, you can do so with jQuery and the following code:

$(".body_content p:first").addClass("blueStuff");

Which would add a class called 'blueStuff' to the first paragraph tag within .body_content.

Jonathan Sampson
+1  A: 

You could possibly use:

p {
/* style as you'd like the first <p> to appear */
}

p + p {
/* style as you want other <p> tags to appear, selects a <p> that is a sibling of another <p>; I think it should style all subsequent instances of the element */
}
David Thomas
p + p selects a paragraph that "immediately follows" another paragraph. See Adjacent sibling selectors in http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors. Yet, this may solve the asker's problem w/o Javascript is his paragraphs do follow each other, which is quite a usual case.
buti-oxa
Yes; that's what I meant when I wrote 'selects a <p> that's a sibling...' Your clarification rates you an upvote. Thanks @ buti-oxa. And yeah; I think I prefer my non- jQuery/JS solution. But, let's face it, I'm clearly biased... =)
David Thomas