tags:

views:

119

answers:

5
+5  Q: 

CSS next element

if i have a header tag <h1 class="hc-reform">title</h1>

h1.hc-reform{
    float:left;
    font-size:30px;
    color:#0e73bb;
    font-weight:bold;
    margin:10px 0px;
}

and after that i have a paragraph <p>stuff here</p>

how can i say using CSS that every <p> tag that follows the h1.hc-reform to use: clear:both;

would that be:

h1.hc-reform > p{
     clear:both;
}

for some reason that's not working.

+11  A: 

This is called the adjacent sibling selector, and it is represented by a plus sign...

h1.hc-reform + p {
  clear:both;
}

Note: this is not supported in IE6 or older.

Josh Stodola
That would only select the `p` that comes just after `h1.hc-reform`. Then again it might be the only one that the `clear: both` needs to be applied on for it to work since it simply clears the `h1` float, so it's still a valid answer.
BoltClock
+1 to the faster gun! and for the link to the w3c
LeguRi
@Josh: (re your deleted comment) yes but it will only select *one* `p`.
BoltClock
@BoltClock Yes you are correct, I mis-read the spec and deleted that comment because it was wrong. This selector will only match the `p` that is immediately preceded by `h1.hc-reform` (with the same parent element, of course).
Josh Stodola
@Josh Stodola wow didn't know about the adjacent sibling selector. Nice, thanks!
danixd
+1  A: 

no > is a child selector.

the one you want is +

so try h1.hc-reform + p

browser support isn't great

Moin Zaman
+1  A: 

The > is a child selector. So if your HTML looks like this:

<h1 class="hc-reform">
    title
    <p>stuff here</p>
</h1>

... then that's your ticket.

But if your HTML looks like this:

<h1 class="hc-reform">
    title
</h1>
<p>stuff here</p>

Then you want the adjacent selector:

h1.hc-reform + p{
     clear:both;
}
LeguRi
I sure hope he didn't nest p's inside h1's.. Also, adjacent only selects the first p.
Litso
A: 

You can use the ~ (tilde):

h1.hc-reform ~ p{
     clear:both;
}

http://reference.sitepoint.com/css/generalsiblingselector

Should work in most browsers but IE6:

http://www.quirksmode.org/css/contents.html#CSS3

Although according to the first link it's buggy still in newer IE versions. I usually go by what quirksmode says though.

Litso
answer fixed, downvote not necessary
Litso
The IE bugs in the first link are obscure edge case stuff, which is probably why quirksmode overlooks them.
Ax
A: 

Not exactly. The h1.hc-reform > p means "any p exactly one level underneath h1.hc-reform".

What you want is h1.hc-reform + p. Of course, that might cause some issues in older versions of Internet Explorer; if you want to make the page compatible with older IEs, you'll be stuck with either adding a class manually to the paragraphs or using some JavaScript (in jQuery, for example, you could do something like $('h1.hc-reform').next('p').addClass('first-paragraph')).

More info: http://www.w3.org/TR/CSS2/selector.html or http://css-tricks.com/child-and-sibling-selectors/

Justin Russell