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 ensure 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.
You can use the sibling selector
~
:This selects all the
p
elements that come after.hc-reform
, not just the first one.no
>
is a child selector.the one you want is
+
so try
h1.hc-reform + p
browser support isn't great
This is called the adjacent sibling selector, and it is represented by a plus sign...
Note: this is not supported in IE6 or older.
The
>
is a child selector. So if your HTML looks like this:... then that's your ticket.
But if your HTML looks like this:
Then you want the adjacent selector:
Not exactly. The
h1.hc-reform > p
means "anyp
exactly one level underneathh1.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/